Flyer HDU - 4768
原题链接
考察:二分
思路:
前缀和,二分右端点找出现总次数为奇数的r
Code
#include <iostream>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 20010;
int n,a[N],b[N],c[N];
LL check(LL mid)
{
LL sum = 0;
for (int i = 1; i <= n;i++)
sum += max((LL)(min(b[i]*1ll,mid)-a[i]) / c[i]+(a[i]<=mid?1:0),0ll);
return sum;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
for (int i = 1; i <= n;i++)
scanf("%d%d%d", &a[i], &b[i], &c[i]);
LL l = 1, r = (1ll << 32) + 1;
while(l<r)
{
LL mid = l + r >> 1;
if(check(mid)&1)
r = mid;
else
l = mid + 1;
}
if(r>(1ll<<32))
puts("DC Qiang is unhappy.");
else
printf("%lld %lld\n", r,check(r)-check(r-1));
}
return 0;
}