您的位置:首页 > 运维架构

USACO 2011 Open Gold 1.Mowing the Lawn 修剪草坪

2015-08-02 11:23 429 查看

Description

After winning the annual town competition for best lawn a year ago,

Farmer John has grown lazy; he has not mowed the lawn since then

and thus his lawn has become unruly. However, the competition is

once again coming soon, and FJ would like to get his lawn into

tiptop shape so that he can claim the title.

Unfortunately, FJ has realized that his lawn is so unkempt that he

will need to get some of his N (1 <= N <= 100,000) cows, who are

lined up in a row and conveniently numbered 1..N, to help him. Some

cows are more efficient than others at mowing the lawn; cow i has

efficiency E_i (0 <= E_i <= 1,000,000,000).

FJ has noticed that cows near each other in line often know each

other well; he has also discovered that if he chooses more than K

(1 <= K <= N) consecutive (adjacent) cows to help him, they will

ignore the lawn and start a party instead. Thus, FJ needs you to

assist him: determine the largest total cow efficiency FJ can obtain

without choosing more than K consecutive cows.

题意

在去年赢得了小镇的最佳草坪比赛后,约翰变得懒惰了,再也没有修剪过草坪了。现在,新一轮的比赛又开始了,约翰希望能再次夺冠。然而,约翰的草坪非常脏乱,因此,约翰需要让他的奶牛来完成这项工作。约翰有N头奶牛,平时排成一条直线,编号为1到N。每只奶牛的能力是不同的,第i头奶牛的能力为Ei。靠在一起的奶牛很熟悉,所以如果安排编号连续的K+1头奶牛在一起工作,她们就会密谋罢工 。因此,约翰需要你的帮助,如何挑选奶牛,才能使她们的工作能力之和最高,而且不会罢工呢?

Input

* Line 1: Two space-separated integers: N and K
* Lines 2..N+1: Line i+1 contains the single integer: E_i


Output

* Line 1: A single integer that is the best total efficiency FJ can obtain.


Sample Input

5 2
1
2
3
4
5


Sample Output

12


Key To Problem

这道题与烽火传递神似,把烽火传递的代码稍微修改就可以直接AC。


CODE

#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 100010
using namespace std;
typedef long long ll;
int n,m;
ll sum;
ll f
;
int que
;

int main()
{
scanf("%d%d",&n,&m);
m++;
int head=1,tail=0;
for(int i=1;i<=n;i++)
{
ll x;
scanf("%lld",&x);
sum+=x;
while(head<=tail&&f[que[tail]]>=f[i-1])
tail--;
que[++tail]=i-1;
while(que[head]<i-m)
head++;
f[i]=x+f[que[head]];
}
ll ans=1ll<<62;
for(int i=n;i>n-m;i--)
ans=min(ans,f[i]);
printf("%lld\n",sum-ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息