CF 535c 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
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
定理 序列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的更多相关文章
- Codeforces 535C - Tavas and Karafs
535C - Tavas and Karafs 思路:对于满足条件的r,max(hl ,hl+1 ,hl+2 ,......,hr )<=t(也就是hr<=t)且∑hi<=t*m.所 ...
- CodeForces 535C Tavas and Karafs —— 二分
题意:给出一个无限长度的等差数列(递增),每次可以让从l开始的m个减少1,如果某个位置已经是0了,那么可以顺延到下一位减少1,这样的操作最多t次,问t次操作以后从l开始的最长0序列的最大右边界r是多少 ...
- CF Tavas and Karafs (二分)
Tavas and Karafs time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- C. Tavas and Karafs 二分查找+贪心
C. Tavas and Karafs #include <iostream> #include <cstdio> #include <cstring> #incl ...
- 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:/ ...
- codeforces 536a//Tavas and Karafs// Codeforces Round #299(Div. 1)
题意:一个等差数列,首项为a,公差为b,无限长.操作cz是区间里选择最多m个不同的非0元素减1,最多操作t次,现给出区间左端ll,在t次操作能使区间全为0的情况下,问右端最大为多少. 这么一个简单题吞 ...
- Tavas and Karafs 二分+结论
二分比较容易想到 #include<map> #include<set> #include<cmath> #include<queue> #includ ...
- 【Codeforces Round #299 (Div. 2) C】 Tavas and Karafs
[链接] 我是链接,点我呀:) [题意] 给你一个规则,让你知道第i根萝卜的高度为si = A+(i-1)*B 现在给你n个询问; 每次询问给你一个固定的起点l; 让你找一个最大的右端点r; 使得l. ...
随机推荐
- Java-从字符串或一个子字符串中搜索一个字符
indexOf函数 package com.tj; public class MyClass implements Cloneable { public static void main(String ...
- 在xcode上把你的app多语言国际化(NSLocalizedString)
1.到project->info->localizations 下面的加号,添加你需要的语言 千万不要删除 base 否虽然我不知道有什么用,我是删了整个storyboard没了,很 ...
- 开始 python programming第三版案例分析
最近研究python,打算将python programming第三版案例分析下 但是全书1600多页 比较费时 而且 介绍太多 感觉没有必要! python programming 堪称经典之作 第 ...
- 关于面试总结-python笔试题
关于面试总结4-python笔试题 前言 现在面试测试岗位,一般会要求熟悉一门语言(python/java),为了考验求职者的基本功,一般会出2个笔试题,这些题目一般不难,主要考察基本功. 要是给你一 ...
- Linux 文件/文件夹重命名
mv命令既可以重命名,又可以移动文件或文件夹. 例子:将目录A重命名为Bmv A B 例子:将/a目录移动到/b下,并重命名为cmv /a /b/c 其实在文本模式中要重命名文件或目录,只需要使用mv ...
- 第42届亚洲区域赛青岛站(2017icpc青岛)经验总结以及一些感想
上一次写这种东西还是天梯赛,当时打完心里也是挺激动的,然后我们队也没有去参加省赛,但是过了一段时间我还是从那里面恢复了出来.因为我当时确实还是很菜的,当时连个暴力都不会,看着自己仅过的那些百度的题目确 ...
- 组合数学的卡特兰数 TOJ 3551: Game of Connections
这个就是卡特兰数的经典问题 直接用这个公式就好了,但是这个题涉及大数的处理h(n)=h(n-1)*(4*n-2)/(n+1) 其实见过好几次大数的处理了,有一次他存的恰好不多于30位,直接分成两部分l ...
- [luoguP1053] 篝火晚会(贪心 + 乱搞)
传送门 假设第一个位置是1,那么枚举它的左右两边是谁,有两种情况,然后可以递推求出序列. 然后可以贪心,两个序列有多少个不同的数,答案就是多少,具体为啥,yy一下即可 然后就是判断递推求出的序列和目标 ...
- 浅谈中途相遇攻击--meet-in-the-middle attack
貌似挖的坑也够多了....好多都没填,这篇最后会不会TJ还得看心情TUT 看过大白书的人应该都会发现一种神奇的算法:中途相遇法.(在第58页)这种算法将以空间换时间的思路运用到了极致,但事实上它在密码 ...
- bzoj3196 二逼平衡树 树套树(线段树套Treap)
Tyvj 1730 二逼平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 4697 Solved: 1798[Submit][Status][D ...