二分查找
1 #include<cstdio> 2 #define min(a,b) (a<b?a:b) 3 typedef __int64 LL; 4 const int maxn=20000+5; 5 LL a[maxn],b[maxn],c[maxn]; 6 int n; 7 LL cal(LL m) 8 { 9 LL sum=0; 10 for(int i=0;i<n;i++) 11 { 12 int tmp=min(b[i],m); 13 if(tmp>=a[i]) 14 sum+=(tmp-a[i])/c[i]+1; 15 } 16 return sum; 17 } 18 int main() 19 { 20 while(scanf("%d",&n)!=EOF) 21 { 22 for(int i=0;i<n;i++) 23 scanf("%I64d%I64d%I64d",&a[i],&b[i],&c[i]); 24 LL l=0,r=1LL<<31; 25 while(l<r) 26 { 27 LL mid=(l+r)>>1; 28 if(cal(mid)&1) r=mid; 29 else l=mid+1; 30 } 31 if(l==1LL<<31) 32 printf("DC Qiang is unhappy. "); 33 else 34 { 35 LL ans=0; 36 for(int i=0;i<n;i++) 37 if(l>=a[i]&&l<=b[i]&&(l-a[i])%c[i]==0) ans++; 38 printf("%I64d %I64d ",l,ans); 39 } 40 } 41 return 0; 42 }