Codeforces Gym 100513F F. Ilya Muromets 线段树

时间:2022-09-30 12:11:34

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 线段树的更多相关文章

  1. Codeforces Gym 100513F F&period; Ilya Muromets 水题

    F. Ilya Muromets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/probl ...

  2. &lbrack;Codeforces 266E&rsqb;More Queries to Array&period;&period;&period;&lpar;线段树&plus;二项式定理&rpar;

    [Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...

  3. &lbrack;Codeforces 280D&rsqb;k-Maximum Subsequence Sum(线段树&rpar;

    [Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...

  4. codeforces 1217E E&period; Sum Queries&quest; (线段树

    codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...

  5. Codeforces Round &num;530 &lpar;Div&period; 2&rpar; F &lpar;树形dp&plus;线段树)

    F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...

  6. Educational Codeforces Round 47 &lpar;Rated for Div&period; 2&rpar;F&period; Dominant Indices 线段树合并

    题意:有一棵树,对于每个点求子树中离他深度最多的深度是多少, 题解:线段树合并快如闪电,每个节点开一个权值线段树,递归时合并即可,然后维护区间最多的是哪个权值,到x的深度就是到根的深度减去x到根的深度 ...

  7. 【Codeforces】Gym 101608G WiFi Password 二分&plus;线段树

    题意 给定$n$个数,求有最长的区间长度使得区间内数的按位或小于等于给定$v$ 二分区间长度,线段树处理出区间或,对每一位区间判断 时间复杂度$O(n\log n \log n)$ 代码 #inclu ...

  8. Codeforces 1045&period; A&period; Last chance&lpar;网络流 &plus; 线段树优化建边&rpar;

    题意 给你 \(n\) 个武器,\(m\) 个敌人,问你最多消灭多少个敌人,并输出方案. 总共有三种武器. SQL 火箭 - 能消灭给你集合中的一个敌人 \(\sum |S| \le 100000\) ...

  9. Codeforces 446C DZY Loves Fibonacci Numbers &lbrack;线段树,数论&rsqb;

    洛谷 Codeforces 思路 这题知道结论就是水题,不知道就是神仙题-- 斐波那契数有这样一个性质:\(f_{n+m}=f_{n+1}f_m+f_{n}f_{m-1}\). 至于怎么证明嘛-- 即 ...

随机推荐

  1. Atitti &&num;160&semi;onvif 设备发现与原理

    Atitti  onvif 设备发现与原理 1.1. ,有以下几个步骤:1 1.2. 设备搜索原理及编程技巧:2 1.3. Ws disconvert 的组播地址和端口就是37022 1)发现ipca ...

  2. url中文参数解决方案

    首先,弄清楚为什么url传递中文会转码或者乱码,以及http头 contentType="text/html; charset=GBK" 的作用. html代码会经过web服务器, ...

  3. HDU 5305 Friends &lpar;DFS,穷举&plus;剪枝&rpar;

    题意: 给定n个人,m对朋友关系,如果对于每个人,只能刚好选择其所有朋友中的一半的人进行聊天(只是我和我的朋友,不是我的朋友和我的朋友),那么有多少种情况?只要一个选择不同,视为不同情况. 思路: 比 ...

  4. listagg&lpar; &rpar; within group &lpar; order by &rpar; 与 wm&lowbar;concat

    listagg( ) within group ( order by ) 与 wm_concat --oracle 11g 及以后适合 最好 select spbywslid,listagg(xm,' ...

  5. Netty 4源码解析:服务端启动

    Netty 4源码解析:服务端启动 1.基础知识 1.1 Netty 4示例 因为Netty 5还处于测试版,所以选择了目前比较稳定的Netty 4作为学习对象.而且5.0的变化也不像4.0这么大,好 ...

  6. window&period;open post

    前端代码 expExcel(){ window.open(PreURL+'company_list_exp?keyword='+this.keyword+'&area_code='+this. ...

  7. fastDFS 命令笔记

    端口开放 这是命令运行的前提 iptables -I INPUT -p tcp -m state –state NEW -m tcp –dport 22 -j ACCEPT iptables -I I ...

  8. vue确认密码

    rules: { pwd:[{ required:true, message:'创建密码',trigger:'blur' }], cpwd:[{ required:true,message:'确认密码 ...

  9. MyBatis-Plus使用教程

    单机版 安装环境 上传压缩包到/usr/local/software/下 解压安装包,进入解压目录的bin目录下,启动命令: ./solr start -force 默认端口是8983,请求虚拟机, ...

  10. 【MediaElement】WPF视频播放器【1】

    一.前言       前两天上峰要求做一个软件使用向导,使用WPF制作.这不,这两天从一张白纸开始学起,做一个播放演示视频的使用向导.以下是粗设计的原型代码: 二.效果图 三.代码 前台代码: &lt ...