二分搜索 Codeforces Round #299 (Div. 2) C. Tavas and Karafs

 

题目传送门

 1 /*
 2     题意:给定一个数列,求最大的r使得[l,r]的数字能在t次全变为0,每一次可以在m的长度内减1
 3     二分搜索:搜索r,求出sum <= t * m的最大的r
 4     详细解释:http://blog.csdn.net/libin56842/article/details/45082747
 5 */
 6 #include <cstdio>
 7 #include <algorithm>
 8 #include <cstring>
 9 #include <cmath>
10 using namespace std;
11 
12 typedef long long ll;
13 
14 const int MAXN = 1e4 + 10;
15 const int INF = 0x3f3f3f3f;
16 ll A, B, n, l, t, m;
17 
18 ll cal(ll x)    {return A + (x - 1) * B;}
19 
20 ll get_sum(ll l, ll r)
21 {
22     return (cal (l) + cal (r)) * (r - l + 1) / 2;
23 }
24 
25 int main(void)        //Codeforces Round #299 (Div. 2) C. Tavas and Karafs
26 {
27     while (scanf ("%I64d%I64d%I64d", &A, &B, &n) == 3)
28     {
29         for (int i=1; i<=n; ++i)
30         {
31             scanf ("%I64d%I64d%I64d", &l, &t, &m);
32             if (cal (l) > t)    {puts ("-1");    continue;}
33             ll x = l;    ll r = (t - A) / B + 1;
34             while (x <= r)
35             {
36                 ll mid = (x + r) / 2;
37                 if (get_sum (l, mid) <= t * m)    x = mid + 1;
38                 else    r = mid - 1;
39             }
40             printf ("%d\n", r);
41         }
42 
43     }
44 
45     return 0;
46 }

 

转载于:https://www.cnblogs.com/Running-Time/p/4547127.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值