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; }
相关文章推荐
- C++动态规划之最长公子序列实例
- C++动态规划之背包问题解决方法
- 浅谈单调队列、单调栈
- C#使用动态规划解决0-1背包问题实例分析
- 动态规划
- C++ 动态规划
- DP(动态规划) 解游轮费用问题
- 动态规划的用法——01背包问题
- 动态规划的用法——01背包问题
- 《收集苹果》 动态规划入门
- 《DNA比对》蓝桥杯复赛试题
- 《背包问题》 动态规划
- 关于爬楼梯的动态规划算法
- 动态规划 --- hdu 1003 **
- DP问题各种模型的状态转移方程
- 0-1背包解题过程
- USACO 3.2.2:Stringsobits
- 字符串编辑距离
- HDU ACM Step 2.2.2 Joseph(约瑟夫环问题)
- ACM常用算法