Sicily 13862. Empty stalls

Source: Internet
Author: User

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, and a preferred stall they would like to Occup Y. 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 this remains unoccupied after all th E cows has returned to the barn. Notice 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 are specified in a concise format us ing k lines (1 <= k <=) 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 is is at most N-1. Cows can added to the same stall by several of these lines.

Output

* Line 1:the Minimum index of an unoccupied stall.

Sample Input
10 33 2 2 42 1 0 11 1 1 7
Sample Output
5
Hint

In the sample, there is stalls in the barn, numbered 0..9. The second line of input states that 3 cows prefer stall (2*1+4) mod = 6, and 3 cows prefer stall (2*2+4) mod 10 = 8. The third line States 2 cows prefer stall (0*1+1) mod 10 = 1. Line four specifies the 1 cow prefers stall (1*1+7) mod = 8 (So a total of 4 cows prefer this stall). All stalls'll end up occupied except stall 5.

Problem Source

2015 The second game of every Monday

Let's say that each stall can put countless cows, so that each cow lives in the stall they like. And then from the first
0 stall begin processing each stall: As long as there are more than 1 cows in the first I stall, the cows will be transferred
To the first i+1 stall.


After a round of processing, only a few cows with more than 1 n-1 stall are left. If the first n-1 stall cows
More than 1, we continue to transfer the extra cows to the No. 0 stall so that we can deal with them again, we will ensure that each
Stall up to 1 cows. Finally, check to see which one is the smallest stall without cows. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.