Present - CODEFORCES, 460c 二分 扫描线求和

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 nm 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
Note

In the first sample beaver can water the last 3 flowers at the first day. On the next day he may not to water flowers at all. In the end he will get the following heights: [2, 2, 2, 3, 2, 2]. The smallest flower has height equal to 2. It's impossible to get height 3 in this test.


题目意思:有n朵花,每朵花都有一个输入的初始高度,还剩下m天,每天可以给连续w朵花浇水,每天只能浇一次,每浇一次花长一个单位长度,求m天所有花中最矮的花最大高度。

解题思路:二分最后高度,对于每一个高度判断所有花是否都能满足。判断时从左往右依次判断,如果每次给连续的w朵花都加1,效率为n*w = 10^10会超时。用一个temp变量表示当前花朵已经被浇了多少次,用一个数组(初值都为0)表示temp中有多少次对它不起作用。



假设此时二分高度为 h=6, 每朵花初始高度依次如图所标。首先给第一朵花浇水,当前高度为2,还需要浇4天,temp = temp + 4,temp=4,但这四天只能对1,2,3起作用,对4不起作用,所以c[4] = -4,表示前面累积天数有四天对花4不起作用。给第二朵花浇水时,temp=4,已经浇了四天,而他只需要两天所以不用再浇,3也一样,c[2+w]=c[5]=0, c[3+w] = c[6]=0, 到4时temp = 4, 先消除前面影响,temp += c[4], temp = 0. 所以还需再浇4天,temp = 4,依次往后推。。。最后看总共浇水天数与剩下天数相比较 判定该高度是否可行。

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

#define maxn 100000+10
#define INF 0x3fffffff

/
int n,m,w;
int a[maxn];
/
bool ok(int k){
    int temp = 0,b[maxn],q,cnt=0;
    memset(b,0,sizeof(b));
    for(int i=0;i<n;i++){
        q = k - a[i];
        temp += b[i];
        if(q>temp){
            cnt += q - temp;
            if(cnt>m) return false;
            if(i+w<n) b[i+w] = -(q-temp);
            temp = q;
        }
    }
//printf("k: %d  cnt:  %d\n",k,cnt);
    return cnt <=m;
}
int main(){
    cin >> n >> m >>w;
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    int l = 0, r = INF, mid;
    while(l<r){
        mid = (l+r) >> 1;
        if(ok(mid)) l = mid+1;
        else r = mid;
    }
    cout << r - 1 <<endl;
 }



这是一道 CodeForces 上的题目,题目编号为 749C,题目名称为 Voting。 题目描述: 有 $n$ 个人参加选举,选出一位领导人。每个人都会投票,你知道了每个人选择谁,并且可以知道选票中作废票和弃权票的数量。如果有一个人获得了半数以上的有效选票(即除去作废票和弃权票的票数),那么他将成为领导人。如果没有任何一个人获得半数以上的有效选票,则选举无效。 现在你可以修改任意数量的作废票或弃权票,使选举有效,并使你支持的候选人成为领导人。你需要最少的修改次数。 输入格式: 第一行包含三个整数 $n, a, b$,分别表示选民的数量,作废票的数量和弃权票的数量。 接下来 $n$ 行,每行包含一个字符串,表示每个人的投票情况。如果该字符串为 “YES” 或 “NO”,表示该选民对应的是弃权票;如果该字符串为 “POLL”, 表示该选民对应的是作废票;如果该字符串为其他字符串,则表示该选民对应的是有效选票,该字符串为该选民的投票对象。 输出格式: 如果无法通过修改使选举有效,则输出 -1;否则输出最少的修改次数,使得选举有效,并且你支持的候选人成为领导人。 数据范围: $1 \leq n \leq 2000, 0 \leq a, b \leq n$ 输入样例 #1: ``` 7 2 1 YES NO POLL YES YES YES NO ``` 输出样例 #1: ``` 1 ``` 样例 #1 解释: 总共有7个选民,其中弃权票有3个,作废票有2个,有效选票有2个。由于选民投票意见分散,无法确定一位领导人,因此选举无效。 我们可以将2张作废票修改为支持你所支持的候选人,这样你所支持的候选人将获得3张有效选票,超过半数,成为领导人。因此修改次数为1。 输入样例 #2: ``` 3 1 0 YES NO YES ``` 输出样例 #2: ``` 0 ``` 样例 #2 解释: 总共有3个选民,其中弃权票有1个,作废票有0个,有效选票有2个。你所支持的候选人获得了2张有效选票,超过半数,成为领导人。由于选民投票意见不分散,选举有效,无需修改任何票。因此修改次数为0。 算法1: (模拟) $O(n)$ 首先计算出除作废票和弃权票之外的票数,如果有一人的得票率超过50%,则选举有效,直接输出0。 否则,需要计算出至少需要修改多少张作废票或弃权票,才能使选举有效,并且支持你所支持的候选人成为领导人。 具体来说,我们可以考虑枚举需要修改的作废票和弃权票的数量,假设需要修改 $i$ 张作废票和 $j$ 张弃权票,使得选举有效。那么,你所支持的候选人需要得到至少 $\lceil \frac{n}{2} \rceil$ 张有效选票。我们可以通过统计当前你所支持的候选人得到的有效选票数 $cnt$,以及当前已经修改的作废票和弃权票的数量 $a'$ 和 $b'$,来判断是否存在一组解 $(i, j)$,使得选举有效。 具体来说,如果当前得票数 $cnt + i \ge \lceil \frac{n}{2} \rceil$,那么选举有效,输出 $i+j$ 即可。如果 $(\lceil \frac{n}{2} \rceil - cnt) \le a' + i \le n-b'-j$,那么也存在一组解 $(i,j)$,使得选举有效,输出 $i+j$ 即可。 如果枚举完所有情况,都无法使选举有效,那么输出 -1。 时间复杂度:$O(n^2)$ C++ 代码
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值