Priority queue practice (again my big bell God)

Source: Internet
Author: User

"Problem description"


Now there are m positions can be hit SIF, there are n+1 individuals in line waiting to hit the SIF. Now tell your individual how long it takes for each person to hit SIF and ask you for the first time? + 1 people when to hit Sif. Before?
Individuals must come in order)


"Input Format"


The first line of two integers n,m as described above.
Next, an integer for each line represents the time that everyone needs.


"Output Format"


An integer line represents the answer.


"Sample Input"


3 2
1
1
1


"Sample Output"


1


"Sample Interpretation"


There is a temple in the mountains, and the temple is a god of bells.


"Data size and conventions"


For 100% of the data, everyone needs less than 10 5 of the time.
Test point N m test point N m
1 10 10 1 5000 500
2 20 10 2 100000 5000
3 50 10 3 100000 10000
4 1000 500 4 100000 20000
5 2000 500 5 100000 50000

Ideas:

Priority_queue (priority queue) maintains the sum of time spent by people who have been playing SIF in m locations;

The minimum position is maintained each time according to the nature of the priority queue;

Wait until n maintenance to output the minimum value;

But we maintain the minimum value, so we need a small Gan, but I really do not want to write the heap (after all, a konjac konjac);

So we have a Dagen (priority queue) of a silk optimization so that he became a slightly perverted little Gan;

What is this optimization?

Ha

Is that all elements are multiplied by-1;

Come on, on the code:

#include <queue>#include<cstdio>using namespacestd;intn,m,ai[100001],cur;CharCh;priority_queue<int>Q;voidQread (int&x) {x=0; ch=GetChar ();  while(ch>'9'|| ch<'0') ch=GetChar ();  while(ch<='9'&&ch>='0') {x=x*Ten+(int) (ch-'0'); ch=GetChar ();}}intMain () {Qread (n), Qread (m);  for(intI=1; i<=n;i++) {qread (Ai[i]), Ai[i]*=-1; if(Q.size () <m) Q.push (Ai[i]); Else{cur=Q.top ();            Q.pop (); Cur+=Ai[i];        Q.push (cur); }} cur=q.top () * (-1); printf ("%d\n", cur); return 0;}

Priority queue practice (again my big bell God)

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.