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个数,然后问你两刀最多能砍下数的和是多少
题解:
线段树,枚举这一块,然后除了这一块的其他块都是可以选择的
查询区间最大值就好了
代码
#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;
}
Codeforces Gym 100513F F. Ilya Muromets 线段树的更多相关文章
- Codeforces Gym 100513F F. Ilya Muromets 水题
F. Ilya Muromets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/probl ...
- [Codeforces 266E]More Queries to Array...(线段树+二项式定理)
[Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...
- [Codeforces 280D]k-Maximum Subsequence Sum(线段树)
[Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...
- codeforces 1217E E. Sum Queries? (线段树
codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...
- Codeforces Round #530 (Div. 2) F (树形dp+线段树)
F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...
- Educational Codeforces Round 47 (Rated for Div. 2)F. Dominant Indices 线段树合并
题意:有一棵树,对于每个点求子树中离他深度最多的深度是多少, 题解:线段树合并快如闪电,每个节点开一个权值线段树,递归时合并即可,然后维护区间最多的是哪个权值,到x的深度就是到根的深度减去x到根的深度 ...
- 【Codeforces】Gym 101608G WiFi Password 二分+线段树
题意 给定$n$个数,求有最长的区间长度使得区间内数的按位或小于等于给定$v$ 二分区间长度,线段树处理出区间或,对每一位区间判断 时间复杂度$O(n\log n \log n)$ 代码 #inclu ...
- Codeforces 1045. A. Last chance(网络流 + 线段树优化建边)
题意 给你 \(n\) 个武器,\(m\) 个敌人,问你最多消灭多少个敌人,并输出方案. 总共有三种武器. SQL 火箭 - 能消灭给你集合中的一个敌人 \(\sum |S| \le 100000\) ...
- Codeforces 446C DZY Loves Fibonacci Numbers [线段树,数论]
洛谷 Codeforces 思路 这题知道结论就是水题,不知道就是神仙题-- 斐波那契数有这样一个性质:\(f_{n+m}=f_{n+1}f_m+f_{n}f_{m-1}\). 至于怎么证明嘛-- 即 ...
随机推荐
- 获取某月第一天,最后一天的sql server脚本 【转】http://blog.csdn.net/chaoowang/article/details/9167969
这是计算一个月第一天的SQL 脚本: SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) --当月的第一天 SELECT DATEADD(mm, DA ...
- SQL Server 2005的XML数据修改语言(XML DML)
转:http://www.microsoft.com/china/msdn/library/data/sqlserver/XMLDML.mspx?mfr=true 作为对XQuery语言的扩展,XML ...
- Linux User's Manual IOSTAT
IOSTAT(1) Linux User's Manual IOSTAT(1) NAME iostat - Report Central Processing Unit (CPU) statistic ...
- 位图引起的内存溢出OutOfMemory解决方案
一.问题描述:Android下的相机在独自使用时,拍照没有问题,通过我们的代码调用时,也正常,但是更换了不同厂商的平板,ROM由Android4.0变成了Android4.1后,拍照出现了OutOfM ...
- python中的类型转换
函数 描述 int(x [,base ]) 将x转换为一个整数 long(x [,base ]) 将x转换为一个长整数 float(x ) 将x转换到一个浮点数 complex(real [,imag ...
- Designing Evolvable Web API with ASP.NET 随便读,随便记 “The Internet,the World Wide Web,and HTTP”——HTTP
HTTP 我们将只聚焦在于与创建 Web APIs有关的部分. HTTP 是信息系统中的一个应用层协议,是Web的支柱. 其原先由 Berners-Lee, Roy Fielding 和 Henrik ...
- 《Genesis-3D开源游戏引擎完整实例教程-跑酷游戏篇:简介及目录》(附上完整工程文件)
跑酷游戏制作 游戏类型: 此游戏Demo,为跑酷类游戏. 框架简介: 游戏通常由程序代码和资源组成.如果说模型.贴图.声音之类的可以给游戏环境提供一个物理描述和设置,那么脚本和代码块会给游戏赋予生命, ...
- vim对erlang语法支持
发现vim写erlang代码语法缩进都不对,后来发现vim是7.0的,vim7.3开始才对erlang这块进行了支持,所以升级vim git上下载源码包,然后一系列配置安装 http://www.2c ...
- 解决问题的步骤(第一篇)-- clwu
现象: 之前打开IE 还是正常的,但前几天开始打开就不正常了,报错如下. 处理(别人的)问题的步骤: 百度一下 0xc0000018,没有什么有用信息. 看一下程序(IE)启动时做了些什么. 怎么看 ...
- APACHE如何里一个站点绑定多个域名?用ServerAlias
APACHE2如何里一个站点绑定多个域名?用ServerAlias以前很笨,要使多个域名指向同一站点总是这样写: <VirtualHost *:80>ServerAdmin i@kuigg ...