码迷,mamicode.com
首页 > 其他好文 > 详细

Sicily 13862. Empty Stalls

时间:2015-03-30 09:30:44      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:sicily

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;
}

Sicily 13862. Empty Stalls

标签:sicily

原文地址:http://blog.csdn.net/u012925008/article/details/44730929

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!