"Greedy" poj3190-stall reservations

Source: Internet
Author: User

"The main topic"

Give each cow milking time, at the same time there will not be two cows in the same corral milking, ask at least how many corral.

Ideas

Sort the cows in ascending order at the start of milking, and then use a small top pile to maintain the current milking end time for each corral. For the current cow, if the minimum end time for all corral is greater than its start time, a new corral is opened, the end time is set to the end time of the current cow, the priority queue is added, and if the corral with the lowest end time is available, the end time of the corral is updated to the end time of the current cow.

Finally, the results are output in the order of input.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <queue>5 using namespacestd;6 Const intmaxn=50000+ -;7typedef pair<int,int>p;8 /*First Indicates the end time, second indicates the corral number*/9 structnodeTen { One     intA,b,inno,no; A     BOOL operator< (ConstNode & x)Const -     { -         returna<x.a; the     } - }COW[MAXN]; - intn,num=0; -  + voidSubmain () - { +Sort (cow,cow+n); Apriority_queue<p,vector<p>,greater<p> >que; at     /*set up a small top heap with end time as the keyword*/  -      -num++; -Que.push (P (cow[0].b,num)); -cow[0].no=num; -      for(intI=1; i<n;i++) in     { -P now=que.top (); to         if(cow[i].a<=Now.first) +         { -             /*If the current corral cannot be shared, open a new corral*/ thenum++; * Que.push (P (cow[i].b,num)); $cow[i].no=num;Panax Notoginseng         } -         Else the         { +             /*If you can share it, update the end time of the current corral so long*/ A Que.pop (); the Que.push (P (cow[i].b,now.second)); +cow[i].no=Now.second; -         } $     } $ } -  - voidprint () the { -cout<<num<<Endl;Wuyi     int  out[MAXN]; the      for(intI=0; i<n;i++) out[cow[i].inno]=cow[i].no; -      for(intI=0; i<n;i++) cout<< out[i]<<Endl; Wu } -  About intMain () $ { -scanf"%d",&n); -      for(intI=0; i<n;i++)  -     { Ascanf"%d%d",&cow[i].a,&cow[i].b); +cow[i].inno=i; the         /*since the output is in order of input, the original number must be preserved before sorting*/  -     } $ Submain (); the print (); the     return 0; the}

"Greedy" poj3190-stall reservations

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.