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>
#include <math.h>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
template <class T>
inline bool rd(T &ret) {
    char c; int sgn;
    if (c = getchar(), c == EOF) return 0;
    while (c != '-' && (c<'0' || c>'9')) c = getchar();
    sgn = (c == '-') ? -1 : 1;
    ret = (c == '-') ? 0 : (c - '0');
    while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return 1;
}
template <class T>
inline void pt(T x) {
    if (x <0) {
        putchar('-');
        x = -x;
    }
    if (x>9) pt(x / 10);
    putchar(x % 10 + '0');
}
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
int n;
ll A, B;
ll l, t, m, mx;
ll siz(ll x){
    return A + (x - 1)*B;
}
ll work(){
    if (siz(l) - t > 0)return -1;

    ll L = l, R = mx, ans = l;
    while (L <= R){
        ll mid = (L + R) >> 1;
        ll len = mid - l + 1;
        ll all = (siz(l) + siz(mid))*len / 2;
        ll M = min(m, len);
        if (all > t*M || siz(mid)>t)
            R = mid - 1;
        else {
            ans = mid;
            L = mid + 1;
        }
    }
    return ans;
}
int main() {
    while (cin >> A >> B >> n){
        mx = (1e6 - A) / B;
        mx++;
        while (n--){
            rd(l); rd(t); rd(m);
            pt(work()); puts("");
        }
    }
    return 0;
}
时间: 2024-08-28 18:47:44

Codeforces 536A 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    那么就可

Tavas and Karafs 二分+结论

二分比较容易想到 #include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<vector> #include<cstdio> #include<cassert> #include<iomanip> #include<cstdlib> #include<cstring>

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.

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

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

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 数学归纳

题意:给你一个等差数列.每一次选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

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