Codeforces Round #437 (Div. 2, based on Memsql start[c]up 3.0-round 2) E

Source: Internet
Author: User
Tags memsql

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.