poj 1003 [离线预处理 + 二分]

来源:互联网 发布:java编写计算器教程 编辑:程序博客网 时间:2024/06/16 18:36
#include <iostream>#include <string>#include <cstring>#include <cstdlib>#include <cstdio>#include <cmath>#include <vector>#include <stack>#include <queue>#include <map>#include <set>#include <iterator>#include <algorithm>#define MID(x,y) ( ( x + y ) >> 1 )#define L(x) ( x << 1 )#define R(x) ( x << 1 | 1 )#define BUG puts("here!!!");#define STOP system("pause");using namespace std;const int N = 300;const double norm = 0.00000001;int zero(double x) {if(x < -norm) return -1;return x > norm;}int main() {double len[N];int total;len[0] = 0.0;for(total = 1; zero(len[total-1] - 5.20) <= 0; total++) { len[total] = len[total - 1] + 1.0 / double(total + 1);}double x;while(cin >> x, zero(x)) {int l, r;l = 0, r = total;while(l <= r) {int mid = MID(l, r);if(zero(len[mid] - x) <= 0) {l = mid + 1;}else r = mid - 1;}cout << r + 1 << " card(s)" << endl;}return 0;}