Sicily 13862. Empty Stalls

农夫约翰的新牛棚由一个巨大的环形区域组成,包含N个编号为0到N-1的摊位,最后一个摊位与第一个相邻。每天结束时,奶牛们陆续返回,每个都有首选的摊位,但如果首选摊位被占据,它会从那里向前扫描直到找到第一个空摊位。给定每个奶牛的首选摊位,确定最后剩下的空摊位的最小索引。答案与奶牛返回的顺序无关。问题输入以简洁格式给出,需要根据描述计算空摊位的最小索引。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

13862. Empty Stalls

Constraints

Time Limit: 2 secs, Memory Limit: 64 MB

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.

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

In the sample, 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.

Problem Source

2015年每周一赛第二场

先假设每个stall可以放无数只奶牛,让每只奶牛都住到它们喜欢的stall中. 然后从第
0个stall开始处理每个stall:只要第i个stall有多于1只奶牛,就将多出来的奶牛转移
到第i+1个stall.


经过一轮处理后,最多只剩下第n-1个stall有多于1只的奶牛. 如果第n-1个stall的奶牛
数多于1,我们继续将多出来的奶牛转移到第0个stall,这样再处理一遍,就能保证每个
stall最多只有1只奶牛. 最后检查一下没有奶牛的最小stall是哪一个即可. O(N).

#include <stdio.h>

int stall[3000005];

int main() {
	int n, k, x, y, a, b;
	scanf("%d%d", &n, &k);
	for (int i = 0; i < k; i++) {
		scanf("%d%d%d%d", &x, &y, &a, &b);
		a %= n;
		int sum = b % n;
		while (y--) {
			sum += a;
			if (sum >= n) sum -= n;
			stall[sum] += x;
		}
	}
	int m = n - 1;
	for (int i = 0; i < m; i++) {
		if (stall[i] > 1) {
			stall[i + 1] += stall[i] - 1;
			stall[i] = 1;
		}
	}
	if (stall[m] > 1) {
		stall[0] += stall[m] - 1;
		stall[m] = 1;
	}
	for (int i = 0; i < n; i++) {
		if (stall[i] == 0) {
			printf("%d\n", i);
			return 0;
		}
		if (stall[i] > 1) {
			stall[i + 1] += stall[i] - 1;
			stall[i] = 1;
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值