题解 P4597 【序列sequence】
bztMinamoto · · 题解
表示完全看不懂楼下的大佬们说啥……特别是某绿……某c姓大佬
来说说个人的理解吧
大佬们说:考虑当前的数
不难看出,
看到这里,我一直有一个疑问,如果令
仔细想了想,实际上应该是这样的:为了让序列非降,
那么这里为什么要让
概括一下,对于当前的数,无论最优解如何,对答案的贡献是一定的。而为了保证之后的解也最优,令
代码好短……
//minamoto
#include<cstdio>
#include<iostream>
#include<queue>
using namespace std;
#define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<21],*p1=buf,*p2=buf;
inline int read(){
#define num ch-'0'
char ch;bool flag=0;int res;
while(!isdigit(ch=getc()))
(ch=='-')&&(flag=true);
for(res=num;isdigit(ch=getc());res=res*10+num);
(flag)&&(res=-res);
#undef num
return res;
}
priority_queue<int> q;
int n;long long ans;
int main(){
n=read();
while(n--){
int x=read();q.push(x);
if(x<q.top()){
ans+=q.top()-x;
q.pop();q.push(x);
}
}
printf("%lld\n",ans);
return 0;
}