Day 4: Coins In The Middle

3.8

25 votes
Medium
Problem

Pakkhu is a 3rd year student of NIT, Bhopal and also a generous senior. He has a total of C coins with him today. Out of which he has organised a game for his intelligent juniors.

He gathers M juniors and makes all of them sit in a circle and assigns them an ID (1,2,3….M). Some c (1<=c<=C) coins are kept in the centre. Junior with ID P starts the game. Each player in his turn can pick atmost K coins (and minimum 1) from the centre. Let the player who in his turn picks all the coins left, has ID W. Points are then awarded in the following manner :

 

Junior with ID W gets M points

Junior with ID W+1 gets M-1 points

Junior with ID W+2 gets M-2 points

.

.

.

Junior with ID W-1 gets 1 point

(..because the juniors are sitting in a circle)

 

A total of R rounds like this will be played. Your job is to find the ID of the player who has the highest total points and his/her total points after all rounds have been played. If two players have the same points, then the player with lower ID will be declared as the winner. Assume that each junior makes every move so as trying to maximize his/her own points.

INPUT

  • The first line of input contains a single integer T denoting the number of test cases.

  • The first line of each test case contains 4 space separated integers - C, M, R, K.

  • Next, R lines contain two space-separated integers - P, c.

 

OUTPUT

  • The output of each test case will be two integers - ID of junior with maximum points, and his points at the end of R rounds.

CONSTRAINTS

  • 1   100

  • 1   109

  • 1   5*103

  • 1   3*103

  • 1    109

  • 1   M

  • 1  c   C

Sample Input
2
5 4 2 2
1 1
2 2
5 5 1 2
1 3
Sample Output
2 7
2 5
Time Limit: 0.5
Memory Limit: 256
Source Limit:
Explanation

Test Case 1 - We only have one coin in the centre which will be picked by junior 1 in his turn. Points of juniors after this round will be -

 

Junior - 1 2 3 4

Points - 4 3 2 1

 

We have two coins in the centre which will be picked by junior 2 in his turn. Points of juniors after this round will be -

 

Junior - 1 2 3 4

Points - 5 7 5 3

 

So the winner after all the rounds have been played is Junior 2 with 7 points.

 

 

Test Case 2 - No matter how many coins junior 1 picks in his turn, junior 2 will take all the remaining coins.Points of juniors after this round will be -

 

Junior - 1 2 3 4 5

Points - 1 5 4 3 2

 

So the winner, after all the rounds have been played, is Junior with ID 2 with 5 points.

Editor Image

?