D. Single-use Stones
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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 (1≤l< w≤105
) — the width of the river and the maximum length of a frog’s jump.
The second line contains w−1
integers a1,a2,…,aw−1 (0≤ai≤104), 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.
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: 0→3→6→9→10, 0→2→5→8→10, 0→1→4→7→10
.
#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define pb push_back
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define rep1(i,b,a) for(int i=b;i>=a;i--)
using namespace std;
const int N=1e5+100;
ll arr[N];
/*
我们用前缀和数组arr[i]表示石头总数
由于一个青蛙最远跳l
所以arr[i]-arr[i-l]的最小值就是在石头最少的路段可以通过的青蛙数
也就是枚举i并且更新最小值
*/
int main()
{
int l,w;
cin>>w>>l;
rep(i,1,w-1)
{
cin>>arr[i];
arr[i]+=arr[i-1];
}
ll mi=inf;
rep(i,l,w-1)
{
mi=min(mi,arr[i]-arr[i-l]);
}
cout<<mi<<endl;
return 0;
}
本文介绍了一个经典的编程问题——青蛙过河。青蛙需要借助河中不同位置的石头过河,每块石头只能使用一次。文章提供了算法思路及实现代码,通过前缀和与滑动窗口的方法求解最多有多少只青蛙能成功过河。
1335

被折叠的 条评论
为什么被折叠?



