传送门
题目描述
给你一个长度为n的数列a和整数c
你需要把它任意分段
每一段假设长度为k,就去掉前 ⌊ k c ⌋ \lfloor\frac{k}{c}\rfloor ⌊ck⌋小的数
最小化剩下的数的和
分析
我们可以把每一个长度为k的区间删减掉一个最小值,也可以把每一个数单独拆出来不进行删除,所以很容易写出状态方程
f[i] = min(f[i - 1] + a[I],f[i - m] + sum[I] - sum[i - m] - min[i - m + 1 ~i])
最小值可以用单调队列或者线段树来维护
代码
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <cstring>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define _CRT_SECURE_NO_WARNINGS
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#pragma GCC option("arch=native","tune=native","no-zero-upper")
#pragma GCC target("avx2")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 10;
int n,m;
ll a[N];
ll sum[N];
ll f[N];
struct node{
int l,r;
ll v;
}tr[N * 5];
void pushup(int u){ //子节点信息更新父节点
tr[u].v = min(tr[u << 1].v,tr[u << 1 | 1].v);
}
void build(int u,int l,int r){
tr[u] = {l,r};
if(l == r) {
tr[u].v = a[l];
return;
}
int mid = l + r >> 1;
build(u << 1,l,mid),build(u << 1 | 1,mid + 1,r);
pushup(u);
}
int query(int u,int l,int r){
if(tr[u].l >= l && tr[u].r <= r) return tr[u].v;
int mid = tr[u].l + tr[u].r >> 1;
int v = INF;
if(l <= mid) v = query(u << 1,l,r);
if(r > mid) v = min(v,query(u << 1 | 1,l,r));
return v;
}
int main(){
scanf("%d%d",&n,&m);
for(int i = 1;i <= n;i++) {
scanf("%lld",&a[i]);
sum[i] = sum[i - 1] + a[i];
}
build(1,1,n);
for(int i = 1;i <= n;i++){
f[i] = f[i - 1] + a[i];
if(i < m) continue;
f[i] = min(f[i],f[i - m] + sum[i] - sum[i - m] - query(1,i - m + 1,i));
}
printf("%lld\n",f[n]);
return 0;
}
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/