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

时间:2021-05-19 17:36:23

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

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

这题的话咋看无从下手,其实我们的目标就是要尽可能去掉多的数。一个数只能被整段区间去掉,而区间长度只能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 cf940e(优先队列/单调队列+dp) smallest. For example, the value of the array [3, 1, 6, 5, 2] withc = 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 ofa.

Output

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

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.