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
SRM 507(2-1000pt)的更多相关文章
- TC250专场
SRM 623 DIV2 1000pt 题意:给出一个最多50*50的矩阵,每个单元可能为'.'.'P'.'A','.'代表空地,你每次操作可以把一个P或者A拿到空地上,求一个最大的含有相同字符的矩形 ...
- SRM149 - SRM150(少SRM150-DIV1-LV3)
SRM 149 DIV2 1000pt 题意: 对于n个人,第i人有pi的钱.将他们分成不超过四个组,每组统一交费x,对每个人,若他拥有的钱超过x则交费,否则不交费.问最多能使这些人交多少钱. 1&l ...
- Topcoder 好题推荐
SRM SRM147 DIV1 1000pt DP SRM148 DIV1 1100pt 递归 SRM149 DIV1 1000pt math SRM150 DIV1 500pt DP SRM469 ...
- 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)数列所有 ...
- SRM 508(2-1000pt)
DIV2 1000pt 题意:给定整数n和r,求有多少个这样的数列,a1,a2...an,使得a1 + a2 +...+an = a1|a2|a3|...|an,(按位或).输出这样数列的个数mod ...
- 记第一次TopCoder, 练习SRM 583 div2 250
今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...
- SRM 513 2 1000CutTheNumbers(状态压缩)
SRM 513 2 1000CutTheNumbers Problem Statement Manao has a board filled with digits represented as St ...
- SRM 510 2 250TheAlmostLuckyNumbersDivTwo(数位dp)
SRM 510 2 250TheAlmostLuckyNumbersDivTwo Problem Statement John and Brus believe that the digits 4 a ...
- SRM 657 DIV2
-------一直想打SRM,但是感觉Topcoder用起来太麻烦了.题目还是英文,不过没什么事干还是来打一打好了.但是刚注册的号只能打DIV2,反正我这么弱也只适合DIV2了.. T1: 题目大意: ...
随机推荐
- String or binary data would be truncated. The statement has been terminated.
常见的情况为:插入的值大于字段定义的最大长度. String or binary data would be truncated. The statement has been terminated
- Datatables+Bootstrap
http://sandbox.runjs.cn/show/thwac3ec 运行效果 <!DOCTYPE html> <html lang="en"> &l ...
- oracle 触发器number判断空值,:NEW赋值,for each row,sql变量引号,to_date,to_char
1.number类型在库中可能存在null这种数据 判断是否为空时要用如下: IF(nvl(:NEW.BACAH,0) <>0) 不能用IF(BACAH IS NOT NULL) 2. 2 ...
- 用PHP实现一个高效安全的ftp服务器(一)
摘要: 本文主要阐述使用PHP的swoole扩展实现ftp服务器,同时扩展ftp服务器个性化功能和安全性.真正实现一个自己完全掌控的ftp服务器,可以个性化定制的ftp服务器. 正文: FTP服务器想 ...
- JSONKit 在iOS9 arm7 64位下出现的问题
最近遇到了一个关于JSONKit的问题,在项目加了arm7 64位以后,JSONKIT会出现[params JSONString] forKey:@”gson”];报错的情况,如下图 具体原因不太清楚 ...
- 【深度解析】Google第二代深度学习引擎TensorFlow开源
作者:王嘉俊 王婉婷 TensorFlow 是 Google 第二代深度学习系统,今天宣布完全开源.TensorFlow 是一种编写机器学习算法的界面,也可以编译执行机器学习算法的代码.使用 Tens ...
- Cocos2dx开发(3)——Cocos2dx打包成APK,ANT环境搭建
前面cocos2dx的运行环境(Android SDK,JDK,),最后Cocos2dx的APK的打包环境,最运行环境上再加ANT环境就好了 1.ANT下载配置 官网下载:http://ant.apa ...
- 安装完 MySQL 后必须调整的 10 项配置
原文出处: mysqlperformanceblog 译文出处:开源中国 欢迎分享原创到伯乐头条 当我们被人雇来监测MySQL性能时,人们希望我们能够检视一下MySQL配置然后给出一些提高建议 ...
- Python 环境
文章出处:http://www.cnblogs.com/winstic/,请保留此连接 总结一下自己使用python过程中安装三方包的方法 Python 安装 Python的安装非常简单,本人使用的w ...
- 【转】python中List的sort方法(或者sorted内建函数)的用法
原始出处:http://gaopenghigh.iteye.com/blog/1483864 python列表排序 简单记一下python中List的sort方法(或者sorted内建函数)的用法. ...