Hdoj 5360 Hiking Priority queue + greedy

Source: Internet
Author: User

Original title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5360

Test instructions

Probably means that everyone has a number to accept the range $[l_i,r_i]$, now you can choose a person from the one who has not been selected, and if the current number fits the person's needs, then this person will be selected. Now lets you output a selection of sequences so that the number of people selected is as much as possible.

Exercises

Greed is good, in general is the number of constantly increasing, each time from the feasible all $l$ choose $r$ the smallest one. As to why this is optimal. Need a brain tonic.

Code:
#include <iostream>#include<queue>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<vector>#include<functional>#defineMax_n 100005using namespacestd;structNode { Public:    intVal; intPOS; Node (intVintp): Val (v), POS (p) {} node () {}BOOL operator< (ConstNode &a)Const {        returnVal >A.val; }};p Riority_queue<node>que;intN;intT;vector<node>L[max_n];vector<int>G;intL[max_n],r[max_n];BOOLUsed[max_n];voidinit () { for(inti =0; I <= N; i++) l[i].clear (); memset (Used,0,sizeof(used));    G.clear ();  while(Que.size ()) Que.pop ();}intMain () {scanf ("%d", &T);  while(t--) {scanf ("%d", &N);        Init ();  for(inti =0; I < n; i++) scanf ("%d", &L[i]);  for(inti =0; I < n; i++) scanf ("%d", &R[i]);  for(inti =0; I < n; i++) L[l[i]].push_back (node (r[i), I+1)); intAns =0;  for(inti =0; I <= N; i++) {             for(intj =0; J < L[i].size (); J + +) Que.push (L[i][j]);  while(Que.size () && que.top (). val <i) Que.pop (); if(Que.empty ()) {ans=i;  Break;            } g.push_back (Que.top (). POS);        Que.pop (); } printf ("%d\n", ans);  for(inti =0; I < g.size (); i++) {printf ("%d", G[i]); Used[g[i]]=1; }         for(inti =1; I <= N; i++)if(!used[i]) printf ("%d", i); printf ("\ n"); }    return 0;}

Hdoj 5360 Hiking Priority queue + greedy

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.