1. 程式人生 > >[USACO13NOV]空蕩蕩的攤位Empty Stalls

[USACO13NOV]空蕩蕩的攤位Empty Stalls

question 描述 art 被占用 eterm read his class except

題目描述

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.

約翰的谷倉中有N(2 <= N <=3,000,000)個房間,編號0到N-1,這些房間排布成環狀,編號0的和編號N-1的相鄰。

每天傍晚,奶牛們一只一只排隊回到谷倉,每頭奶牛都有一個喜歡的房間,但是,如果它喜歡的房間已被其他奶牛占了,它會向前挨個探索其他房間(如果它探索過了N-1號房間,它會繼續探索0號房間,以此繼續下去)直到探到第一個沒有被占用的房間,這時它會宣布占用這個房間。

告訴你每頭奶牛喜歡的房間,當所有奶牛都找到房間後,剩下的沒被占用的房間中,編號最小的是哪個。很明顯,問題的答案與奶牛進入谷倉的順序無關。

為避免輸入內容過多。本題的輸入數據采用一種簡潔的方式:一共K(1 <= K <=10,000)行,每行格式如下:

X Y A B

表示有Y批奶牛,每批X頭,也就是總共X*Y只奶牛喜歡的房間號。Y批奶牛編號1到Y,第i批X頭奶牛喜歡的房間號為(A*i+B) Mod N.

A和B的取值範圍為0...1,000,000,000

註意,只有64M的空間。

輸入輸出格式

輸入格式:
  • 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.
輸出格式:
  • Line 1: The minimum index of an unoccupied stall.

輸入輸出樣例

輸入樣例#1: 復制
10 3 
3 2 2 4 
2 1 0 1 
1 1 1 7 
輸出樣例#1: 復制
5 

說明

There are 10 stalls in the barn, numbered 0..9. The second line of input states that 3 cows prefer stall (2*1+4) mod 10 = 6, and 3 cows prefer stall (2*2+4) mod 10 = 8. The third line states that 2 cows prefer stall (0*1+1) mod 10 = 1. Line four specifies that 1 cow prefers stall (1*1+7) mod 10 = 8 (so a total of 4 cows prefer this stall).

All stalls will end up occupied except stall 5.

思路

f[i]指向向最近的一個空位;

一個類似並查集的find_father()維護;

代碼

 1 #include<cstdio>
 2 #include<cstring>
 3 const int maxn=3e6+10;
 4 int n,k;
 5 int x,y,a,b,c,d;
 6 int f[maxn];
 7 int ff(int k){return f[k]==f[n]?k:f[k]=ff(f[k]);}
 8 int main(){
 9     freopen("empty.in","r",stdin);
10     freopen("empty.out","w",stdout);
11     scanf("%d%d",&n,&k);
12     memset(f,0x7f,sizeof(f));
13     for(int i=1;i<=k;i++){
14         scanf("%d%d%d%d",&x,&y,&a,&b);
15         for(int i=1;i<=y;i++){
16             c=(1ll*a*i+b)%n;
17             for(int j=0;j<x;j++){
18                 d=ff((c+j)%n);
19                 f[d]=ff((d+1)%n);
20             }
21         }
22     }
23     printf("%d",ff(0));
24     return 0;
25 }

[USACO13NOV]空蕩蕩的攤位Empty Stalls