2018/7/20 CF#476 D. Single-use Stones

本文介绍了一个有趣的问题:一群青蛙如何利用河中有限的石头数量成功过河。文章详细阐述了问题背景、输入输出格式,并提供了一种基于贪心思想的解决方案。

题目

A lot of frogs want to cross a river. A river is ww units width, but frogs can only jump ll units long, where l<w. Frogs can also jump on lengths shorter than ll. 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 aiai stones at the distance of ii 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?

输入格式

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≤10^4), where ai is the number of stones at the distance i from the bank the frogs are currently at.

输出格式

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

Examples

input

10 5
0 0 1 0 2 0 0 1 0

output

3

input

10 3 1 1 1 1 2 1 1 1 1

output

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 55, that does not help. The three paths are: 0→3→6→9→100→3→6→9→10, 0→2→5→8→100→2→5→8→10, 0→1→4→7→100→1→4→7→10.

题目大意:

青蛙过河,河的宽度为w,每只青蛙最长能跳l个距离,分别给出在距离河岸一边1,2,3···w-1处有ai块石头,青蛙可以在石头上落脚,当一只青蛙跳过这块石头时,石头就会沉下去,不能再给下一只青蛙用了,问最多有多少只青蛙能过河。

解题思路:

思维题,有点贪心意味,一开始想暴力模拟来着,估计是要爆hhh,这道题思路很奇特,就是遍历检测河面上所以长度为l(青蛙能跳的最长距离)里面有多少块石头,然后输出那个最小的数字;反过来想一想,那个最小区间上的所以石头,都分别能有一只青蛙踩上去,因为在别的区间的石头都比这个区间的石头多,而用完了这个区间的所有石头,青蛙是不是也就得在这个区间前面停下来了,代码很简单,简单动态规划然后遍历找最小就行,想出来很难。

#include <iostream>
#include<cstdio>
#include<algorithm>

using namespace std;

int riverstone[100005];

int main()
{
    int w, l;
    scanf("%d%d", &w, &l);
    int i;
    for(i = 0; i < w-1; i++)
    {
        scanf("%d", &riverstone[i]);
        if(i!=0)
            riverstone[i] += riverstone[i-1];
    }
    int minn = riverstone[l-1];
    for(i = l;i<w-1;i++)
    {
        minn = min(minn,riverstone[i]-riverstone[i-l]);
    }
    cout << minn << endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值