Codeforces Gym 100513F F. Ilya Muromets 水题
F. Ilya Muromets
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/gym/100513/problem/F
Description
I
Ilya Muromets is a legendary bogatyr. Right now he is struggling against Zmej Gorynych, a dragon with n heads numbered from 1 to nfrom left to right.
Making one sweep of sword Ilya Muromets can cut at most k contiguous heads of Zmej Gorynych. Thereafter heads collapse getting rid of empty space between heads. So in a moment before the second sweep all the heads form a contiguous sequence again.
As we all know, dragons can breathe fire. And so does Zmej Gorynych. Each his head has a firepower. The firepower of the i-th head isfi.
Ilya Muromets has time for at most two sword sweeps. The bogatyr wants to reduce dragon's firepower as much as possible. What is the maximum total firepower of heads which Ilya can cut with at most two sword sweeps?
Input
The first line contains a pair of integer numbers n and k (1 ≤ n, k ≤ 2·105) — the number of Gorynych's heads and the maximum number of heads Ilya can cut with a single sword sweep. The second line contains the sequence of integer numbers f1, f2, ..., fn(1 ≤ fi ≤ 2000), where fi is the firepower of the i-th head.
Output
Print the required maximum total head firepower that Ilya can cut.
Sample Input
8 2
1 3 3 1 2 3 11 1
Sample Output
20
HINT
题意
一个人可以砍两刀,每刀可以消去连续的K个数,然后问你两刀最多能砍下数的和是多少
题解:
这个我们枚举第一次砍的位置的左端点,并记为i,则贡献即为sum[i,i+k-1]+max[i+1,n];
其中sum[i,i+k-1]就是i到i+k-1的和,max[i+1,n]就是第一刀砍在i+1,n区间里所获得的最大值。
然后用个前缀和,再倒着扫一遍,求max数组就没了。
代码
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 #define N 200050 5 int n,k,kk,ans,a[N],s[N],mx[N]; 6 template<typename T>void read(T&x) 7 { 8 ll k=0; char c=getchar(); 9 x=0; 10 while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar(); 11 if (c==EOF)exit(0); 12 while(isdigit(c))x=x*10+c-'0',c=getchar(); 13 x=k?-x:x; 14 } 15 void read_char(char &c) 16 {while(!isalpha(c=getchar())&&c!=EOF);} 17 int main() 18 { 19 #ifndef ONLINE_JUDGE 20 freopen("aa.in","r",stdin); 21 #endif 22 read(n); read(k); 23 kk=min(n,2*k); 24 k=min(n,k); 25 for(int i=1;i<=n;i++)read(a[i]),s[i]=a[i]+s[i-1]; 26 for(int i=n;i>=1;i--) 27 { 28 int tp=s[min(n,i+k-1)]-s[i-1]; 29 mx[i]=max(mx[i+1],tp); 30 if (i+k-1<=n)ans=max(ans,tp+mx[i+k]); 31 } 32 printf("%d",ans); 33 }