3847: Mowing the Lawn (单调队列)

3847: Mowing the Lawn 分享至QQ空间

时间限制(普通/Java):1000MS/3000MS     内存限制:65536KByte
总提交: 97            测试通过:32

描述

 

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.

 

输入

 

* Line 1: Two space-separated integers: N and K

* Lines 2..N+1: Line i+1 contains the single integer: E_i
 

 

输出

 

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

 

样例输入

 

样例输出

 

提示

INPUT DETAILS:

FJ has 5 cows whose efficiencies are 1, 2, 3, 4, and 5, in that order. He wants to choose some of the cows such that their total efficiency is maximized, but he cannot choose more than 2 consecutive cows.
 

OUTPUT DETAILS:

FJ chooses all cows but the third. The total efficiency of the cows is thus 1 + 2 + 4 + 5 = 12.

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 typedef long long ll;
 7 
 8 ll n,k;
 9 const int N=100005;
10 ll dp[N],que[N];     //第i个放烟火 前i个的最小值
11 ll minn=0x3f3f3f3f3f3f3f3f;
12 
13 int main(){
14     ios::sync_with_stdio(false);
15     ll frontt=1,rear=1;
16     que[1]=0;
17     ll d;
18     ll sum=0;
19     cin>>n>>k;
20     for(int i=1;i<=n;i++){
21         cin>>d;
22         sum+=d;
23         while(frontt<=rear&&i-que[frontt]>k+1) frontt++;   //超过了就出队
24         dp[i]=d+dp[que[frontt]];  //维护单调递增 保持最小
25         while(frontt<=rear&&dp[i]<=dp[que[rear]]) rear--;   //维护单调递增
26         que[++rear]=i;
27     }
28     for(int i=n;i>=n-k;i--) minn=min(minn,dp[i]);
29     cout << sum-minn << endl;
30     return 0;
31 }
View Code

 

posted @ 2019-08-12 10:35  厂长在线养猪  Views(330)  Comments(0Edit  收藏  举报