CF Tavas and Karafs (二分)
2 seconds
256 megabytes
standard input
standard output
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.
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.
For each query, print its answer in a single line.
2 1 4
1 5 3
3 3 10
7 10 2
6 4 8
4
-1
8
-1
1 5 2
1 5 10
2 7 4
1
2
今天才发现比赛时候的算法是对的。。。只不过数据爆了,改成long long就过了,还重新找题解写了一次。。。。
不过新写的更快一点,130ms,二分查找,下界为L,二分查上界,上界的初始值可以算出来。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <ctime>
using namespace std; long long bsearch(long long,long long,long long); long long A,B;
int main(void)
{
long long l,t,m,r;
long long n; scanf("%lld%lld%lld",&A,&B,&n);
while(n --)
{
scanf("%lld%lld%lld",&l,&t,&m);
if(t < A + (l - ) * B)
{
puts("-1");
continue;
}
r = bsearch(l,t,m);
printf("%lld\n",r);
} return ;
} long long bsearch(long long l,long long t,long long m)
{
long long low = l;
long long high = (t - A) / B + ; while(low <= high)
{
long long mid = (low + high) / ;
long long box = (A + (l - ) * B + A + (mid - ) * B) * (mid - l + ) / ; if(box <= m * t)
low = mid + ;
else
high = mid - ;
} return low - ;
}
CF Tavas and Karafs (二分)的更多相关文章
- C. Tavas and Karafs 二分查找+贪心
C. Tavas and Karafs #include <iostream> #include <cstdio> #include <cstring> #incl ...
- Tavas and Karafs 二分+结论
二分比较容易想到 #include<map> #include<set> #include<cmath> #include<queue> #includ ...
- CodeForces 535C Tavas and Karafs —— 二分
题意:给出一个无限长度的等差数列(递增),每次可以让从l开始的m个减少1,如果某个位置已经是0了,那么可以顺延到下一位减少1,这样的操作最多t次,问t次操作以后从l开始的最长0序列的最大右边界r是多少 ...
- CF 535c Tavas and Karafs
Tavas and Karafs Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u S ...
- Codeforces 535C - Tavas and Karafs
535C - Tavas and Karafs 思路:对于满足条件的r,max(hl ,hl+1 ,hl+2 ,......,hr )<=t(也就是hr<=t)且∑hi<=t*m.所 ...
- 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 ...
- 二分搜索 Codeforces Round #299 (Div. 2) C. Tavas and Karafs
题目传送门 /* 题意:给定一个数列,求最大的r使得[l,r]的数字能在t次全变为0,每一次可以在m的长度内减1 二分搜索:搜索r,求出sum <= t * m的最大的r 详细解释:http:/ ...
- CF 84D Doctor(二分)
题目链接: 传送门 Doctor time limit per test:1 second memory limit per test:256 megabytes Description Th ...
- ProbS CF matlab源代码(二分系统)(原创作品,转载注明出处,谢谢!)
%ProbS clear all;%% 数据读入与预处理 data = load('E:\network_papers\u1.base');test = load('E:\network_papers ...
随机推荐
- (转)Http协议经典详解
转自:http://blog.csdn.net/gueter/archive/2007/03/08/1524447.aspx Author :Jeffrey 引言 HTTP 是一个属于应用层的面向对象 ...
- JSF 2.0 + Ajax hello world example
In JSF 2.0, coding Ajax is just like coding a normal HTML tag, it's extremely easy. In this tutorial ...
- eclipse svn切换账号登陆问题
1.当一个人有权限访问文件代码,而另一个账号无法访问该文件代码,要在eclipse上切换账号登陆有权限的账号时,eclipse会用缓存的账号,不会弹出从新输入新账号的窗口. 这样该怎么解决呢? 关闭e ...
- POJ 2777 Count Color (线段树成段更新+二进制思维)
题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...
- exit和_exit的区别
参考 http://www.cnblogs.com/hnrainll/archive/2011/08/17/2142001.html p.p1 { margin: 0.0px 0.0px 0.0px ...
- 命令行刷机教程( 以Linux系统为例 )
//第一步adb device // 如果不能cd AndroidSDK/platform-toolsadb kill-server adb start-server //第二步adb reboot ...
- 【Java】Treeset实现自定义排序
两个类,一个学生类,含姓名和出生日期两个属性:还有一个学生排序类,重写compare函数,自定义排序规则是先比较出生日期,如果相同再比较姓名字母 package birthday; import ja ...
- thttpd的定时器
运用了static函数实现文件封装 提升变量访问效率的关键字register,该关键字暗示该变量可能被频繁访问,如果可能,请将值存放在寄存器中 内存集中管理,每个节点在取消后并没有立即释放内存,而是调 ...
- window.parent != window 解决界面嵌套问题
页面在被嵌套的时,效果:,,如果用户点击“刷新”,该问题即可解决. 如果想通过代码解决的话,这个问题属于客户端的问题,不是服务器端的问题. 如果直接写:window.location.href = “ ...
- ADO.NET 快速入门(一):ADO.NET 概述
ADO.NET 概述 ADO.NET是改进的ADO数据访问模型用于开发可扩展应用程序.他是专门为可伸缩性.无状态和XML核心的web而设计的. ADO.NET使用一些ADO对象,如Connecti ...