Codeforces Round #257 (Div. 2) A. Jzzhu and Children
There are n children in Jzzhu's school. Jzzhu is going to give some candies to them. Let's number all the children from 1 to n. The i-th child wants to get at least ai candies.
Jzzhu asks children to line up. Initially, the i-th child stands at the i-th place of the line. Then Jzzhu start distribution of the candies. He follows the algorithm:
- Give m candies to the first child of the line.
- If this child still haven't got enough candies, then the child goes to the end of the line, else the child go home.
- Repeat the first two steps while the line is not empty.
Consider all the children in the order they go home. Jzzhu wants to know, which child will be the last in this order?
The first line contains two integers n, m (1 ≤ n ≤ 100; 1 ≤ m ≤ 100). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100).
Output a single integer, representing the number of the last child.
5 2
1 3 1 4 2
4
6 4
1 1 2 2 3 3
6
Let's consider the first sample.
Firstly child 1 gets 2 candies and go home. Then child 2 gets 2 candies and go to the end of the line. Currently the line looks like [3, 4, 5, 2] (indices of the children in order of the line). Then child 3 gets 2 candies and go home, and then child 4 gets 2 candies and goes to the end of the line. Currently the line looks like [5, 2, 4]. Then child 5 gets 2 candies and goes home. Then child 2 gets two candies and goes home, and finally child 4 gets 2 candies and goes home.
Child 4 is the last one who goes home.
1 #include<iostream> 2 #include<string.h> 3 #include<stdio.h> 4 #include<ctype.h> 5 #include<algorithm> 6 #include<stack> 7 #include<queue> 8 #include<set> 9 #include<math.h> 10 #include<vector> 11 #include<map> 12 #include<deque> 13 #include<list> 14 using namespace std; 15 16 struct person 17 { 18 int id,cd; 19 }; 20 person tem; 21 22 queue<person> q; 23 24 int main() 25 { 26 int n,m,a[110]; 27 scanf("%d%d", &n, &m); 28 for(int i = 1 ; i <= n ; i++) scanf("%d", &a[i]); 29 30 while(!q.empty()) q.pop(); 31 32 for(int i = 1 ; i <= n ; i++) 33 { 34 tem.id=i; 35 tem.cd=0; 36 q.push(tem); 37 } 38 39 while(q.size()>1) 40 { 41 tem=q.front(); 42 q.pop(); 43 tem.cd+=m; 44 if(tem.cd<a[tem.id]) q.push(tem); 45 } 46 47 tem=q.front(); 48 printf("%d\n", tem.id); 49 50 return 0; 51 }