CF 535c Tavas and Karafs

Tavas and Karafs

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

Submit Status Practice CodeForces 535C

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 41 5 33 3 107 10 26 4 8

Output

4-18-1

Input

1 5 21 5 102 7 4

Output

12

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

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

然后用二分来做

网上的代码

 1 #include<queue>
 2 #include<math.h>
 3 #include<stdio.h>
 4 #include<string.h>
 5 #include<iostream>
 6 #include<algorithm>
 7 using namespace std;
 8 #define N 12345678
 9 #define M 1234
10
11 long long a,b,n,l,t,m;
12
13 int main()
14 {
15     while(~scanf("%lld%lld%lld",&a,&b,&n))
16     {
17         for(int i=0;i<n;i++)
18         {
19             scanf("%lld%lld%lld",&l,&t,&m);
20             if(a+(l-1)*b>t)
21             {
22                 puts("-1");
23                 continue;
24             }
25             long long ll=l,r=(t-a)/b+1,mid;
26
27             while(ll<=r)
28             {
29                 mid=(ll+r)/2;
30                 if( (2*a+(mid+l-2)*b)*(mid-l+1)/2  <=t*m)
31                 {
32                     ll=mid+1;
33                 }
34                 else
35                 {
36                     r=mid-1;
37                 }
38             }
39             cout<<ll-1<<endl;
40         }
41     }
42
43     return 0;
44 }

我的代码

 1 #include<queue>
 2 #include<math.h>
 3 #include<stdio.h>
 4 #include<string.h>
 5 #include<iostream>
 6 #include<algorithm>
 7 using namespace std;
 8 #define N 12345678
 9 #define M 1234
10
11 long long a,b,n,l,t,m;
12
13 int main()
14 {
15     while(~scanf("%lld%lld%lld",&a,&b,&n))
16     {
17         for(int i=0;i<n;i++)
18         {
19             scanf("%lld%lld%lld",&l,&t,&m);
20             if(a+(l-1)*b>t)
21             {
22                 puts("-1");
23                 continue;
24             }
25             long long ll=l,r=(t-a)/b+1,mid;
26
27             while(ll<=r)
28             {
29                 mid=(ll+r)/2;
30                 if( (2*a+(mid+l-2)*b)*(mid-l+1)/2  <=t*m
31                     &&  (2*a+(mid+1+l-2)*b)*(mid+1-l+1)/2  >t*m)
32                                         //这里wa了一次,  把 > 写成了 >= ,
33                 {
34                     break;
35                 }
36                 else if( (2*a+(mid+l-2)*b)*(mid-l+1)/2  <=t*m)
37                 {
38                     ll=mid+1;
39                 }
40                 else
41                 {
42                     r=mid-1;
43                 }
44             }
45             cout<<mid<<endl;
46         }
47     }
48
49     return 0;
50 }
时间: 2024-10-29 04:46:45

CF 535c Tavas and Karafs的相关文章

Codeforces 535C Tavas and Karafs

题目链接:CF - 535C 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

Codeforces 535C Tavas and Karafs(二分)

题意  有一个等差数列  从A开始  公差为B  然后n个询问  每个询问给定l,t,m   然后要求如果每次可以最多选择m个数   使这m个数-1   那么在t次操作中可以使l为左端点的最长序列中使所有数为0  输出这个最长序列的右端序号 定理  序列h1,h2,...,hn 可以在t次时间内(每次至多让m个元素减少1)  全部减小为0  当且仅当 max(h1, h2, ..., hn) <= t  &&  h1 + h2 + ... + hn <= m*t    那么就可

CodeForces 535C Tavas and Karafs —— 二分

题意:给出一个无限长度的等差数列(递增),每次可以让从l开始的m个减少1,如果某个位置已经是0了,那么可以顺延到下一位减少1,这样的操作最多t次,问t次操作以后从l开始的最长0序列的最大右边界r是多少. 分析:由题意可以挖掘出两个条件:l~r中最大的值(因为是递增的,即r的值)必定不大于t:同时,t*m要大于或等于这一段的和.那么根据这两个条件进行二分即可. 细节:二分的右端点inf不能设置的太大,否则第一次的mid可能就会爆long long. 代码如下: 1 #include <stdio.

Codeforces 535C Tavas and Karafs 数学归纳

题意:给你一个等差数列.每一次选m个不同的数使得都减一,问你最多能承受t次的右端点是多少(左端点已给)要使得这个右端点最右. 解题思路:显然是二分,但是不知道如何判断.官方题解. 解题代码: 1 // File Name: c.cpp 2 // Author: darkdream 3 // Created Time: 2015年04月15日 星期三 01时04分48秒 4 5 #include<vector> 6 #include<list> 7 #include<map&g

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

C. Tavas and Karafs 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <string> 7 #include <vector> 8 #include <set> 9 #include <map> 10

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) 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 #includ

codeforces535C:Tavas and Karafs(二分)

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-b

cf#299 Tavas and Malekas

传送门在这里 题意:给出一个字符串的一个子串,告诉你子串在某些位置和原串匹配,求一共有多少可能的原串 思路:其实就是要判断给出子串的所有前缀和后缀哪些是相等的. 首先kmp的next数组求的是所有前缀子串(a1  a1a2  a1a2a3...)中长度最大的前缀和后缀的长度, 设字符串长度为len,那么next[len]得到的是整个字符串中长度最长的相等的前缀和后缀,并且可知,所有起始位置小于next[len]的后缀都不可能存在相等的前缀了,那么下一步就是求起始位置在[next[len]+1,l