codevs 1083 1083 Cantor表【模拟+二分改进】
2016-01-12 20:10
363 查看
题目链接:
http://codevs.cn/problem/1083/
一个模拟题,列表出来就是酱紫:
![](https://images2015.cnblogs.com/blog/815915/201601/815915-20160112200640303-1050015933.png)
这样为了找到数据在哪一组,很明显就要二分一下,自己写一个改进的二分,类似STL中的lower_bound。。。
http://codevs.cn/problem/1083/
一个模拟题,列表出来就是酱紫:
![](https://images2015.cnblogs.com/blog/815915/201601/815915-20160112200640303-1050015933.png)
这样为了找到数据在哪一组,很明显就要二分一下,自己写一个改进的二分,类似STL中的lower_bound。。。
#pragma comment(linker, "/STACK:16777216") //防爆栈 #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<string> #include<vector> #include<set> #include<queue> #include<map> #include<stack> #include<iterator> using namespace std; #define clr(c) memset(c, 0, sizeof(c)); #define pi acos(-1.0) #define debug(x) cout<<"debug "<<x<<endl; #define LLD "%I64d" int dirx[8] = {0, 1, 0, -1, 1, 1, -1, -1}; int diry[8] = {1, 0, -1, 0, 1, -1, -1, 1}; // 移动方向 const int INF = 0x3f3f3f3f; const int mod = 1e9+7; // 模一个很大的素数, 素数的性质使模后得到相同数字的概率会低 const int _mod = 1e9+9; // int最多到2*10^9 long long最多到10^18 这样模后的运算也不会溢出 const double eps = 1e-8; // 浮点数精度 typedef long long ll; typedef unsigned long long ull; typedef struct point{ int x, y; bool operator < (const point& p) const{ if(x == p.x) return y < p.y; else return x < p.x; } bool operator > (const point& p) const{ return p < *this; } }p; const int MAXL = 10005; int sum[MAXL]; void pre(){ sum[1] = 1; for(int i = 2; i <= 10000; i++){ sum[i] = sum[i-1]+i; } } int binarySearch(int value){ // 修正的二分查找算法 //如果找到value就返回value所在的位置 //否则返回小于value的最大元素的位置 int l = 1; int r = 10000; while(l <= r){ int mid = (l+r) / 2; if(sum[mid] == value) return mid; else if(sum[mid] < value) l = mid + 1; else r = mid - 1; } return r;//小于value的最大元素的位置 //l是大于value的最小元素的位置 } int n; int pos, Div, Sum, part1, part2; int main(){ pre(); while(~scanf("%d", &n)){ pos = binarySearch(n); Div = n - sum[pos]; if(Div == 0){ Sum = pos+1; if(pos%2 == 1){ // 奇数 part1 = 1; part2 = Sum-part1; } else{ part2 = 1; part1 = Sum-part2; } } else{ Sum = pos+2; if((pos+1)%2 == 1){ // 奇数 part1 = Sum-Div; part2 = Div; } else{ part1 = Div; part2 = Sum-part1; } } printf("%d/%d\n", part1, part2); } return 0; }
相关文章推荐
- Android Studio——synchronized
- matlab中安装编译器
- 解决can't connect to MySQL server on 'localhost'(10061)问题
- git 常用命令
- iOS 调用相册,拍照并上传图片详解
- Android Studio Gradle编译禁用Lint报错
- 文件后缀和相应的文件类型,打开方法参考大全
- java算法之三快速排序(交换排序)
- 判断字符串是否为空--string.Empty、string=""、s.length==0
- Android程序的调试-输出日志信息的几种方法
- 表示不同文件类型的魔术数字
- Android安装卸载探究
- 4236: JOIOJI 思路题 map
- OMNeT++ ALOHA sample代码阅读
- CentOS 7安装配置Apache HTTP Server
- linux进程高cpu问题分析
- 二维图形旋转公式的推导
- 事件驱动模型Libev(二)
- MIME类型
- Maven查找依赖树,解决jar包冲突问题