1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
Time Limit: 10 Sec Memory Limit: 64 MB
Submit: 876 Solved: 496
[Submit][Status][Discuss]
1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 876 Solved: 496
[Submit][Status][Discuss]
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
有N头牛,每头牛有个喝水时间,这段时间它将专用一个Stall 现在给出每头牛的喝水时间段,问至少要多少个Stall才能满足它们的要求
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
1 10
2 4
3 6
5 8
4 7
Sample Output
这道题,有两种做法,一种是线段树,一种是差分序列
然而我都没交对。。。。。
粘俩代码,谁也别喷我。。。。
#include<cstdio>
#define M 1000010
inline int read()
{
int x=0;char ch=getchar();
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x;
}
inline int max(int a,int b){return a>b?a:b;}
struct ljn{int l,r,maxx,lazy;}tr[4*M];
int n,x,y;
void make(int l,int r,int p)
{
tr[p].l=l,tr[p].r=r,tr[p].lazy=0;tr[p].maxx=0;
if(l==r) return;
int mid=(l+r)>>1;
make(l,mid,p<<1);
make(mid+1,r,p<<1|1);
}
void pd(int p)
{
int lz=tr[p].lazy;
tr[p<<1].lazy+=lz;
tr[p<<1|1].lazy+=lz;
tr[p<<1].maxx+=lz;
tr[p<<1|1].maxx+=lz;
tr[p].lazy=0;
}
void xg(int l,int r,int c,int p)
{
if(tr[p].l==l&&tr[p].r==r)
{
tr[p].lazy+=c;
tr[p].maxx+=c;
return;
}
pd(p);
int mid=(tr[p].l+tr[p].r)>>1;
if(mid>=r) xg(l,r,c,p<<1);
else if(mid<l) xg(l,r,c,p<<1|1);
else
{
xg(l,mid,c,p<<1);
xg(mid+1,r,c,p<<1|1);
}
tr[p].maxx=max(tr[p<<1].maxx,tr[p<<1|1].maxx);
}
int main()
{
n=read();
make(1,1000000,1);
for(int i=0;i<n;i++)
{
x=read();y=read();
xg(x,y,1,1);
}
printf("%d\n",tr[1].maxx);
}
下面这个diao一点
#include<cstdio>
#include<algorithm>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
const int maxd=1000000+10;
int f[maxd];
int i,j,k,l,t,n,m,ans;
int main(){
scanf("%d",&n);
fo(i,1,n){
scanf("%d%d",&j,&k);
f[j]++;f[k+1]--;
}
fo(i,1,1000001){
f[i]+=f[i-1];
ans=max(ans,f[i]);
}
printf("%d\n",ans);
}