codeforces535C:Tavas and Karafs(二分)

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 thatl ≤ 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 4
1 5 3
3 3 10
7 10 2
6 4 8
Output
4
-1
8
-1
Input
1 5 2
1 5 10
2 7 4
Output
1
2

昨天用模拟算的,结果总是WA。。。
好不容易看明白一道英文题目,还老是WA ╭∩╮(︶︿︶)╭∩╮
简单的说一下题目的意思:
其实s[i]就是以a为首项,以b为公差的等差数列(i从1开始算,如果i从0开始算,首项就是a-b)。每次操作就是把从l开始的m个元素减一,如果减到0,就向右移一位,一共执行t次这样的操作 (感觉说的挺明白的 (‾◡◝) )。最后输出最长0序列右边界是多少 (嗯嗯,就是这样的 ( ^_^ ) )
传说还存在一个定理(看到别人的博客上写的不造出自哪里):
序列h1,h2,…,hn 可以在t次时间内(每次至多让m个元素减少1) 全部减小为0 当且仅当 max(h1, h2, …, hn) <= t && h1 + h2 + … + hn <= m*t 那么就可以二分右端点来解决了 下限为l 上限为hi不超过t的最大i
终于要上代码了 Y(^o^)Y


 #include <iostream>
 #include <cstdio>
 #include <string.h>
typedef long long int ll;
using namespace std;
ll a, b, n, l, t, m;
ll s(ll x)
{
    return a + (x - 1) * b;
} 
ll get_sum(ll r)
{
    return (s(l) + s(r)) * (r - l + 1) / 2;
}
int main()
{
 #ifndef ONLINE_JUDGE
    freopen("1.txt", "r", stdin);
 #endif
    while(scanf("%I64d%I64d%I64d", &a, &b, &n) !=EOF)
    {
        //cout << a << b << n;
        while(n--)
        {
            cin >> l >> t >> m;
            if (s(l) > t)
            {
                printf("-1\n");
                continue;
            }
            ll left = l, right = (t - a)/b + 1, mid; //s(right) = a - (right - 1) * b <= t
            while(left <= right)
            {
                mid = (left + right) / 2;
                if(get_sum(mid) <= t * m)
                {
                    left = mid + 1;
                }
                else
                {
                    right = mid - 1;
                }
            }
            cout << left - 1 << endl;

        }
    }
    return 0;
} 

加油! ↖(^ω^)↗

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值