CF940E Cashback 线段树优化DP
题目描述
Since you are the best Wraith King, Nizhniy Magazin «Mir» at the centre of Vinnytsia is offering you a discount.
You are given an array a a a of length n n n and an integer c c c .
The value of some array b b b of length k k k is the sum of its elements except for the smallest. For example, the value of the array [3,1,6,5,2] [3,1,6,5,2] [3,1,6,5,2] with c=2 c=2 c=2 is 3+6+5=14 3+6+5=14 3+6+5=14 .
Among all possible partitions of a a a into contiguous subarrays output the smallest possible sum of the values of these subarrays.
输入格式
The first line contains integers n n n and c c c ( 1<=n,c<=100000 1<=n,c<=100000 1<=n,c<=100000 ).
The second line contains n n n integers ai a_{i} ai ( 1<=ai<=109 1<=a_{i}<=10^{9} 1<=ai<=109 ) — elements of a a a .
输出格式
Output a single integer — the smallest possible sum of values of these subarrays of some partition of a a a .
题意翻译
给你一个长度为n的数列a和整数c
你需要把它任意分段
每一段假设长度为k,就去掉前\(\lfloor\frac{k}{c}\rfloor\) 小的数
最小化剩下的数的和
输入输出样例
输入 #1
3 5
1 2 3
输出 #1
6
输入 #2
12 10
1 1 10 10 10 10 10 10 9 10 10 10
输出 #2
92
输入 #3
7 2
2 3 6 4 5 7 1
输出 #3
17
输入 #4
8 4
1 3 4 5 5 3 4 1
输出 #4
23
说明/提示
In the first example any partition yields 6 as the sum.
In the second example one of the optimal partitions is [1,1],[10,10,10,10,10,10,9,10,10,10] [1,1],[10,10,10,10,10,10,9,10,10,10] [1,1],[10,10,10,10,10,10,9,10,10,10] with the values 2 and 90 respectively.
In the third example one of the optimal partitions is [2,3],[6,4,5,7],[1] [2,3],[6,4,5,7],[1] [2,3],[6,4,5,7],[1] with the values 3, 13 and 1 respectively.
In the fourth example one of the optimal partitions is [1],[3,4,5,5,3,4],[1] [1],[3,4,5,5,3,4],[1] [1],[3,4,5,5,3,4],[1] with the values 1, 21 and 1 respectively.
分析
首先,因为要最小化剩下的数的和,那么我们肯定要使取走的数的总和尽可能大
我们还可以发现,因为要去掉前\(\lfloor\frac{k}{c}\rfloor\) 小的数
因此只有一段区间的长度大于等于\(c\)时才会对结果产生贡献
而且我们划分出长度为\(k\times c + m (1\leq m < c)\)的区间一定不如划分出长度为\(k\times c\)的区间更优
因为这两种情况选出的数字的数量相同,但是在第二种情况中选出数的最小值一定不会比前一种情况更小
但是这样写还是不太好处理,因为要涉及到前\(k\)小的数,所以似乎要用到某些高级数据结构
而这样显然是不好处理的
我们进一步推导会发现,将一个长度为\(k\times c\)的区间划分为\(k\)个长度为\(c\)的区间所产生的结果只会更优
比如下面一个长度为\(8\)的序列,\(c=4\)
\(1、 1 、2 、5 、3 、7、 8、 9\)
如果我们把它划分为长度为\(8\)的序列,那么产生的贡献为\(1+1=2\)
但是如果我们把它分成两个长度为\(4\)的序列
\(1、1、2、5\)和\(3、7 、8、9\)
那么产生的贡献为\(1+3=4\)
显然后一种更优
因此,我们将原题进一步转换成将一个长度为\(n\)的序列划分为若干长度为\(c\)的序列,使每一个序列的最小值之和最大
其中区间最值可以用线段树去维护
那么我们可以写出如下的状态转移方程
for(int i=1;i<=n;i++){
f[i]=max(f[i],f[i-1]);
if(i>=c) f[i]=max(f[i],f[i-c]+(long long)jl[i]);
}
其中\(f[i]\)表示以遍历到下标为\(i\)的元素所选出的最大价值
\(jl[i]\)表示以\(a[i]\)结尾的长度为\(c\)的区间中的最小值
如果我们选取以\(a[i]\)结尾的长度为\(c\)的区间,那么\(f[i]=max(f[i],f[i-c]+(long long)jl[i]\)
否则\(f[i]=max(f[i],f[i-1])\)
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int a[maxn];
struct trr{
int l,r,mmin;
}tr[maxn<<2];
void push_up(int da){
tr[da].mmin=min(tr[da<<1].mmin,tr[da<<1|1].mmin);
}
void build(int da,int l,int r){
tr[da].l=l,tr[da].r=r;
if(l==r){
tr[da].mmin=a[l];
return;
}
int mids=(l+r)>>1;
build(da<<1,l,mids);
build(da<<1|1,mids+1,r);
push_up(da);
}
int cx(int da,int l,int r){
if(tr[da].l>=l && tr[da].r<=r){
return tr[da].mmin;
}
int ans=0x3f3f3f3f,mids=(tr[da].l+tr[da].r)>>1;
if(l<=mids) ans=min(ans,cx(da<<1,l,r));
if(r>mids) ans=min(ans,cx(da<<1|1,l,r));
return ans;
}
int jl[maxn];
long long f[maxn];
int main(){
long long tot=0;
int n,c;
scanf("%d%d",&n,&c);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
tot+=(long long)a[i];
}
build(1,1,n);
long long ans=0;
for(int i=1;i<=n-c+1;i++){
jl[i+c-1]=cx(1,i,i+c-1);
}
for(int i=1;i<=n;i++){
f[i]=max(f[i],f[i-1]);
if(i>=c) f[i]=max(f[i],f[i-c]+(long long)jl[i]);
}
printf("%lld\n",tot-f[n]);
return 0;
}
CF940E Cashback 线段树优化DP的更多相关文章
- Codeforces Round #426 (Div. 2) D 线段树优化dp
D. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...
- BZOJ2090: [Poi2010]Monotonicity 2【线段树优化DP】
BZOJ2090: [Poi2010]Monotonicity 2[线段树优化DP] Description 给出N个正整数a[1..N],再给出K个关系符号(>.<或=)s[1..k]. ...
- [AGC011F] Train Service Planning [线段树优化dp+思维]
思路 模意义 这题真tm有意思 我上下楼梯了半天做出来的qwq 首先,考虑到每K分钟有一辆车,那么可以把所有的操作都放到模$K$意义下进行 这时,我们只需要考虑两边的两辆车就好了. 定义一些称呼: 上 ...
- 【bzoj3939】[Usaco2015 Feb]Cow Hopscotch 动态开点线段树优化dp
题目描述 Just like humans enjoy playing the game of Hopscotch, Farmer John's cows have invented a varian ...
- POJ 2376 Cleaning Shifts (线段树优化DP)
题目大意:给你很多条线段,开头结尾是$[l,r]$,让你覆盖整个区间$[1,T]$,求最少的线段数 题目传送门 线段树优化$DP$裸题.. 先去掉所有能被其他线段包含的线段,这种线段一定不在最优解里 ...
- 洛谷$P2605\ [ZJOI2010]$基站选址 线段树优化$dp$
正解:线段树优化$dp$ 解题报告: 传送门$QwQ$ 难受阿,,,本来想做考试题的,我还造了个精妙无比的题面,然后今天讲$dp$的时候被讲到了$kk$ 先考虑暴力$dp$?就设$f_{i,j}$表示 ...
- D - The Bakery CodeForces - 834D 线段树优化dp···
D - The Bakery CodeForces - 834D 这个题目好难啊,我理解了好久,都没有怎么理解好, 这种线段树优化dp,感觉还是很难的. 直接说思路吧,说不清楚就看代码吧. 这个题目转 ...
- 4.11 省选模拟赛 序列 二分 线段树优化dp set优化dp 缩点
容易想到二分. 看到第一个条件容易想到缩点. 第二个条件自然是分段 然后让总和最小 容易想到dp. 缩点为先:我是采用了取了一个前缀最小值数组 二分+并查集缩点 当然也是可以直接采用 其他的奇奇怪怪的 ...
- Codeforces 1603D - Artistic Partition(莫反+线段树优化 dp)
Codeforces 题面传送门 & 洛谷题面传送门 学 whk 时比较无聊开了道题做做发现是道神题( 介绍一种不太一样的做法,不观察出决策单调性也可以做. 首先一个很 trivial 的 o ...
随机推荐
- Linux文件目录和访问权限
前言 本文知识点是曾经学习过程中收录整理的,方便学习使用,并非在下撰写. 一>Lniux目录结构 /:根目录,一般根目录下只存放目录,在Linux下有且只有一个根目录.所有的东西都是从这里开始. ...
- 看完这篇 HashMap,和面试官扯皮就没问题了
HashMap 概述 如果你没有时间细抠本文,可以直接看 HashMap 概述,能让你对 HashMap 有个大致的了解. HashMap 是 Map 接口的实现,HashMap 允许空的 key-v ...
- java之SFTP上传下载
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.ut ...
- 【解读】Http协议
一.HTTP简介 1.HTTP协议,即超文本传输协议(Hypertext transfer protocol).是一种详细规定了浏览器和万维网(WWW = World Wide Web)服务器之间互相 ...
- Perl入门(一)Perl的基本类型及运算符
在学习Perl的基础之前,还是希望大家有空去看以下Perl的简介.百度百科 一.Perl的基本类型 Per的基本类型分为两种:数值型和字符串型. 数值型可细分为 整数型.如123. 浮点型.如123. ...
- Jmeter系列(29)- 详解 JDBC Connection Configuration
如果你想从头学习Jmeter,可以看看这个系列的文章哦 https://www.cnblogs.com/poloyy/category/1746599.html 前言 发起 jdbc 请求前,需要有 ...
- swagger ui demo
前言 前几天一个朋友公司在用Springboot集合swagger时候总是从浏览器看不了接口,我两找了问题,但是他还是没有找到,于是我就自己从http://start.spring.io/上下载了一个 ...
- Spring9——通过用Aware接口使用Spring底层组件、环境切换
通过用Aware接口使用Spring底层组件 能够供我们使用的组件,都是Aware的子接口. ApplicationContextAware:实现步骤: (1)实现Applic ...
- 主线程用afxBeginThread()创建多个线程安全退出的办法
HANDLE hand[]; CCriticalSection m_crisecoin; CEvent m_event; struct Student { int nNO; int nYear; CW ...
- BigDecimal类型比较数字大小
BigDecimal类型比较数字大小1.转成intBigDecimal b1 = new BigDecimal("-121454125453.145");if(b1.intValu ...