题意:给你一个等差数列。每一次选m个不同的数使得都减一,问你最多能承受t次的右端点是多少(左端点已给)要使得这个右端点最右.
解题思路:显然是二分,但是不知道如何判断。官方题解。
解题代码:
1 // File Name: c.cpp 2 // Author: darkdream 3 // Created Time: 2015年04月15日 星期三 01时04分48秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #include<deque> 10 #include<stack> 11 #include<bitset> 12 #include<algorithm> 13 #include<functional> 14 #include<numeric> 15 #include<utility> 16 #include<sstream> 17 #include<iostream> 18 #include<iomanip> 19 #include<cstdio> 20 #include<cmath> 21 #include<cstdlib> 22 #include<cstring> 23 #include<ctime> 24 #define LL long long 25 26 using namespace std; 27 LL a, b, n; 28 LL l , t, m; 29 LL be; 30 int ok(LL k) 31 { 32 if(((be + (k-1) * b ) + be)*k/2 <= m * t && be + (k-1) * b <= t) 33 return 1; 34 return 0 ; 35 } 36 int solve(LL low ,LL high) 37 { 38 while(low <= high) 39 { 40 LL mid = (low + high)/2; 41 if(ok(mid)) 42 { 43 low = mid + 1; 44 }else high = mid -1; 45 } 46 return high; 47 } 48 int main(){ 49 scanf("%lld %lld %lld",&a,&b,&n); 50 for(LL i = 1;i <= n;i ++){ 51 scanf("%lld %lld %lld",&l,&t,&m); 52 be = (l - 1) * b + a; 53 int k = solve(1,1e6); 54 if(k < 1) 55 printf("-1\n"); 56 else printf("%lld\n",k+l-1); 57 } 58 return 0; 59 }
没有梦想,何谈远方