cf940e(优先队列/单调队列+dp)

针对Cashback问题,本文介绍了一种使用单调队列或优先队列进行预处理的方法,通过动态规划求解数组的最佳分区方案,以达到最小化分区价值总和的目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这场可以说是相当水了。。然而一下涨了辣么多很高兴:)

这题题意看了很久。。楞是看不懂(怀疑自己和外国人的思维有些脱节?)。。然后还是别人告诉的题意。。

这题的话咋看无从下手,其实我们的目标就是要尽可能去掉多的数。一个数只能被整段区间去掉,而区间长度只能c的整数倍,因为再加上一两个元素没有作用,然后再想一下可以知道,如果2*c长度的区间分成2个长度为c的区间其实更合适,因为如果2个区间和并,次小值绝对不可能变大,相反可能会变小。。所以区间长度只能是c,或者干脆不给分区间。。。

这样一来,就可以先预处理区间最小数,很容易想到用单调队列,这里用了优先队列其实没有必要。。。

处理完后,就直接来一次dp就可以了

设扫到i位去掉数之和为d[i],区间右段为i时去掉的数为c[i]

d[i]=max(d[i-1],d[i-k]+c[i])



优先队列

/**
 *        ┏┓    ┏┓ 
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃   
 *        ┃   ━    ┃ 
 *        ┃ >   < ┃ 
 *        ┃       ┃ 
 *        ┃... ⌒ ...  ┃ 
 *        ┃       ┃ 
 *        ┗━┓   ┏━┛ 
 *          ┃   ┃ Code is far away from bug with the animal protecting           
 *          ┃   ┃   神兽保佑,代码无bug 
 *          ┃   ┃            
 *          ┃   ┃         
 *          ┃   ┃ 
 *          ┃   ┃            
 *          ┃   ┗━━━┓ 
 *          ┃       ┣┓ 
 *          ┃       ┏┛ 
 *          ┗┓┓┏━┳┓┏┛ 
 *           ┃┫┫ ┃┫┫ 
 *           ┗┻┛ ┗┻┛ 
 */  
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-12
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 200005
#define nm 105
#define pi 3.1415926535897931
using namespace std;
const ll inf=1000000000;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}






ll a[NM],c[NM],d[NM],ans,s;
ll n,k;

struct tmp{
	int num;
	bool operator<(const tmp&o)const{return a[num]>a[o.num];}
}b[NM];
priority_queue<tmp>q;
int main(){
	n=read();k=read();
	inc(i,1,n)b[i].num=i;
	inc(i,1,n)a[i]=read();
	inc(i,1,n)s+=a[i];
	inc(i,1,k-1)q.push(b[i]);
	inc(i,k,n){
		q.push(b[i]);
		tmp t=q.top();
		while(t.num<=i-k)q.pop(),t=q.top();
		c[i]=a[t.num];
	}
	inc(i,k,n)d[i]=max(d[i-1],d[i-k]+c[i]);
	inc(i,1,n)ans=max(ans,d[i]);
	printf("%I64d\n",s-ans);
	return 0;
}




单调队列

/**
 *        ┏┓    ┏┓ 
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃   
 *        ┃   ━    ┃ 
 *        ┃ >   < ┃ 
 *        ┃       ┃ 
 *        ┃... ⌒ ...  ┃ 
 *        ┃       ┃ 
 *        ┗━┓   ┏━┛ 
 *          ┃   ┃ Code is far away from bug with the animal protecting           
 *          ┃   ┃   神兽保佑,代码无bug 
 *          ┃   ┃            
 *          ┃   ┃         
 *          ┃   ┃ 
 *          ┃   ┃            
 *          ┃   ┗━━━┓ 
 *          ┃       ┣┓ 
 *          ┃       ┏┛ 
 *          ┗┓┓┏━┳┓┏┛ 
 *           ┃┫┫ ┃┫┫ 
 *           ┗┻┛ ┗┻┛ 
 */  
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-12
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 200005
#define nm 105
#define pi 3.1415926535897931
using namespace std;
const ll inf=1000000000;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}






ll a[NM],c[NM],d[NM],ans,s;
int n,k,q[NM],qh,qt;
int main(){
	n=read();k=read();k=min(k,n+1);
	inc(i,1,n)a[i]=read();
	inc(i,1,n)s+=a[i];
	q[qh=qt=1]=1;
	inc(i,2,k-1){while(a[i]<=a[q[qt]])qt--;q[++qt]=i;}
	inc(i,k,n){
		while(qh<=qt&&q[qh]<=i-k)qh++;
		while(qh<=qt&&a[i]<=a[q[qt]])qt--;
		q[++qt]=i;
		c[i]=a[q[qh]];
	}
	inc(i,k,n)d[i]=max(d[i-1],d[i-k]+c[i]);
	inc(i,1,n)ans=max(ans,d[i]);
	printf("%I64d\n",s-ans);
	return 0;
}






E. Cashback
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 of length n and an integer c.

The value of some array b of length k is the sum of its elements except for the smallest. For example, the value of the array [3, 1, 6, 5, 2] with c = 2 is 3 + 6 + 5 = 14.

Among all possible partitions of a into contiguous subarrays output the smallest possible sum of the values of these subarrays.

Input

The first line contains integers n and c (1 ≤ n, c ≤ 100 000).

The second line contains n integers ai (1 ≤ ai ≤ 109) — elements of a.

Output

Output a single integer  — the smallest possible sum of values of these subarrays of some partition of a.

Examples
Input
Copy
3 5
1 2 3
Output
6
Input
Copy
12 10
1 1 10 10 10 10 10 10 9 10 10 10
Output
92
Input
Copy
7 2
2 3 6 4 5 7 1
Output
17
Input
Copy
8 4
1 3 4 5 5 3 4 1
Output
23
Note

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] with the values 2 and 90 respectively.

In the third example one of the optimal partitions is [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] with the values 1, 21 and 1 respectively.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值