POJ--3176 Stall Reservations(DP)

Posted 57one

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ--3176 Stall Reservations(DP)相关的知识,希望对你有一定的参考价值。

记录
23:51 2023-5-14

http://poj.org/problem?id=3176

reference:《挑战程序设计竞赛(第2版)》第二章练习题索引 p135

POJ 3190 Stall Reservations

Stall Reservations

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15069   Accepted: 5270   Special Judge

Description

Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be 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 to determine which stall each cow can be assigned for her milking time. Of course, no cow will share such a private moment with other cows.

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

Input

Line 1: A single integer, N

Lines 2..N+1: Line i+1 describes cow i‘s milking interval with two space-separated integers.

Output

Line 1: The minimum number of stalls the barn must have.

Lines 2..N+1: Line i+1 describes the stall to which cow i will be assigned for her milking period.

Sample Input

5
1 10
2 4
3 6
5 8
4 7

Sample Output

4
1
2
3
2
4

Hint

Explanation of the sample:

Here‘s a graphical schedule for this output:

Time     1  2  3  4  5  6  7  8  9 10

Stall 1 c1>>>>>>>>>>>>>>>>>>>>>>>>>>>
Stall 2 .. c2>>>>>> c4>>>>>>>>> .. ..
Stall 3 .. .. c3>>>>>>>>> .. .. .. ..
Stall 4 .. .. .. c5>>>>>>>>> .. .. ..
Other outputs using the same number of stalls are possible.
 
题意:一头牛在同一时间只能处理一个区间,问处理所有区间最少需要几个区间,并输出牛的安排情况
 
题解:将所有区间[l,r]排序,l不相等时小l排在前面,相等时小r排在前面,再用优先队列q保存每头牛的工作区间,越快结束的放在前面(r不相等时,小r放前面,r相等时,大l放前面),按顺序处理结构体p里面的牛,能安排队列q中的牛就优先安排,安排不了就加牛
 
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<queue>
#include<set>
#define ll long long
using namespace std;
struct node

    int id;
    int l;
    int r;
p[1000005],temp,now;

int vis[1000005];
bool cmp(node x,node y)//将开始处理时间早,处理时间短的放前面

    if(x.l!=y.l)
        return x.l<y.l;
    else
        return x.r<y.r;

bool operator < (node x,node y)//二次处理的时候,将结束时间早,开始时间晚的放前面

    if(x.r!=y.r)
        return x.r>y.r;
    else
        return x.l<y.l;


priority_queue<node>q;
int main()

    int n;
    while(~scanf("%d",&n))
    
        for(int i=0;i<n;i++)
        
            scanf("%d%d",&p[i].l,&p[i].r);
            p[i].id=i;
        
        sort(p,p+n,cmp);
        int cnt=1;
        q.push(p[0]);
        vis[p[0].id]=1;
        for(int i=1;i<n;i++)
        
            if(!q.empty()&&q.top().r<p[i].l)
            
                vis[p[i].id]=vis[q.top().id];
                q.pop();
            
            else
            
                cnt++;
                vis[p[i].id]=cnt;
            
            q.push(p[i]);
        
        printf("%d\n",cnt);
        for(int i=0;i<n;i++)
            printf("%d\n",vis[i]);
        while(!q.empty())
            q.pop();
    
    return 0;

 

以上是关于POJ--3176 Stall Reservations(DP)的主要内容,如果未能解决你的问题,请参考以下文章

poj3176

Cow Bowling POJ - 3176

POJ-3176

POJ-3176 Cow Bowling

动态规划_Cow Bowling_POJ-3176

Stall Reservations