Main topic
A sequence of numbers is given, which requires that the sequence of numbers be changed into a monotonically non-descending sequence. If the original number is a[i], the number after the change is b[i], then the cost is |A[I]?B[I]| 。 Find a solution that minimizes the cost.
Ideas
One eye can see is two points, and then greedy or whatever yy a bit on the line.
CODE
#define _crt_secure_no_warnings#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MAX 50010using namespace STD;#define LEFT (POS << 1)#define RIGHT (pos << 1|1)structcow{intX,y,c;intst,ed;BOOL operator< (ConstCow &a)Const{returny > a.y; }voidRead () {scanf("%d%d%d", &x, &y, &c); X *=-1; St = c * (X-1); ed = st + (c <<1); }}src[max];intcows;pair<int,int*> Xx[max <<1];intCnt,t;intTree[max <<4];inline voidPushdown (intPOS) {if(Tree[pos]) {Tree[left] = Tree[pos]; Tree[right] = Tree[pos]; Tree[pos] =0; }}voidModify (intLintRintXintYintCintPOS) {if(L = = x && y = = r) {Tree[pos] = C;return; } pushdown (POS);intMid = (L + r) >>1;if(Y <= mid) Modify (L, Mid, X, Y, C, left);Else if(X > Mid) Modify (Mid +1, R, X, Y, C, right);Else{Modify (L, Mid, X, Mid, C, left); Modify (Mid +1, R, Mid +1, Y, C, right); }}inline intAsk (intLintRintXintPOS) {if(L = = r)returnTree[pos]; Pushdown (POS);intMid = (L + r) >>1;if(x <= mid)returnAsk (L, Mid, X, left);returnAsk (Mid +1, R, X, right);}BOOLV[max];intMain () {Cin>> cows; for(inti =1; I <= cows; ++i) Src[i]. Read (); Sort (src +1, src + cows +1); for(inti =1; I <= cows; ++i) {xx[++cnt] = Make_pair (Src[i].st, &src[i].st); XX[++CNT] = Make_pair (Src[i].ed, &src[i].ed); } sort (xx +1, XX + cnt +1); for(inti =1; I <= CNT; ++i) {if(i = =1|| Xx[i].first! = xx[i-1].first) ++t; *xx[i].second = t; } for(inti =1; I <= cows; ++i) Modify (1, CNT, Src[i].st, Src[i].ed, I,1); for(inti =1; I <= CNT; ++i) V[ask (1, CNT, I,1)] =true;intAns =0; for(inti =1; I <= cows; ++i) ans + = v[i];cout<< ans << Endl;return 0;}
Bzoj 3613 Heoi 2014 South Park floor pile Light flocculation two points + greedy