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个数,然后问你两刀最多能砍下数的和是多少
题解:
线段树,枚举这一块,然后除了这一块的其他块都是可以选择的
查询区间最大值就好了
代码
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
const int maxn=;
#define mod 1000000007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//**************************************************************************************
struct node
{
int l,r,ma,mi;
}a[maxn];
int d[maxn];
void build(int x,int l,int r)
{
a[x].l=l,a[x].r=r;
a[x].ma=-inf,a[x].mi=inf;
if(l==r)
{
a[x].ma=d[l];
a[x].mi=d[l];
return;
}
else
{
int mid=(l+r)>>;
build(x<<,l,mid);
build(x<<|,mid+,r);
a[x].ma=max(a[x<<].ma,a[x<<|].ma);
a[x].mi=min(a[x<<].mi,a[x<<|].mi);
}
}
int query1(int x,int st,int ed)
{
int l=a[x].l,r=a[x].r;
if(st<=l&&r<=ed)
return a[x].mi;
else
{
int mid=(l+r)>>;
int mi1=inf,mi2=inf;
if(st<=mid)
mi1=query1(x<<,st,ed);
if(ed>mid)
mi2=query1(x<<|,st,ed);
return min(mi1,mi2);
}
} int query2(int x,int st,int ed)
{
int l=a[x].l,r=a[x].r;
if(st<=l&&r<=ed)
return a[x].ma;
else
{
int mid=(l+r)>>;
int mi1=-inf,mi2=-inf;
if(st<=mid)
mi1=query2(x<<,st,ed);
if(ed>mid)
mi2=query2(x<<|,st,ed);
return max(mi1,mi2);
}
}
int aa[maxn];
int dp[maxn];
int sum=;
int main()
{
int n=read(),k=read();
for(int i=;i<=n;i++)
aa[i]=read();
for(int i=;i<=n;i++)
sum=sum+aa[i];
for(int i=;i<=k;i++)
dp[i]+=aa[i]+dp[i-];
for(int i=k+;i<=n;i++)
dp[i]=dp[i-]+aa[i]-aa[i-k];
if(*k>=n)
{
cout<<sum<<endl;
return ;
}
int ans=;
for(int i=;i<=n-k+;i++)
d[i]=dp[k-+i];
build(,,n-k+);
for(int i=;i<=n-k+;i++)
{
if(i>k)
{
ans=max(ans,d[i]+query2(,,i-k));
}
else
ans=max(ans,d[i]);
}
cout<<ans<<endl;
}