hdu 4768 Flyer (二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4768
算法思路:最核心的就是找到前n个人的传单数总和可以二分。
代码:
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int maxn = 20002; long long A[maxn],B[maxn],C[maxn]; int N; int main() { //freopen("E:\\acm\\input.txt","r",stdin); while(cin>>N) { long long l = 1e12; long long r = 0; for(int i=1; i<=N; i++) { scanf("%I64d %I64d %I64d",&A[i],&B[i],&C[i]); l = min(l,A[i]); r = max(r,B[i]); } long long sum = 0; for(int i=1; i<=N; i++) { sum += (B[i]-A[i])/C[i] + 1; } if(sum%2 == 0) { printf("DC Qiang is unhappy.\n"); } else { while(l < r) { long long mid = (l+r)/2; sum = 0; for(int i=1; i<=N; i++) { int temp = min(B[i],mid)-A[i]; if(temp >= 0) sum += temp/C[i]+1; } if(sum%2) r = mid; else l = mid+1; } int num = 0; for(int i=1; i<=N; i++) { if(l>=A[i] && (l-A[i])%C[i] == 0 && l <= B[i]) num++; } printf("%lld %d\n",l,num); } } }