1 条题解

  • 0
    @ 2026-7-4 11:04:39

    #include <cstdio>
    #include <queue>
    using namespace std;
    const int M = 200005;
    #define int long long
    int read()
    {
    	int x=0,f=1;char c;
    	while((c=getchar())<'0' || c>'9') {if(c=='-') f=-1;}
    	while(c>='0' && c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();}
    	return x*f;
    }
    int n,sh,ans,a[M];priority_queue<int> q;
    signed main()
    {
    	n=read();
    	for(int i=1;i<=n;i++)
    	{
    		a[i]=read();int x=a[i],y=0;
    		if(i>1) sh+=max(a[i]-a[i-1],0ll);//tag
    		q.push(-sh);q.push(-sh);//insert the change point
    		q.push(a[i]-sh);q.push(a[i]-sh);
    		q.pop();y=q.top()+sh;q.pop();//pre min
    		ans+=2*y-x;//update the 0-slope-line
    	}
    	printf("%lld\n",ans);
    }
    
    
    • 1

    信息

    ID
    9231
    时间
    2000ms
    内存
    1024MiB
    难度
    10
    标签
    递交数
    2
    已通过
    1
    上传者