CodeForces 536A---Tavas and Karafs(二分)

Tavas and Karafs
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
CodeForces 536A
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 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

题意:
根据样例来看,输入a,b,n; 其中a和b满足式子:
a+(i-1)*b=s
然后接下来有n个测试数据,每次询问包括:
l,t,m;
l代表左端点,t代表接下来有t次操作,m代表最多m个元素(-1);

也就是在一个左端点为l的序列里(h1,h2,h3,h4.....hn)在t次询问时间内,最多m个元素-1,直到左端点的最长序列所有数为0,求右端点。

如果没有r的话,输出-1。

其中max(h1,h2,h3.....hn)<=t  &&   (h1+h2+h3+...+hn)<=t*m


看样例:
2(a)    1(b)   4(接下来有4行)
第一个样例:
1 5 3
//1-左端点为1     5-5次操作    3-每次最多3个数参与-1这个操作
首先知道得到的S序列为:2,3,4,5

用二分:

当进行第一次操作时,其中三个数减3,那么还有2次操作,因为每次最多减1,序列可以变为2,0,1,2

第二次操作时,减2,序列变为:0,0,0,0

2 3 4 5

2 0 1 2(二分减3)

0 0 0 0(继续减2)

------------------------

mid   right    left

2         4           1

3         4           3

4         4           4



#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
LL a,b,n,l,t,m;
LL fun(LL p)
{
    return a+(p-1)*b;
}
LL getsum(LL r)
{
    return fun(r)+fun(l)*(r-l+1)/2;
}
int main()
{
    while(scanf("%lld%lld%lld",&a,&b,&n)==3)
    {
        while(n--)
        {
            scanf("%lld%lld%lld",&l,&t,&m);
            if(fun(l)>t)
                printf("-1\n");
            else
            {
                LL left=l,right=(t-a)/b+1,mid;
                while(left<=right)
                {
                    mid=(right+left)/2;
                    //printf("%mid=%lld,right=%lld,left=%lld\n",mid,right,left);
                   // printf("getsum(mid)=%lld\n",getsum(mid));
                    //printf("\n\n");
                    if(getsum(mid)<=t*m)
                       {
                        left=mid+1;
                       }
                    else
                        right=mid-1;
                }
             printf("%lld\n",left-1);
            }
        }
    }
    return 0;
}


















































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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值