Stall Reservations (POJ 3190 greedy)

Source: Internet
Author: User

Language:DefaultStall Reservations
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3394 Accepted: 1215 Special Judge

Description

Oh those picky n (1 <= n <= 50,000) cows! They is so picky, each one is only being milked over some precise time interval A. B (1 <= a <= b <= 1,000,000), which includes both times A and B. Obviously, FJ must create A reservation system t o determine which stall each cow can is assigned for her milking time. Of course, no cow would share such a private moment with other cows.

Help FJ by determining:
    • The minimum number of stalls required in the barn so, each cow can has her private milking period
    • An assignment of cows to these stalls over time
Many answers is correct for each test dataset; A program would grade your answer.

Input

Line 1: A single integer, N

Lines 2..n+1:line i+1 describes cow i ' s milking interval with the space-separated integers.

Output

Line 1:the minimum number of stalls the barn must has.

Lines 2..n+1:line i+1 describes the stall to which cow I'll be a assigned for her milking period.

Sample Input

51 102 43 65 84 7

Sample Output

412324

Hint

Explanation of the sample:

Here's a graphical schedule for this output:

Time     1  2  3  4  5  6  7  8  9 10Stall 1 c1>>>>>>>>>> >>>>>>>>>>>>>>>>>stall 2.. C2>>>>>> c4>>>>>>>>>. .. Stall 3.. .. C3>>>>>>>>>. .. .. .. Stall 4.. .. .. C5>>>>>>>>>. .. ..
Other outputs using the same number of stalls is possible.

Source

Usaco 2006 February Silver

Test instructions: Given the time period for milking each cow, a machine can only work on a cow at a time, ask at least how many machines are needed, and output the machine number used by each cow.

Idea: First according to the starting time of each cow from small to large sort, maintain a priority queue, N cows in turn, the time to end the first out of the queue, compared to the earliest cattle end time and the current ready to join the cattle start time, if the former is smaller than the latter, then the current cattle can be used in front of empty machine, No one wants to add a new machine.

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set > #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define MAXN 50010#define MAXN 2005# Define mod 1000000009#define INF 0x3f3f3f3f#define pi ACOs ( -1.0) #define EPS 1e-6#define Lson rt<<1,l,mid#define RSO N rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A, I <= b; i++) #define FRL (i,a,b) for (i = A; I < b; i++) #define Mem (T, v) memset ((t), V, sizeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d", &a, & AMP;B) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, &c) #define PF printf#define DBG pf ("hi\n"    ) typedef long Long ll;using namespace std;struct cow{int s,t; int local;}    Cow[maxn];int cmp (Cow A,cow b) {if (A.S==B.S) return a.t<b.t; return A.S&LT;B.S;} PrioRity_queue<cow>q;int ans[maxn];int n;bool operator< (Cow A,cow b) {return a.t>b.t;}    int main () {int i,j;    Cow St;            while (~SF (n)) {FRL (i,0,n) {SFF (cow[i].s,cow[i].t);        Cow[i].local=i;        } sort (cow,cow+n,cmp);        Q.push (Cow[0]);        int cnt=1;        ans[cow[0].local]=cnt; FRL (i,1,n) {if (!                Q.empty () &&q.top (). T<cow[i].s) {st=q.top ();                Q.pop ();            ans[cow[i].local]=ans[st.local];            } else ans[cow[i].local]=++cnt;        Q.push (Cow[i]);        } printf ("%d\n", CNT);    FRL (i,0,n) pf ("%d\n", Ans[i]); } return 0;} /*51 102 43 65 84 7*/



Stall Reservations (POJ 3190 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.