HDU ACM 4546 Race Difficulty-priority queue

Source: Internet
Author: User

Analysis: Use priority queues.

Take next as priority, small first-out team
Sort after reading the data, initialize the first element of the queue (0,a[0],0)
Each time out of the team an element, the queue (sum,sum+a[nextid+1],nextid+1), (next,next+a[nextid+1],nextid+1), that is, whether add a[nextid+1] are considered in.
In this way each new element is the next smallest (next), and the M-time is small.

#include <iostream> #include <queue> #include <algorithm>using namespace std;struct a{a () {}a (int s), int n,int ni): sum (s), Next (N), NextID (NI) {}bool operator< (const a& A) Const{return This->next>a.next;} int sum;    Current and int next;   Add the next element and int nextid;  The next element id};int A[10010];int n,m;int Run () {priority_queue<a> q; A Tmp;int Cnt;q.push (A (0,a[0],0)); Cnt=0;while (!q.empty ()) {tmp=q.top (); Q.pop (); if (tmp.nextid>=n)  //out of range, A[n], set the end value Continue;q.push (A (tmp.sum,tmp.sum+a[tmp.nextid+1],tmp.nextid+1)); Q.push (A (tmp.next,tmp.next+a[ tmp.nextid+1],tmp.nextid+1)); Cnt++;if (cnt==m) return tmp.next;}} int main () {int t,i,c;scanf ("%d", &t), C=0;while (t--) {scanf ("%d%d", &n,&m), for (i=0;i<n;i++) scanf ("%d" , &a[i]); sort (a,a+n); a[n]=0;printf ("Case #%d:%d\n", ++c,run ());} return 0;}


HDU ACM 4546 Race Difficulty-priority queue

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.