Test instructions: Minus the previous number, plus the number of the back, to ensure that the last number, plus and minus the same number of times;
Puzzle: Emmmmm, see is a greedy, first on the value sort, the same on the subscript sort, the rule is to find the first one, and then from the back to the next table is not used the first, subtract, and then until found,
But this time complexity is O (n^2), think for a long time or do not know how to use priority queue to optimize = =, the results found that other people are not the rule to do .... , are direct violence thrown in, direct reduction, in order to ensure that the largest reduction, the number of the reduction is still two to the queue, so as to ensure that the time to reduce the amount of the current number minus the previous number, and the middle number is thrown into the queue
#include <bits/stdc++.h>#defineFi first#defineSe Second#defineMP Make_pair#definePB Push_back#defineRead (a) scanf ("%d", &a)#definePII pair<int,int>#defineC 0.5772156649#definePi ACOs (-1.0)#definell Long Long#defineMoD 1000000007#defineLS l,m,rt<<1#defineRS M+1,r,rt<<1|1using namespacestd;Const Doubleg=10.0, eps=1e-7;Const intn=300000+Ten, maxn= -+Ten, inf=0x3f3f3f;p Riority_queue<int,vector<int>,greater<int> >Q;intMain () {intN; Read (n); ll ans=0; for(intI=0; i<n;i++) { intA; Read (a); if(Q.empty () | | a<=q.top ()) Q.push (a); Else{ans+=a-Q.top (); Q.pop (); Q.push (a); Q.push (a); }} printf ("%lld\n", ans); return 0;}/****************************************/
View Code
Codeforces Round #437 (Div. 2, based on Memsql start[c]up 3.0-round 2) E