您的位置:首页 > 其它

codevs 1083 1083 Cantor表【模拟+二分改进】

2016-01-12 20:10 363 查看
题目链接:

http://codevs.cn/problem/1083/

一个模拟题,列表出来就是酱紫:



这样为了找到数据在哪一组,很明显就要二分一下,自己写一个改进的二分,类似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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: