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

题目传送门

 1 /*
 2     题意:给定一个数列,求最大的r使得[l,r]的数字能在t次全变为0,每一次可以在m的长度内减1
 3     二分搜索:搜索r,求出sum <= t * m的最大的r
 4     详细解释:http://blog.csdn.net/libin56842/article/details/45082747
 5 */
 6 #include <cstdio>
 7 #include <algorithm>
 8 #include <cstring>
 9 #include <cmath>
10 using namespace std;
11
12 typedef long long ll;
13
14 const int MAXN = 1e4 + 10;
15 const int INF = 0x3f3f3f3f;
16 ll A, B, n, l, t, m;
17
18 ll cal(ll x)    {return A + (x - 1) * B;}
19
20 ll get_sum(ll l, ll r)
21 {
22     return (cal (l) + cal (r)) * (r - l + 1) / 2;
23 }
24
25 int main(void)        //Codeforces Round #299 (Div. 2) C. Tavas and Karafs
26 {
27     while (scanf ("%I64d%I64d%I64d", &A, &B, &n) == 3)
28     {
29         for (int i=1; i<=n; ++i)
30         {
31             scanf ("%I64d%I64d%I64d", &l, &t, &m);
32             if (cal (l) > t)    {puts ("-1");    continue;}
33             ll x = l;    ll r = (t - A) / B + 1;
34             while (x <= r)
35             {
36                 ll mid = (x + r) / 2;
37                 if (get_sum (l, mid) <= t * m)    x = mid + 1;
38                 else    r = mid - 1;
39             }
40             printf ("%d\n", x - 1);
41         }
42
43     }
44
45     return 0;
46 }
时间: 2024-12-25 05:08:20

二分搜索 Codeforces Round #299 (Div. 2) C. Tavas and Karafs的相关文章

DFS Codeforces Round #299 (Div. 2) C. Tavas and Karafs

题目传送门 1 /* 2 DFS:按照长度来DFS,最后排序 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <iostream> 8 #include <cmath> 9 #include <vector> 10 using namespace std; 11 12 const int MAXN = 1e3 + 10; 1

水题 Codeforces Round #299 (Div. 2) A. Tavas and Nafas

题目传送门 1 /* 2 很简单的水题,晚上累了,刷刷水题开心一下:) 3 */ 4 #include <bits/stdc++.h> 5 using namespace std; 6 7 char s1[11][10] = {"zero", "one", "two", "three", "four", "five", "six", "seven

Codeforces Round #299 (Div. 1)C. Tavas and Pashmaks (凸壳)

C. Tavas and Pashmaks Tavas is a cheerleader in the new sports competition named "Pashmaks". This competition consists of two part: swimming and then running. People will immediately start running R meters after they finished swimming exactly S 

Codeforces Round #299 (Div. 2) A. Tavas and Nafas

题目链接:http://codeforces.com/problemset/problem/535/A #include <iostream> #include <string> using namespace std; int main() { string s1[10]={"zero","one","two","three","four","five",&qu

Codeforces Round #299 (Div. 2)D. Tavas and Malekas

KMP,先预处理按每个节点标记,扫一遍更新每个匹配位置,最后kmp判断是否有重合而且不相同的地方 注意处理细节,很容易runtime error #include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<vector> #include<cstdio> #include<cassert> #inclu

二分搜索 Codeforces Round #218 (Div. 2) C. Hamburgers

题目传送门 1 /* 2 题意:一个汉堡制作由字符串得出,自己有一些原材料,还有钱可以去商店购买原材料,问最多能做几个汉堡 3 二分:二分汉堡个数,判断此时所花费的钱是否在规定以内 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cmath> 8 using namespace std; 9 10 typedef long long ll; 11 const int MAXN = 1e2 + 10;

Codeforces Round #299 (Div. 2) Tavas and Karafs(二分)

英语渣渣感觉无力...翻了下题解才知道题意... 给你一个无限长的等差数列a,n个查询.对于每个查询,给出l,t,m,求最大的r.表示在等差数列a的第l号元素到第r号元素,执行t次操作,对于每一次操作,在[l,r]区间内选出m个数,并且把这m个数-1,执行完t次操作后,使得区间[l,r]内的数全部为0,求区间最大的右端点r. 如果t小于a[l],那么执行完t次操作a[l]也不会为0,输出-1 那么同理我们可以求出r的范围,r肯定小于等于(t-a)/b,因为数列是递增的. 既然我们都已经知道了范围

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/

*Codeforces Round #251 (Div. 2)AK)

A.确定性数学计算,水,读题要快 1 #include<iostream> 2 #include<stdio.h> 3 4 using namespace std; 5 int N,d; 6 int main(){ 7 while(~scanf("%d%d",&N,&d)){ 8 int cnt=0; 9 for(int i=0;i<N;i++){ 10 int t; 11 scanf("%d",&t); 12