Codeforces Round #299 (Div. 2) C. Tavas and Karafs 二分搜索+数列
来源:互联网 发布:能过淘宝账号查一个人 编辑:程序博客网 时间:2024/06/05 00:43
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 issi = 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.
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 41 5 33 3 107 10 26 4 8
4-18-1
1 5 21 5 102 7 4
12
Source
Codeforces Round #299 (Div. 2)
My Solution
题意:每个萝卜长度为 hi = a + (i - 1) * b,然后每次询问是每次操作最多把 m 个不同的未吃完的萝卜每个咬掉1单位长度,最多 t 次操作,其中求最大的r,是的[l......r]访问内的萝卜被这 t,m的操作吃完
二分搜索+数列
首先r必须且只要满足2个条件即可,1)a + (r - 1) * b <= t
2) sum{hi | l <= i <= r } <= t * m
所以对于每个询问进行一次二分搜索即可。
suml = (2 * a + ((l - 1) - 1) * b) * (l - 1) / 2;LL x = l, y = (t - a) / b + 1 + 1, mid; // y = (t - a) / b + 1 + 1 多加个1不上整数除法的精度损失, 也可以直接用y = 2e6
while(x + 1 < y){
mid = (x + y) >> 1;
if(check(a, b, suml, t, m, mid)) x = mid;
else y = mid;
}
然后答案可能在x里也可能在y里,且y比x更优,所以
if(check(a, b, suml, t, m, y)) r = y;
else if(check(a, b, suml, t, m, x)) r = x;
else r = -1;
即可
复杂度 O(nlogn)
#include <iostream>#include <cstdio>#include <cstring>using namespace std;typedef long long LL;const int maxn = 1e6 + 8;inline bool check(const LL &a, const LL &b, LL &suml, const LL &t, const LL &m, const LL &mid){ if(a + (mid - 1) * b > t) return false; if((2*a + (mid - 1) * b) * mid / 2 - suml > t * m) return false; else return true;}int main(){ #ifdef LOCAL freopen("c.txt", "r", stdin); //freopen("c.out", "w", stdout); int T = 2; while(T--){ #endif // LOCAL ios::sync_with_stdio(false); cin.tie(0); LL a, b, n, l, t, m, r, suml; cin >> a >> b >> n; while(n--){ r = -1; cin >> l >> t >> m; suml = (2 * a + ((l - 1) - 1) * b) * (l - 1) / 2; LL x = l, y = (t - a) / b + 1 + 1, mid; // y = (t - a) / b + 1 + 1 多加个1不上整数除法的精度损失, 也可以直接用y = 2e6 while(x + 1 < y){ mid = (x + y) >> 1; if(check(a, b, suml, t, m, mid)) x = mid; else y = mid; } //cout << x << " " << y << endl; if(check(a, b, suml, t, m, y)) r = y; else if(check(a, b, suml, t, m, x)) r = x; else r = -1; if(n) cout << r << "\n"; else cout << r << endl; } #ifdef LOCAL cout << endl; } #endif // LOCAL return 0;}
Thank you!
------from ProLights
- Codeforces Round #299 (Div. 2) C. Tavas and Karafs 二分搜索+数列
- Codeforces Round #299 (Div. 2) Tavas and Karafs(二分)
- Codeforces Round #299 (Div. 2) C. Tavas and Karafs
- codeforces 299 div 2 (C Tavas and Karafs)
- Codeforces Round #299 (Div. 2)——C. Tavas and Karafs
- Codeforces Round #299 (Div. 1) A. Tavas and Karafs(二分)
- Codeforces 535C Tavas and Karafs(二分)
- Codeforces 535C Tavas and Karafs(二分)
- 二分 Tavas and Karafs:CodeForces 535C
- Codeforces 535C : Tavas and Karafs(二分)
- Codeforces 535C Tavas and Karafs(二分)
- Codeforces 535C Tavas and Karafs
- CodeForces 535C Tavas and Karafs
- CodeForces 535C Tavas and Karafs
- CodeForces 535C Tavas and Karafs
- Codeforces Round #299 (Div. 1)C. Tavas and Pashmaks
- codeforces535C:Tavas and Karafs(二分)
- codeforces535C:Tavas and Karafs(二分)
- 通信录作业,在链表中的结构体数组有点多此一举了
- Scala.js:将Scala编译为JavaScript的编译器
- 早上好!
- Linux下修改进程名称
- Django上传文件之iframe
- Codeforces Round #299 (Div. 2) C. Tavas and Karafs 二分搜索+数列
- 原生java http方式上传大文件(含 文件流分段上传问题、base64分段转码问题解决思路)
- Java中的垃圾回收机制
- XML
- 图片剪切导致的内存泄漏
- 技术公众号推荐
- Java对象初始化顺序
- android 代码混淆
- Django学习笔记之二