CF940E Cashback
题目大意
给你一个长度为 的数列 和整数 ,你需要把它任意分段。每一段假设长度为 ,就去掉前 小的数。
最小化剩下的数的和。
题目分析
因为要让剩下数的和最小,所以我们要让去掉的数的和最大。
观察 ,可知分段对答案有贡献当且仅当 。那么我们 满足什么条件时答案最优呢?
首先, 当然要大于等于 (废话上面说过了),其次,容易发现一个性质:一段子串长度为 时答案不会比一段子串长度为 更优,其中 为常数且 。并且我们一定尽量将序列分割为数个长度为 的子串, 应尽量小。综上, 取 。
举个例子: 且数列为 7 9 8 1 1 1
时,分成 7 9 8
和 1 1 1
贡献为 ;但分成 7 9 8 1 1 1
贡献为 。
所以问题转化为:将序列分为数个长度为 的子串(不要求序列长度为 的倍数,即不要求分割的子串连续),每个子串取一个最小值使得取出数之和最大。
令 表示前 个数分割后取出数的和的最大值,于是有:
恍然大悟,随便抽一个数据结构来维护即可,我这里用的是 树套树 线段树。
代码
//2022/2/26
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <climits>//need "INT_MAX","INT_MIN"
#include <cstring>//need "memset"
#include <numeric>
#include <algorithm>
#define int long long
#define enter() putchar(10)
#define debug(c,que) cerr << #c << " = " << c << que
#define cek(c) puts(c)
#define blow(arr,st,ed,w) for(register int i = (st);i <= (ed); ++ i) cout << arr[i] << w;
#define speed_up() cin.tie(0),cout.tie(0)
#define mst(a,k) memset(a,k,sizeof(a))
#define Abs(x) ((x) > 0 ? (x) : (-x))
const int mod = 1e9 + 7;
inline int MOD(int x) {
while (x < 0) x += mod;
while (x >= mod) x -= mod;
return x;
}
namespace Newstd {
char buf[1 << 21],*p1 = buf,*p2 = buf;
inline int getc() {
return p1 == p2 && (p2 = (p1 = buf) + fread(buf,1,1 << 21,stdin),p1 == p2) ? EOF : *p1 ++;
}
inline int read() {
int ret = 0,f = 0;char ch = getc();
while (!isdigit(ch)) {
if(ch == '-') f = 1;
ch = getc();
}
while (isdigit(ch)) {
ret = (ret << 3) + (ret << 1) + ch - 48;
ch = getc();
}
return f ? -ret : ret;
}
inline void write(int x) {
if(x < 0) {
putchar('-');
x = -x;
}
if(x > 9) write(x / 10);
putchar(x % 10 + '0');
}
}
using namespace Newstd;
using namespace std;
const int ma = 1e5 + 5;
int a[ma],dp[ma];
int n,m,sum;
struct Segment_Tree {
struct Node {
int l,r;
int Min;
} node[ma << 2];
#define lson (p << 1)
#define rson (p << 1 | 1)
inline void pushup(int p) {
node[p].Min = min(node[lson].Min,node[rson].Min);
}
inline void build(int p,int l,int r) {
node[p].l = l,node[p].r = r;
if (l == r) {
node[p].Min = a[l];
return;
}
int mid = l + r >> 1;
build(lson,l,mid),build(rson,mid + 1,r);
pushup(p);
}
inline int query(int x,int y,int p) {
if (x <= node[p].l && node[p].r <= y) {
return node[p].Min;
}
int mid = node[p].l + node[p].r >> 1,res = INT_MAX;
if (x <= mid) res = min(res,query(x,y,lson));
if (y > mid) res = min(res,query(x,y,rson));
return res;
}
#undef lson
#undef rson
} seg;
#undef int
int main(void) {
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
#define int long long
n = read(),m = read();
for (register int i = 1;i <= n; ++ i) {
a[i] = read();
sum += a[i];
}
seg.build(1,1,n);
for (register int i = m;i <= n; ++ i) {
dp[i] = max(dp[i - 1],dp[i - m] + seg.query(i - m + 1,i,1));
}
printf("%lld\n",sum - dp[n]);
return 0;
}
本文作者:Coros_Trusds
本文链接:https://www.cnblogs.com/Coros-Trusds/p/15940481.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步