1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1031 Solved: 582
[ 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
OUTPUT DETAILS:
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.
HINT
不妨试下这个数据,对于按结束点SORT,再GREEDY的做法 1 3 5 7 6 9 10 11 8 12 4 13 正确的输出应该是3
Source
第一种方法是直接用线段树暴力区间加1
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1e9;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
const int N = 1000005;
struct {
int l,r,val,tag;
}t[3000005];
void pushdown(int k)
{
//向下传递tag
if(t[k].l==t[k].r) return;
int tag = t[k].tag;
t[k].tag=0;
if(tag)
{
t[k<<1].tag =t[k<<1].tag+tag;
t[k<<1|1].tag=t[k<<1|1].tag+tag;
t[k<<1].val=t[k<<1].val+tag;
t[k<<1|1].val=t[k<<1|1].val+tag;
}
}
void build(int k,int l,int r)
{
t[k].l = l;
t[k].r = r;
if(l==r) return ;
int mid = (l+r)>>1;
build(k<<1,l,mid);
build(k<<1|1,mid+1,r);
}
void update(int k,int x,int y,int val)
{
pushdown(k);
int l = t[k].l;
int r = t[k].r;
//更新最大值
if(l==x&&r==y)
{
t[k].tag ++;
t[k].val ++;
return ;
}
//分段更新
int mid = (l+r)>>1;
if(y<=mid)
update(k<<1,x,y,val);
else if(x>mid)
update(k<<1|1,x,y,val);
else
{
update(k<<1,x,mid,val);
update(k<<1|1,mid+1,y,val);
}
t[k].val=max(t[k<<1].val,t[k<<1|1].val);
}
int query(int k,int x)
{
pushdown(k);
int l = t[k].l;
int r = t[k].r;
if(l==r)
{
return t[k].val;
}
int mid = (l+r)>>1;
if(x<=mid)
{
return query(k<<1,x);
}
else
{
return query(k<<1|1,x);
}
}
int n;
int l,r;
int main()
{
// freopen("data.txt","r",stdin);
ios_base::sync_with_stdio(false);
cin >> n;
build(1,1,1000000);
for(int i=0;i<n;i++)
{
cin >> l>>r;
update(1,l,r,1);
}
pushdown(1);
cout << t[1].val<<endl;
return 0;
}
第二种是用优先队列,优先右边值最小的
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1e9;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
struct node {
int l;
int r;
bool operator < (const node &x) const
{
return r>x.r;
}
};
bool cmp(node x,node y)
{
return x.l<y.l;
}
node nt;
node cur;
int n;
vector<node> a;
int main()
{
// freopen("data.txt","r",stdin);
// ios_base::sync_with_stdio(false);
n=read();
for(int i=0;i<n;i++)
{
nt.l=read();
nt.r=read();
a.push_back(nt);
}
sort(a.begin(),a.end(),cmp);
priority_queue<node> q;
q.push(a[0]);
for(int i=1;i<n;i++)
{
cur = q.top();
if(cur.r<a[i].l)
{
q.pop();
}
q.push(a[i]);
}
printf("%d\n",q.size());
return 0;
}