Here’s the second problem of the qualification round:
———–
A group of N high school students wants to play a basketball game. To divide themselves into two teams they first rank all the players in the following way:
Players with a higher shot percentage are rated higher than players with a lower shot percentage.
If two players have the same shot percentage, the taller player is rated higher.
Luckily there are no two players with both the same shot percentage and height so they are able to order themselves in an unambiguous way. Based on that ordering each player is assigned a draft number from the range [1..N], where the highest-rated player gets the number 1, the second highest-rated gets the number 2, and so on. Now the first team contains all the players with the odd draft numbers and the second team all the players with the even draft numbers.
Each team can only have P players playing at a time, so to ensure that everyone gets similar time on the court both teams will rotate their players according to the following algorithm:
Each team starts the game with the P players who have the lowest draft numbers.
If there are more than P players on a team after each minute of the game the player with the highest total time played leaves the playing field. Ties are broken by the player with the higher draft number leaving first.
To replace her the player on the bench with the lowest total time played joins the game. Ties are broken by the player with the lower draft number entering first.
The game has been going on for M minutes now. Your task is to print out the names of all the players currently on the field, (that is after M rotations).
Input
The first line of the input consists of a single number T, the number of test cases.
Each test case starts with a line containing three space separated integers N M P
The subsequent N lines are in the format “
Constraints
1 ≤ T ≤ 50
2 * P ≤ N ≤ 30
1 ≤ M ≤ 120
1 ≤ P ≤ 5
Each name starts with an uppercase English letter, followed by 0 to 20 lowercase English letters. There can be players sharing the same name. Each shot percentage is an integer from the range [0..100]. Each height is an integer from the range [100..240]
Output
For each test case i numbered from 1 to T, output “Case #i: “, followed by 2 * P space separated names of the players playing after M rotations. The names should be printed in lexicographical order.
Example
In the first example if you sort all the players by their shot percentage you get the list: [Wai, Purav, Weiyan, Slawek, Lin, Meihong]. This makes the two teams:
[Wai, Weiyan, Lin]
[Purav, Slawek, Meihong]
The game starts with Lin and Meihong sitting on the bench in their respective teams. After the first minute passes it’s time for Weiyan and Slawek to sit out since they have the highest draft numbers of the people who played. After the second minute passes Lin and Meihong will keep playing since they only played one minute so far and it’s Wai and Purav who have to sit out.
Finally after the third minute Lin and Maihong go back to the bench and all the players currently playing again are:
Purav Slawek Wai Weiyan
Sample Input
5
6 3 2
Wai 99 131
Weiyan 81 155
Lin 80 100
Purav 86 198
Slawek 80 192
Meihong 44 109
7 93 2
Paul 82 189
Kittipat 62 126
Thomas 17 228
Fabien 57 233
Yifei 65 138
Liang 92 100
Victor 53 124
6 62 3
Meihong 33 192
Duc 62 162
Wai 70 148
Fabien 19 120
Bhuwan 48 176
Vlad 30 225
8 59 3
Anil 38 180
Song 7 187
David 65 159
Lin 45 121
Ranjeeth 39 183
Torbjorn 26 181
Clifton 57 158
Phil 3 183
4 72 1
Anh 2 187
Erling 69 226
Purav 0 199
Zejia 29 163
Sample Output
Case #1: Purav Slawek Wai Weiyan
Case #2: Fabien Kittipat Liang Paul
Case #3: Bhuwan Duc Fabien Meihong Vlad Wai
Case #4: Anil Lin Phil Ranjeeth Song Torbjorn
Case #5: Erling Zejia
———
My Solution
Basically I created a simulation of the game to find the correct answer in each case.
#include <stdio.h>
#include <string.h>
void sortArray(char names[][30], int size){
char buffer[30];
int i,j;
int smallest;
int res;
for(i=0;i<size-1;i++){
strcpy(buffer,"zzzzz");
for(j=i;j<size;j++){
res = strcmp(buffer,names[j]);
if(res>0){
strcpy(buffer,names[j]);
smallest = j;
}
}
strcpy(names[smallest],names[i]);
strcpy(names[i],buffer);
}
return;
}
int lowestDraft(int table[][2],int nPlayers){
int i;
int shotMax = 0;
int heightMax = 0;
int index;
for(i=0;i<nPlayers;i++){
if(table[i][0]>shotMax){
shotMax = table[i][0];
heightMax = table[i][1];
index = i;
}
else if(table[i][0]==shotMax){
if(table[i][1]>heightMax){
shotMax = table[i][0];
heightMax = table[i][1];
index = i;
}
}
}
table[index][0] = 0;
table[index][1] = 0;
return index;
}
int findPlayer(int option, int teamTable[][4], int players){
int i;
int maxTime;
int maxDraft;
int index;
/*find player to go out*/
if(option==1){
maxTime = 0;
maxDraft = 0;
for(i=0;i<players;i++){
if(teamTable[i][2]==1){
if(teamTable[i][3]>maxTime){
maxTime = teamTable[i][3];
maxDraft = teamTable[i][1];
index = i;
}
else if(teamTable[i][3]==maxTime){
if(teamTable[i][1]>maxDraft){
maxTime = teamTable[i][3];
maxDraft = teamTable[i][1];
index = i;
}
}
}
}
return index;
}
/*find player to go in*/
else if(option==2){
maxTime = 1000;
maxDraft = 1000;
for(i=0;i<players;i++){
if(teamTable[i][2]==0){
if(teamTable[i][3]<maxTime){
maxTime = teamTable[i][3];
maxDraft = teamTable[i][1];
index = i;
}
else if(teamTable[i][3]==maxTime){
if(teamTable[i][1]<maxDraft){
maxTime = teamTable[i][3];
maxDraft = teamTable[i][1];
index = i;
}
}
}
}
return index;
}
else
return -1;
}
int main(){
int nPlayers, minutes, maxPlayers;
int t,k;
char names[30][30];
int percentHeight[30][2];
int i;
int teamA[15][4];
int teamB[15][4];
int playersA,playersB;
int index;
int draftNumber;
int indexA,indexB;
int x;
int playerOut,playerIn;
char finalPlayers[10][30];
int finalIndex;
int w;
scanf("%d",&t);
for(k=0;k<t;k++){
finalIndex = 0;
playersA = 0;
playersB = 0;
draftNumber = 1;
indexA = 0;
indexB = 0;
/*read input data*/
scanf("%d %d %d",&nPlayers,&minutes,&maxPlayers);
for(i=0;i<nPlayers;i++){
scanf("%s",names[i]);
scanf("%d %d",&percentHeight[i][0],&percentHeight[i][1]);
}
/*distribute plaeyers into teams A and B*/
for(i=0;i<nPlayers;i++){
index = lowestDraft(percentHeight,nPlayers);
if(i%2==0){
teamA[indexA][0] = index;
teamA[indexA][1] = draftNumber;
teamA[indexA][2] = 0;
teamA[indexA][3] = 0;
indexA++;
draftNumber++;
playersA++;
}
else{
teamB[indexB][0] = index;
teamB[indexB][1] = draftNumber;
teamB[indexB][2] = 0;
teamB[indexB][3] = 0;
indexB++;
draftNumber++;
playersB++;
}
}
/*select playes who begin playing*/
for(i=0;i<maxPlayers;i++){
teamA[i][2] = 1;
teamB[i][2] = 1;
}
/*run game*/
for(i=0;i<minutes;i++){
//printf("-----After %d minutes------\n",i+1);
/*do changes in teamA*/
for(x=0;x<playersA;x++){
if(teamA[x][2])
teamA[x][3]++;
}
if(playersA>maxPlayers){
playerOut = findPlayer(1,teamA,playersA);
playerIn = findPlayer(2,teamA,playersA);
teamA[playerOut][2] = 0;
teamA[playerIn][2] = 1;
}
/*do changes in teamB*/
for(x=0;x<playersB;x++){
if(teamB[x][2])
teamB[x][3]++;
}
if(playersB>maxPlayers){
playerOut = findPlayer(1,teamB,playersB);
playerIn = findPlayer(2,teamB,playersB);
teamB[playerOut][2] = 0;
teamB[playerIn][2] = 1;
}
}
/*get list of players in the court*/
for(i=0;i<playersA;i++)
if(teamA[i][2])
strcpy(finalPlayers[finalIndex++],names[teamA[i][0]]);
for(i=0;i<playersB;i++)
if(teamB[i][2])
strcpy(finalPlayers[finalIndex++],names[teamB[i][0]]);
sortArray(finalPlayers,finalIndex);
if(k>0)
printf("\n");
printf("Case #%d: ",k+1);
for(i=0;i<finalIndex;i++)
printf(" %s",finalPlayers[i]);
}
return 0;
}
Hi,
Thanks for sharing your code. it looks like having good size of sub functions.
I get a different results for some cases;
Case#1: Wai Weiyan Purav Slawek
Case#2: Paul Kittipat Thomas Fabien
Case#3: Meihong Fabien Bhuwan Vlad
Case#4: Song David Lin Ranjeeth Clifton Phil
Case#5: Erling Zejia
Initial issue of my program was that I looked for in players including just got out ones.
I checked your program…it looks like not have the same issue, but it is still strange. You may consider see the output at each minute.
Sincerely,
Woon Ho “Cloud” Cho