SRM 507(2-1000pt)

DIV2 1000pt

题意:在一个长度无限的数轴上移动一个方块,每次可以向左或者向右移动距离x,只要x为完全平方数。数轴上有一些坑,如果方块移动到坑上则方块会掉进坑中,不能再被移动。给整数s,e,和所有坑的位置hol[i],求最少多少步能够将方块从s点移动到e点,若不能从s移动到e,则返回-1。1 <= s, e, hol[i]  <= 100000,且他们互不相同。

   注意,若s = 1, e = 5, hol[0] = 3,则答案为-1。

解法:首先,从从s移动到e和从e移动到s的需要的步数是一样的,所以可以不妨设s < e。

   分为三种情况:存在一个i使得s < hol[i] < e,则答案一定为-1。

   若存在一个hol[i] < s和一个hol[j] > e,则将无限的数轴变为有限的,且1 <= hol[i] <= 100000,所以只需要一个建图用BFS做即可。

   

   然后先证明, 如果不存在hol[i] < s存在hol[j] > e,和不存在hol[i] < s且不存在hol[j] > e是等价的情况。这个很容易想明白,比如,从10转化到21,可以10 -> -15 -> 21,也可以10 ->46 -> 21。

   所以,剩下最后一种情况等价于,数轴上没有坑。设d = e - s,则无论如何,三步之内一定能从s走到e。

   若d为完全平方数,一步即可。

   若d = x*x + y*y,则两步即可。(枚举判断)

   若d为奇数,则d = 1 * d = (a+b) * (a-b),即a = (d+1)/2,b = (d-1)/2,则a*a - b*b = d,两步即可。

   若d为偶数。则如果d为4的倍数,同样可以求出a和b使得d=a*a-b*b,两步即可。若d不为4的倍数,则d = 1 + (d-1),三步即可。

   最后这种数轴上没有坑的情况,不能仅通过扩大有限数轴的范围然后用BFS的方法来做,因为范围会大到超时,而且也不容易判断具体范围要扩大到多大。

tag:math, BFS, good

 // BEGIN CUT HERE
/*
* Author: plum rain
* score :
*/
/* */
// END CUT HERE
#line 11 "CubeRoll.cpp"
#include <sstream>
#include <stdexcept>
#include <functional>
#include <iomanip>
#include <numeric>
#include <fstream>
#include <cctype>
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <set>
#include <queue>
#include <bitset>
#include <list>
#include <string>
#include <utility>
#include <map>
#include <ctime>
#include <stack> using namespace std; #define CLR(x) memset(x, 0, sizeof(x))
#define CLR1(x) memset(x, -1, sizeof(x))
#define PB push_back
#define SZ(v) ((int)(v).size())
#define zero(x) (((x)>0?(x):-(x))<eps)
#define out(x) cout<<#x<<":"<<(x)<<endl
#define tst(a) cout<<#a<<endl
#define CINBEQUICKER std::ios::sync_with_stdio(false) typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long int64;
typedef pair<int, int> pii; const double eps = 1e-;
const double PI = atan(1.0)*;
const int maxint = ;
const int maxx = ; bool v[];
pii an[]; bool ok(int a)
{
int x = sqrt(a + 0.0);
if ((x-)*(x-) == a) return ;
if (x * x == a) return ;
if ((x+)*(x+) == a) return ;
return ;
} int BFS (int l, int r, int s, int e)
{
CLR (v);
v[s] = ;
int il = , ir = ;
pii t; t.second = ;
for (int i = ; i < maxx; ++ i){
if (s + i*i < r){
v[s+i*i] = ;
t.first = s + i*i; an[ir++] = t;
continue;
}
else break;
}
for (int i = ; i < maxx; ++ i){
if (s - i*i > l){
v[s - i*i] = ;
t.first = s - i*i; an[ir++] = t;
}
else break;
} while (il < ir){
pii tmp = an[il++];
if (tmp.first == e) return tmp.second; pii t1 = tmp; ++ t1.second;
for (int i = ; i < maxx; ++ i){
if (!v[tmp.first+i*i] && tmp.first + i*i < r){
t1.first = tmp.first + i*i; an[ir++] = t1;
v[t1.first] = ;
}
if (tmp.first + i*i >= r) break;
}
for (int i = ; i < maxx; ++ i){
if (!v[tmp.first-i*i] && tmp.first - i*i > l){
t1.first = tmp.first - i*i; an[ir++] = t1;
v[t1.first] = ;
}
if (tmp.first - i*i <= l) break;
}
}
return -;
} int gao(int d)
{
for (int i = ; i < maxx; ++ i)
for (int j = ; j < maxx; ++ j)
if (i*i + j*j == d) return ;
return ;
} class CubeRoll
{
public:
int getMinimumSteps(int s, int e, vector <int> hol){
if (s > e) swap(s, e);
bool tt = ;
int n = hol.size(), l = , r = ;
for (int i = ; i < n; ++ i){
if (hol[i] < s) l = max(l, hol[i]);
if (hol[i] > e) r = min(r, hol[i]);
if (s < hol[i] && hol[i] < e) tt = ;
} if (tt) return -;
if (l && r != ) return BFS(l, r, s, e); int d = e - s;
if (ok(d)) return ;
if (d & || !(d % )) return ;
return gao(d);
} // BEGIN CUT HERE
public:
void run_test(int Case) { if ((Case == -) || (Case == )) test_case_0(); if ((Case == -) || (Case == )) test_case_1(); if ((Case == -) || (Case == )) test_case_2(); if ((Case == -) || (Case == )) test_case_3(); if ((Case == -) || (Case == )) test_case_4(); }
private:
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
void test_case_0() { int Arg0 = ; int Arg1 = ; int Arr2[] = {}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[]))); int Arg3 = -; verify_case(, Arg3, getMinimumSteps(Arg0, Arg1, Arg2)); }
void test_case_1() { int Arg0 = ; int Arg1 = ; int Arr2[] = {, , , }; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[]))); int Arg3 = ; verify_case(, Arg3, getMinimumSteps(Arg0, Arg1, Arg2)); }
void test_case_2() { int Arg0 = ; int Arg1 = ; int Arr2[] = {,}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[]))); int Arg3 = ; verify_case(, Arg3, getMinimumSteps(Arg0, Arg1, Arg2)); }
void test_case_3() { int Arg0 = ; int Arg1 = ; int Arr2[] = {,,,}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[]))); int Arg3 = ; verify_case(, Arg3, getMinimumSteps(Arg0, Arg1, Arg2)); }
void test_case_4() { int Arg0 = ; int Arg1 = ; int Arr2[] = {,,,}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[]))); int Arg3 = ; verify_case(, Arg3, getMinimumSteps(Arg0, Arg1, Arg2)); } // END CUT HERE }; // BEGIN CUT HERE
int main()
{
// freopen( "a.out" , "w" , stdout );
CubeRoll ___test;
___test.run_test(-);
return ;
}
// END CUT HERE
上一篇:easyui源码翻译1.32--SearchBox(搜索框)


下一篇:C/C++杂记:深入理解数据成员指针、函数成员指针