Tavas and Karafs
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 535C
Description
Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs.
Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.
For a given m, let‘s define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.
Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
Input
The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105).
Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.
Output
For each query, print its answer in a single line.
Sample Input
Input
2 1 41 5 33 3 107 10 26 4 8
Output
4-18-1
Input
1 5 21 5 102 7 4
Output
12
定理 序列h1,h2,...,hn 可以在t次时间内(每次至多让m个元素减少1) 全部减小为0 当且仅当
max(h1, h2, ..., hn) <= t && h1 + h2 + ... + hn <= m*t
然后用二分来做
网上的代码
1 #include<queue> 2 #include<math.h> 3 #include<stdio.h> 4 #include<string.h> 5 #include<iostream> 6 #include<algorithm> 7 using namespace std; 8 #define N 12345678 9 #define M 1234 10 11 long long a,b,n,l,t,m; 12 13 int main() 14 { 15 while(~scanf("%lld%lld%lld",&a,&b,&n)) 16 { 17 for(int i=0;i<n;i++) 18 { 19 scanf("%lld%lld%lld",&l,&t,&m); 20 if(a+(l-1)*b>t) 21 { 22 puts("-1"); 23 continue; 24 } 25 long long ll=l,r=(t-a)/b+1,mid; 26 27 while(ll<=r) 28 { 29 mid=(ll+r)/2; 30 if( (2*a+(mid+l-2)*b)*(mid-l+1)/2 <=t*m) 31 { 32 ll=mid+1; 33 } 34 else 35 { 36 r=mid-1; 37 } 38 } 39 cout<<ll-1<<endl; 40 } 41 } 42 43 return 0; 44 }
我的代码
1 #include<queue> 2 #include<math.h> 3 #include<stdio.h> 4 #include<string.h> 5 #include<iostream> 6 #include<algorithm> 7 using namespace std; 8 #define N 12345678 9 #define M 1234 10 11 long long a,b,n,l,t,m; 12 13 int main() 14 { 15 while(~scanf("%lld%lld%lld",&a,&b,&n)) 16 { 17 for(int i=0;i<n;i++) 18 { 19 scanf("%lld%lld%lld",&l,&t,&m); 20 if(a+(l-1)*b>t) 21 { 22 puts("-1"); 23 continue; 24 } 25 long long ll=l,r=(t-a)/b+1,mid; 26 27 while(ll<=r) 28 { 29 mid=(ll+r)/2; 30 if( (2*a+(mid+l-2)*b)*(mid-l+1)/2 <=t*m 31 && (2*a+(mid+1+l-2)*b)*(mid+1-l+1)/2 >t*m) 32 //这里wa了一次, 把 > 写成了 >= , 33 { 34 break; 35 } 36 else if( (2*a+(mid+l-2)*b)*(mid-l+1)/2 <=t*m) 37 { 38 ll=mid+1; 39 } 40 else 41 { 42 r=mid-1; 43 } 44 } 45 cout<<mid<<endl; 46 } 47 } 48 49 return 0; 50 }