Codeforces 965D

题目链接:http://codeforces.com/contest/965/problem/D

A lot of frogs want to cross a river. A river is w units width, but frogs can only jump l units long, where l<w. Frogs can also jump on lengths shorter than l

. but can't jump longer. Hopefully, there are some stones in the river to help them.

The stones are located at integer distances from the banks. There are ai

stones at the distance of i

units from the bank the frogs are currently at. Each stone can only be used once by one frog, after that it drowns in the water.

What is the maximum number of frogs that can cross the river, given that then can only jump on the stones?

Input

The first line contains two integers w

and l ( 1l<w105

) — the width of the river and the maximum length of a frog's jump.

The second line contains w1

integers a1,a2,,aw1 ( 0ai104), where ai is the number of stones at the distance i

from the bank the frogs are currently at.

Output

Print a single integer — the maximum number of frogs that can cross the river.

扫描二维码关注公众号,回复: 150583 查看本文章
Examples
Input
Copy
10 5
0 0 1 0 2 0 0 1 0
Output
Copy
3
Input
Copy
10 3
1 1 1 1 2 1 1 1 1
Output
Copy
3
Note

In the first sample two frogs can use the different stones at the distance 5

, and one frog can use the stones at the distances 3 and then 8

.

In the second sample although there are two stones at the distance 5

, that does not help. The three paths are: 036910, 025810, 014710

.


题解:方法一: 二分答案, 然后利用前缀和chec

         方法二: 最大流最小割定理, 一定是删除连续的某段这是最小割。求出最小割即为答案。

 
 
#include <bits/stdc++.h>

using namespace std;
using ll = long long ;
const int N = 1E5 + 7;
int pre[N];
int w, l;
int sum(int l, int r)
{
    return pre[r]-pre[l-1];
}
bool ck(int k)
{
    for(int i = 1;i + l - 1 < w;i ++){
        if(sum(i, i + l - 1) < k) return 0;
    }
    return 1;
}
int main()
{
    scanf("%d %d", &w, &l);
    for(int i = 1;i < w;i ++) {
        scanf("%d", &pre[i]);
        pre[i] += pre[i-1];
    }
    int L = 0, R = pre[w-1];
    while(L <= R) {
        int mid = (L + R) / 2;
        if(ck(mid)) L = mid + 1;
        else R = mid - 1;
    }
    printf("%d\n", R);
    return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_36876305/article/details/80101691