USACO Empty Stalls
Description
Problem 1: Empty Stalls [Brian Dean, 2013]
Farmer John's new barn consists of a huge circle of N stalls (2 <= N <=
3,000,000), numbered 0..N-1, with stall N-1 being adjacent to stall 0.
At the end of each day, FJ's cows arrive back at the barn one by one, each
with a preferred stall they would like to occupy. However, if a cow's
preferred stall is already occupied by another cow, she scans forward
sequentially from this stall until she finds the first unoccupied stall,
which she then claims. If she scans past stall N-1, she continues scanning
from stall 0.
Given the preferred stall of each cow, please determine the smallest index
of a stall that remains unoccupied after all the cows have returned to the
barn. Notice that the answer to this question does not depend on the order
in which the cows return to the barn.
In order to avoid issues with reading huge amounts of input, the input to
this problem is specified in a concise format using K lines (1 <= K <=
10,000) each of the form:
X Y A B
One of these lines specifies the preferred stall for XY total cows: X cows
prefer each of the stalls f(1) .. f(Y), where f(i) = (Ai + B) mod N. The
values of A and B lie in the range 0...1,000,000,000.
Do not forget the standard memory limit of 64MB for all problems.
Input
* Line 1: Two space-separated integers: N and K.
* Lines 2..1+K: Each line contains integers X Y A B, interpreted as
above. The total number of cows specified by all these lines
will be at most N-1. Cows can be added to the same stall by
several of these lines.
Output
* Line 1: The minimum index of an unoccupied stall.
Sample Input
10 3 3 2 2 4 2 1 0 1 1 1 1 7
Sample Output
5
HINT
INPUT DETAILS:
There are 10 stalls in the barn, numbered 0..9. The second line of input
states that 3 cows prefer stall (21+4) mod 10 = 6, and 3 cows prefer stall
(22+4) mod 10 = 8. The third line states that 2 cows prefer stall (01+1)
mod 10 = 1. Line four specifies that 1 cow prefers stall (11+7) mod 10 =
8 (so a total of 4 cows prefer this stall).
OUTPUT DETAILS:
All stalls will end up occupied except stall 5.
题解:
看完题思考之后觉得属于集合的维护问题,不要被标签蒙蔽,自己想一种解法就去写就好了。
用并查集可过。
设置(fa[i])表示喜欢i号房间的牛能去的房间。这就是一个并查集的合并问题了。用一个标记数组来维护这个房间有没有被人去过,就可以开始维护全部的信息。一开始都是(fa[i]=i),然后依题意模拟占房间的过程。如果没被人占就直接占上,如果被人占了,那么就是(fa[i]=find(i+1)).
应该还比较好理解。
至于最后的答案维护,看代码吧。
代码:
#include<stdio.h>
int v[3000001];
int fa[3000001];
int n,q,k;
int find(int x)
{
if(x==fa[x])
return x;
return fa[x]=find(fa[x]);
}
char *p1,*p2,buf[100000];
#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
int read()
{
int x=0,f=1;
char ch=nc();
while(ch<'0'||ch>'9')
{
if(ch=='-')
f=-1;
ch=nc();
}
while(ch>='0'&&ch<='9')
x=x*10+ch-'0',ch=nc();
return x*f;
}
int main()
{
n=read();q=read();
for(int i=0;i<n;i++)
fa[i]=i;
fa[n]=0;
for(int i=1;i<=q;i++)
{
int a,b,x,y;
x=read();y=read();a=read();b=read();
a%=n;b%=n;
for(int j=1;j<=y;j++)
{
k=find((a*j+b)%n);
for(int l=1;l<=x;l++)
{
v[k]=1;
k=fa[k]=find(k+1);
}
}
}
for(int i=0;i<n;i++)
if(!v[i])
{
printf("%d",i);
return 0;
}
printf("%d",n);
return 0;
}