POJ 3190 Stall Reservations (greedy)

Source: Internet
Author: User

Stall Reservations
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3590 Accepted: 1284 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


#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <queue> #include <stack> #include <vector> #include <set> #include <map > #define L (x) (x<<1) #define R (x) (x<<1|1) #define MID (x, y) ((x+y) >>1) #define EPST 1e-8typedef __ Int64 ll; #define FRE (i,a,b) for (i = A; I <b; i++) #define FREE (i,b,a) for (i = b; I >= a;i--) #define MEM (T, v) MEMS ET ((t), V, sizeof (t)) #define SSF (n) scanf ("%s", N) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf (          "%d%d", &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, &c) #define PF Printf#define Bug  PF ("hi\n") using namespace std, #define INF 0x3f3f3f3f#define n 50005struct stud{int x, y;  int POS;  BOOL operator < (CONST stud D) Const {return y>d.y; }}f[n];int ans[n];int k;priority_queue<stud>q;int n;int cmp (stud A,stud b) {if (a.x==b.x) return A.y<b.y;return a.x<b.x;} voidSolve () {int I,j;stud cur;while (!q.empty ()) Q.pop (); for (i=1;i<=n;i++) {if (Q.empty ()) {Ans[f[i].pos]=++k;q.push (f[        I]); continue;}        Cur=q.top ();        if (cur.y<f[i].x) {Ans[f[i].pos]=ans[cur.pos];q.pop ();}        else ans[f[i].pos]=++k; Q.push (F[i]);}}    int main () {int i,j;while (~SF (n)) {for (i=1;i<=n;i++) {SFF (F[I].X,F[I].Y); F[i].pos=i;}    Sort (f+1,f+n+1,cmp); K=0;solve ();p F ("%d\n", K), for (i=1;i<=n;i++) pf ("%d\n", Ans[i]);} return 0;}



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