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>
 8 #include<set>
 9 #include<deque>
10 #include<stack>
11 #include<bitset>
12 #include<algorithm>
13 #include<functional>
14 #include<numeric>
15 #include<utility>
16 #include<sstream>
17 #include<iostream>
18 #include<iomanip>
19 #include<cstdio>
20 #include<cmath>
21 #include<cstdlib>
22 #include<cstring>
23 #include<ctime>
24 #define LL long long
25
26 using namespace std;
27 LL a, b, n;
28 LL l , t, m;
29 LL be;
30 int ok(LL k)
31 {
32     if(((be + (k-1) * b ) + be)*k/2 <= m * t && be + (k-1) * b <= t)
33         return 1;
34     return 0 ;
35 }
36 int solve(LL low ,LL high)
37 {
38     while(low <= high)
39     {
40         LL mid = (low + high)/2;
41         if(ok(mid))
42         {
43            low = mid + 1;
44         }else high = mid -1;
45     }
46     return high;
47 }
48 int main(){
49     scanf("%lld %lld %lld",&a,&b,&n);
50     for(LL i = 1;i <= n;i ++){
51         scanf("%lld %lld %lld",&l,&t,&m);
52         be = (l - 1) * b + a;
53         int k = solve(1,1e6);
54         if(k < 1)
55             printf("-1\n");
56         else printf("%lld\n",k+l-1);
57     }
58 return 0;
59 }

时间: 2024-08-06 11:43:11

Codeforces 535C Tavas and Karafs 数学归纳的相关文章

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

题目链接: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 —— 二分

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

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

Codeforces 536A Tavas and Karafs 二分+结论

题目链接:点击打开链接 题意:有一个等差数列,从A开始,公差为B 然后n个询问...每个询问给定l,t,m 然后要求如果每次可以最多选择m个数,使这m个数-1,那么在t次操作中可以使l为左端点的最长序列中使所有数为0 思路: 枚举答案判可行就好了. 有一个结论,就是只要最大的数<=t,那么一定存在某种方案使得:当和 <= t*m 时能把所有数消光. #include <stdio.h> #include <string.h> #include <string>

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

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

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,因为数列是递增的. 既然我们都已经知道了范围