# [BZOJ1651]&&[Usaco2006 Feb]Stall Reservations 专用牛棚

## 描述 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.

## 输入格式 InputFormat

Line 1: A single integer, N.

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

## 输出格式 OutputFormat

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.

10
1 4
2 6
1 5
5 7
5 9
3 4
1 3
5 8
4 5
3 8

7

Silver

BZOJ 1651

## 代码 Code

``````#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int inf=0x7fffffff;
int a;
int i,j,t,n,m,l,r,k,z,y,x,ans;
inline void read(int &x)
{
x=0;char ch=getchar();
while (ch<'0' || ch>'9') ch=getchar();
while (ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
}
int main()
{
memset(a,0,sizeof(a));
read(n);
for (r=0,i=1;i<=n;i++)
{
read(x);read(y);
a[x]++;a[y+1]--;
r=max(r,y+1);
}
for (t=0,i=1;i<=r;i++)
{
t+=a[i];
ans=max(ans,t);
}
printf("%d\n",ans);
return 0;
}``````