题目:
http://poj.org/problem?id=3484
思路:
又是二分咯, 这道题的输入比较神奇,以换行符来判断一个样例的结束.用到sscanf.
AC.
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn = 1000005;
char s[100];
int n;
ll x[maxn], y[maxn], z[maxn];
bool can(ll m)
{
ll s = 0;
for(int i = 0; i < n; ++i) {
if(m < x[i]) continue;
else if(m > y[i]) s += ( (y[i]-x[i])/z[i]+1 );
else s += ( (m - x[i])/z[i]+1 );
}
if(s % 2) {
return true;
}
return false;
}
void solve()
{
ll l = 0, r = 1e15;
while(r - l > 1) {
ll mid = (r+l)/2;
if(can(mid)) r = mid;
else l = mid;
}
if(l == 1e15-1) printf("no corruption\n");
else {
ll sum = 0;
for(int i = 0; i < n; ++i) {
if(r >= x[i] && r <= y[i] && (r - x[i]) % z[i] == 0)
sum++;
}
printf("%I64d %I64d\n", r, sum);
}
}
int main()
{
//freopen("in", "r", stdin);
n = 0;
while(gets(s) != NULL) {
if(strlen(s) != 0) {
sscanf(s, "%lld %lld %lld", &x[n], &y[n], &z[n]);
n++;
}
else if(n) {
solve();
n = 0;
}
}
if(n) {
solve();
}
return 0;
}