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

SRM 507(2-1000pt)的更多相关文章

  1. TC250专场

    SRM 623 DIV2 1000pt 题意:给出一个最多50*50的矩阵,每个单元可能为'.'.'P'.'A','.'代表空地,你每次操作可以把一个P或者A拿到空地上,求一个最大的含有相同字符的矩形 ...

  2. SRM149 - SRM150(少SRM150-DIV1-LV3)

    SRM 149 DIV2 1000pt 题意: 对于n个人,第i人有pi的钱.将他们分成不超过四个组,每组统一交费x,对每个人,若他拥有的钱超过x则交费,否则不交费.问最多能使这些人交多少钱. 1&l ...

  3. Topcoder 好题推荐

    SRM SRM147 DIV1 1000pt DP SRM148 DIV1 1100pt 递归 SRM149 DIV1 1000pt math SRM150 DIV1 500pt DP SRM469 ...

  4. SRM144 - SRM 148(少144-DIV1-LV3,147-DIV2-LV3)

    SRM 144 DIV 1 500pt tag:组合 题意:彩票中奖.给定n, m,从1-n中选择m个数组成数列a1, a2, a3...am.对于数列{am}分别满足以下条件的概率: (1)数列所有 ...

  5. SRM 508(2-1000pt)

    DIV2 1000pt 题意:给定整数n和r,求有多少个这样的数列,a1,a2...an,使得a1 + a2 +...+an = a1|a2|a3|...|an,(按位或).输出这样数列的个数mod  ...

  6. 记第一次TopCoder, 练习SRM 583 div2 250

    今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...

  7. SRM 513 2 1000CutTheNumbers(状态压缩)

    SRM 513 2 1000CutTheNumbers Problem Statement Manao has a board filled with digits represented as St ...

  8. SRM 510 2 250TheAlmostLuckyNumbersDivTwo(数位dp)

    SRM 510 2 250TheAlmostLuckyNumbersDivTwo Problem Statement John and Brus believe that the digits 4 a ...

  9. SRM 657 DIV2

    -------一直想打SRM,但是感觉Topcoder用起来太麻烦了.题目还是英文,不过没什么事干还是来打一打好了.但是刚注册的号只能打DIV2,反正我这么弱也只适合DIV2了.. T1: 题目大意: ...

随机推荐

  1. java异常类的使用

    1.异常的概念 什么是异常?程序出错分为两部分,编译时出粗和运行时出错.编译时出错是编译器在编译源码时发生的错误: 运行时出错是在编译通过,在运行时出现的错误.这种情况叫异常. 例如:数组越界,除数为 ...

  2. Spring通过SchedulerFactoryBean实现调度任务的配置

    http://blog.csdn.net/hu_shengyang/article/details/19815201(里面是配置) 介绍SchedulerFactoryBean http://blog ...

  3. iOS中MVVM的架构设计与团队协作

    对MVVM的理解主要是借鉴于之前的用过的MVC的Web框架,之前用过ThinkPHP框架,和SSH框架,都是MVC的架构模式,今天MVVM与传统的MVC可谓是极为相似,也可以说是兄弟关系,也就是一家人 ...

  4. Codeforces 543B Destroying Roads(最短路)

    题意: 给定一个n个点(n<=3000)所有边长为1的图,求最多可以删掉多少条边后,图满足s1到t1的距离小于l1,s2到t2的距离小于l2. Solution: 首先可以分两种情况讨论: 1: ...

  5. Idea中运行Testng时,报SAXParseException:parallel为none的问题原因及解决

    今天更新了testng的版本为6.9.10, 在idea中运行测试案例时,报错如下: org.testng.TestNGException: org.xml.sax.SAXParseException ...

  6. ICE学习第二步-----从第一个程序了解ICE(HelloWorld)

    ICE(Internet Communications Engine)是一种面向对象的中间件平台,主要用于网络通讯.它为面向对象的“客户端-服务器”模型的应用提供了一组很好的工具和API接口.目前在全 ...

  7. java 按天创建文件夹

    按天创建文件夹,也就是每天创建一个,适合上传文件服务使用,文件数量较多时可以按文件夹区分. public static final String FMT = "yyyy-MM-dd" ...

  8. 如何将eclipse里的项目发布到github

    首先,给eclipse安装上EGit 在“Help > Install new software”中添加 http://download.eclipse.org/egit/updates 两个都 ...

  9. github 中redisPhpAdmin redis 可视化界面

    GITHUB地址:https://github.com/ErikDubbelboer/phpRedisAdmin 在php目录下执行 git clone https://github.com/Erik ...

  10. 离散傅里叶变换(DFT)

    目录     一.研究的意义     二.DFT的定义    三.DFT与傅里叶变换和Z变换的关系     四.DFT的周期性     五.matlab实验       五.1 程序         ...