Tavas and Karafs

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

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

定理  序列h1,h2,...,hn 可以在t次时间内(每次至多让m个元素减少1)  全部减小为0  当且仅当

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

然后用二分来做

网上的代码

 #include<queue>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 12345678
#define M 1234 long long a,b,n,l,t,m; int main()
{
while(~scanf("%lld%lld%lld",&a,&b,&n))
{
for(int i=;i<n;i++)
{
scanf("%lld%lld%lld",&l,&t,&m);
if(a+(l-)*b>t)
{
puts("-1");
continue;
}
long long ll=l,r=(t-a)/b+,mid; while(ll<=r)
{
mid=(ll+r)/;
if( (*a+(mid+l-)*b)*(mid-l+)/ <=t*m)
{
ll=mid+;
}
else
{
r=mid-;
}
}
cout<<ll-<<endl;
}
} return ;
}

我的代码

 #include<queue>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 12345678
#define M 1234 long long a,b,n,l,t,m; int main()
{
while(~scanf("%lld%lld%lld",&a,&b,&n))
{
for(int i=;i<n;i++)
{
scanf("%lld%lld%lld",&l,&t,&m);
if(a+(l-)*b>t)
{
puts("-1");
continue;
}
long long ll=l,r=(t-a)/b+,mid; while(ll<=r)
{
mid=(ll+r)/;
if( (*a+(mid+l-)*b)*(mid-l+)/ <=t*m
&& (*a+(mid++l-)*b)*(mid+-l+)/ >t*m)
//这里wa了一次, 把 > 写成了 >= ,
{
break;
}
else if( (*a+(mid+l-)*b)*(mid-l+)/ <=t*m)
{
ll=mid+;
}
else
{
r=mid-;
}
}
cout<<mid<<endl;
}
} return ;
}

CF 535c Tavas and Karafs的更多相关文章

  1. Codeforces 535C - Tavas and Karafs

    535C - Tavas and Karafs 思路:对于满足条件的r,max(hl ,hl+1 ,hl+2 ,......,hr )<=t(也就是hr<=t)且∑hi<=t*m.所 ...

  2. CodeForces 535C Tavas and Karafs —— 二分

    题意:给出一个无限长度的等差数列(递增),每次可以让从l开始的m个减少1,如果某个位置已经是0了,那么可以顺延到下一位减少1,这样的操作最多t次,问t次操作以后从l开始的最长0序列的最大右边界r是多少 ...

  3. CF Tavas and Karafs (二分)

    Tavas and Karafs time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  4. C. Tavas and Karafs 二分查找+贪心

    C. Tavas and Karafs #include <iostream> #include <cstdio> #include <cstring> #incl ...

  5. Codeforces Round #299 (Div. 1) A. Tavas and Karafs 水题

    Tavas and Karafs Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/536/prob ...

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

    题目传送门 /* 题意:给定一个数列,求最大的r使得[l,r]的数字能在t次全变为0,每一次可以在m的长度内减1 二分搜索:搜索r,求出sum <= t * m的最大的r 详细解释:http:/ ...

  7. codeforces 536a//Tavas and Karafs// Codeforces Round #299(Div. 1)

    题意:一个等差数列,首项为a,公差为b,无限长.操作cz是区间里选择最多m个不同的非0元素减1,最多操作t次,现给出区间左端ll,在t次操作能使区间全为0的情况下,问右端最大为多少. 这么一个简单题吞 ...

  8. Tavas and Karafs 二分+结论

    二分比较容易想到 #include<map> #include<set> #include<cmath> #include<queue> #includ ...

  9. 【Codeforces Round #299 (Div. 2) C】 Tavas and Karafs

    [链接] 我是链接,点我呀:) [题意] 给你一个规则,让你知道第i根萝卜的高度为si = A+(i-1)*B 现在给你n个询问; 每次询问给你一个固定的起点l; 让你找一个最大的右端点r; 使得l. ...

随机推荐

  1. 利用Vert.x构建简单的API 服务、分布式服务

    目前已经使用Vertx已经一年多了,虽然没有太多的造诣,但也已在项目中推广了下:从最初的vertx搭建web服务,到项目上线运营,还算比较稳定.再到后来尝试搭建基于vertx的分布式服务,一路下来也积 ...

  2. python 三——列表、字典、元祖、字符串、set

    本节内容 1.列表 2.元祖 3.字典 4.字符串 不可变类型:整型.字符串.元组tuple 可变类型:列表list.字典dict 1.列表 >>> names ['Alex', ' ...

  3. nodejs 如何发送一个带JSON的GET请求?

    GET /megacorp/employee/_search { "aggs" : { "all_interests" : { "terms" ...

  4. python3--__call__拦截调用

    __call__拦截调用 当实例调用时,使用__call__方法.不,这不是循环定义:如果定义了,Python就会为实例应用函数调用表达式运行__call__方法.这样可以让类实例的外观和用法类似于函 ...

  5. CF802D

    D. Marmots (easy) time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  6. hdu6059[字典树+思维] 2017多校3

    #include <bits/stdc++.h> using namespace std; typedef long long LL; * ][]; * ]; * ]; ][]; ; LL ...

  7. apache kafka系列之客户端开发-java

    1.依赖包 <dependency>            <groupId>org.apache.kafka</groupId>            <a ...

  8. bzoj1063【Noi2008】道路设计

    题意:http://www.lydsy.com/JudgeOnline/problem.php?id=1063 用一种划分方式将树划为重链和轻链,使得所有点到根节点的路径经过的轻链最大值最小 sol: ...

  9. 【NOI2012】骑行川藏

    获得成就:第一次在信竞做神仙数学题 先放个前言,$OI$ 出大型数学题还是比较麻烦的,因为主要是考你数学推导 / 手算式子,你算出来之后把公式套个板子,就得到结论——$OI$ 的大型数学题的代码都是板 ...

  10. C 语言中的 fgets()

    转自:http://blog.csdn.net/daiyutage/article/details/8540932 原型: char *  fgets(char * s, int n,FILE *st ...