3310: [Usaco2013 Nov]Empty Stalls
Description
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个位置绕成一个环,编号为(0...n-1),有很多头牛,每头牛喜欢一个位置并且会占领它,如果这个 位置已经有了牛便会向后找到第一个没有牛的位置占领;由于数据过大,采用更高端的方法读入,读入x ,y,a,b表示有x个牛喜欢f(1)..f(y),f(i) = (a*i+b)%n;
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
3 2 2 4
2 1 0 1
1 1 1 7
INPUT DETAILS: 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).
Sample Output
OUTPUT DETAILS: All stalls will end up occupied except stall 5.
思路:
suika讲题的时候我以为是一眼并查集,貌似也的确可以做。但是是nlog的,本题正解是2n的暴力。因为插入顺序和结果没有关系,我们记录一下每个节点要插多少,2n扫一遍扫平即可,最后找一个答案。
如果有时间应该会补一个并查集的AC代码(如果真的可以AC的话)
下面是代码
#include <cstdio> #include <iostream> #include <algorithm> using namespace std; int n, k; int hav[3100000]; bool used[3100000]; int main() { scanf("%d%d",&n,&k); for(int i=1;i<=k;i++) { int x, y, a, b; scanf("%d%d%d%d",&x,&y,&a,&b); for(int j=1;j<=y;j++) { int tmp = ((long long)a*j+b)%n+1; hav[tmp]+=x; } } int now=0; for(int i=1;i<=n;i++) { now+=hav[i]; if(now&&!used[i]) { used[i]=1; now--; } } for(int i=1;i<=n;i++) { if(!used[i]&&now) { used[i]=1; now--; } } for(int i=1;i<=n;i++) { if(!used[i]) { printf("%d ",i-1); return 0; } } }