CF #262 (DIV2) C . Present (二分答案)

output

standard output

Little beaver is a beginner programmer, so informatics is his favorite subject. Soon his informatics teacher is going to have a birthday and the beaver has decided to prepare a present for her. He planted
n flowers in a row on his windowsill and started waiting for them to grow. However, after some time the beaver noticed that the flowers stopped growing. The beaver thinks it is bad manners to present little flowers. So
he decided to come up with some solutions.

There are m days left to the birthday. The height of the
i-th flower (assume that the flowers in the row are numbered from
1 to n from left to right) is equal to
ai at the moment. At each of the remaining
m days the beaver can take a special watering and water
w contiguous flowers (he can do that only once at a day). At that each watered flower grows by one height unit on that day. The beaver wants the height of the smallest flower be as large as possible in the end. What maximum
height of the smallest flower can he get?

Input

The first line contains space-separated integers n,
m and w
(1?≤?w?≤?n?≤?105; 1?≤?m?≤?105). The second line contains space-separated
integers a1,?a2,?...,?an
(1?≤?ai?≤?109).

Output

Print a single integer — the maximum final height of the smallest flower.

Sample test(s)

Input

6 2 3
2 2 2 2 1 1

Output

2

Input

2 5 1
5 8

Output

9

二分最终的结果,然后判断是否合法。即在当前的最终最大的高度下,是否m次可以达到。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <set>
#include <stack>
#include <cctype>
#include <algorithm>
#define lson o<<1, l, m
#define rson o<<1|1, m+1, r
using namespace std;
typedef long long LL;
const int mod = 99999997;
const int MAX = 2000000000;
const int maxn = 300005;
LL n, m, w;
LL sum[maxn], a[maxn];
bool ok(LL mi) {
    memset(sum, 0, sizeof(sum));
    LL cur, cnt = 0;
    for(int i = 0; i < n; i++) {
        if(i > 0) sum[i] += sum[i-1];
        cur = sum[i] + a[i];
        if(cur < mi) {
            cnt += mi - cur;
            sum[i] += mi - cur;
            sum[i+w] -= mi - cur;
        }
    }
    if(cnt > m) return true;
    return false;
}
int main()
{
    cin >> n >> m >> w;
    for(int i = 0; i < n; i++) cin >> a[i];
    LL l = 0, r = MAX;
    while(r - l > 1) {
        LL mid = (l+r) >> 1;
        if(ok(mid)) r = mid;
        else l = mid;
    }
    cout << l << endl;
    return 0;
}



CF #262 (DIV2) C . Present (二分答案)

时间: 2024-10-12 04:34:38

CF #262 (DIV2) C . Present (二分答案)的相关文章

Codeforces Round #262 (Div. 2)C(二分答案,延迟标记)

这是最大化最小值的一类问题,这类问题通常用二分法枚举答案就行了. 二分答案时,先确定答案肯定在哪个区间内.然后二分判断,关键在于怎么判断每次枚举的这个答案行不行. 我是用a[i]数组表示初始时花的高度,b[i]表示要达到当前枚举的答案(即mid的值)需要这朵花再涨多少.这两个数组很好算,关键是一次浇连续的w朵花,如何更新区间(暴力的O(n2)的去更新就超时了)?可以用线段树,但是这道题没有涉及区间查询,就是在一个数组上更新区间,用线段树未免小题大做.那么其实这种更新就用延迟标记的思想(懒操作)就

codeforces CF85E Guard Towers 二分答案 二分图判定

$ \rightarrow $ 戳我进CF原题 E. Guard Towers time limit per test: 1.5 seconds memory limit per test: 256 megabytes input: standard input outputstandard output In a far away kingdom lives a very greedy king. To defend his land, he built $ n $ guard towers.

Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案

There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebo

cf#261 div2 解题报告

.....代码没什么可说的,主要是学习各路大神姿势 A题 化简化简水题,都告诉平行坐标轴了,数据还出了对角线,后面两个点坐标给的范围也不错 ........和最优代码相比姿势有点混乱 #include <cstdio> int x[4],y[4]; int abs(int n){ return n<0?-n:n; } int main(){ scanf("%d%d%d%d",x,y,x+1,y+1); int dx=abs(x[0]-x[1]); int dy=abs

【CodeForces954G】Castle Defense(二分答案+差分)

Description 题目链接 Solution 二分答案,套一个差分标记即可 每次放弓箭手显然越右边越优 Code #include <cstdio> #include <algorithm> #include <cstring> #define N 2000010 #define ll long long using namespace std; int n,R; ll A[N],k,Ans,l=1e18,r=1e20,cf[N],sum[N]; inline l

Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store

HDU3081Marriage Match II(二分答案+并查集+最大流SAP)经典

Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2507    Accepted Submission(s): 856 Problem Description Presumably, you all have known the question of stable marriage match. A

Codeforce 371C Hamburgers (二分答案)

题目链接 Hamburgers 二分答案,贪心判断即可. 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define REP(i,n) for(int i(0); i < (n); ++i) 6 #define LL long long 7 8 char str[1010]; 9 LL len; 10 LL b, c, s, nb, nc, ns, pb, pc, ps; 11 LL money; 12 13 bool

POJ 3080 Blue Jeans(后缀数组+二分答案)

[题目链接] http://poj.org/problem?id=3080 [题目大意] 求k个串的最长公共子串,如果存在多个则输出字典序最小,如果长度小于3则判断查找失败. [题解] 将所有字符串通过拼接符拼成一个串,做一遍后缀数组,二分答案,对于二分所得值,将h数组大于这个值的相邻元素分为一组,判断组内元素是否覆盖全字典,是则答案成立,对于答案扫描sa,输出第一个扫描到的子串即可. [代码] #include <cstdio> #include <cstring> #inclu