Team Arrangement
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 207 Accepted Submission(s): 74
Problem Description
Barry Bennett, the coach of the Bings football team, wants to arrange his team for an important match against the Bangs. He decides on the formation he wants to play, for example 4-4-2, meaning that there will be four defenders, four midfielders, and two strikers (and of course, one goalkeeper). Your task is to determine the players who will play. For each available player, we know his role (e.g. midfielder). For each role, the players are selected in ascending order of their numbers. When the players are selected, you must determine the captain too, who is the player with the longest record in the team play. In case two players have the same record, the one with bigger number is chosen. Note that the captain is chosen among the players that are selected in the arrange.
Input
The input consists of multiple test cases. The first 22 lines of each test case contain the data for the 22 available players in this format:
number name role year1–year'1 year2–year'2 ...
number is the player number (unique positive integer less than 100). name is a string of at most 20 letters. role is a single character among G, D, M, S, for goalkeeper, defender, midfielder, and striker respectively. Each yeari –year'i pair ( yeari ≤ year'i) shows the player has been a member of the team between the specified years (inclusive). The years are in four-digit format. There is at least one and at most 20 such pairs, and the same year is not repeated more than once in the list. There is a 23rd line describing the desired formation, like 4-4-2 in that format. Note that there are only three numbers in the formation (so, 4-3-2-1 is not valid), none of them is zero, and their sum is always 10. The input is terminated by a line containing a single 0.
number name role year1–year'1 year2–year'2 ...
number is the player number (unique positive integer less than 100). name is a string of at most 20 letters. role is a single character among G, D, M, S, for goalkeeper, defender, midfielder, and striker respectively. Each yeari –year'i pair ( yeari ≤ year'i) shows the player has been a member of the team between the specified years (inclusive). The years are in four-digit format. There is at least one and at most 20 such pairs, and the same year is not repeated more than once in the list. There is a 23rd line describing the desired formation, like 4-4-2 in that format. Note that there are only three numbers in the formation (so, 4-3-2-1 is not valid), none of them is zero, and their sum is always 10. The input is terminated by a line containing a single 0.
Output
For each test case, output a list of 11 players chosen in the arrange. Each line must contain the player number, his name and his role, separated by single blank characters. The players must be sorted according to their role, in the order of goalkeeper, defenders, midfielders, and strikers. The players with the same role are sorted according to ascending order of their numbers. There is an exception that the captain always comes as the first player in the entire list. If it is not possible to arrange the team conforming to the desired formation, write a single line containing IMPOSSIBLE TO ARRANGE in the output. There should be a blank line after the output for each test case.
Sample Input
9 PlayerA M 2000-2001 2003-2006 2 PlayerB M 2004-2006 10 PlayerC D 2001-2005 1 PlayerD D 2000-2001 2002-2004 11 PlayerE S 2003-2006 8 PlayerF M 2005-2006 22 PlayerG S 2005-2006 25 PlayerH G 2000-2001 2002-2003 2005-2006 6 PlayerI D 2003-2006 26 PlayerJ D 2003-2004 2000-2001 18 PlayerK M 2003-2004 19 PlayerL M 2000-2001 2003-2006 7 PlayerM S 2003-2006 1999-2001 21 PlayerN S 2003-2006 13 PlayerO S 2005-2006 15 PlayerP G 2001-2006 14 PlayerQ D 2003-2004 5 PlayerR S 2000-2005 20 PlayerS G 2000-2002 2003-2003 12 PlayerT M 2004-2005 3 PlayerU D 2000-2005 4 PlayerV M 2001-2004 4-4-2 0
Sample Output
7 PlayerM S 15 PlayerP G 1 PlayerD D 3 PlayerU D 6 PlayerI D 10 PlayerC D 2 PlayerB M 4 PlayerV M 8 PlayerF M 9 PlayerA M 5 PlayerR S
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <limits.h>
#include <ctype.h>
#include <string.h>
#include <string>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <deque>
#include <vector>
#include <set>
#include <map>
#define MAXN 22
using namespace std;
struct node1
{
int num1;
char name1[50];
int mark1;
int time1;
} a[MAXN];
struct node2
{
int num2;
char name2[50];
int mark2;
int time2;
} b[MAXN];
struct node3
{
int num3;
char name3[50];
int mark3;
int time3;
} c[MAXN];
struct node4
{
int num4;
char name4[50];
int mark4;
int time4;
} d[MAXN];
bool cmp1(node1 a,node1 b)
{
return a.num1 < b.num1;
}
bool cmp2(node2 a,node2 b)
{
return a.num2 < b.num2;
}
bool cmp3(node3 a,node3 b)
{
return a.num3 < b.num3;
}
bool cmp4(node4 a,node4 b)
{
return a.num4 < b.num4;
}
struct node
{
int num5;
char name5[40];
int mark5;
int time5;
} ans[MAXN];
bool cmp5(node a,node b)
{
if(a.time5 == b.time5){
return a.num5 > b.num5;
}
return a.time5 > b.time5;
}
int main()
{
int numa;
char fname[50];
char wei[5];
//int t;
int ta,tb,tc;
int tmp,tmp1[10];
int i,j,k;
int NUM[10];
int ii;
char s[110];
while(1)
{
memset(NUM,0,sizeof(NUM));
int flag = 1;
int count1=0;
int count2=0;
int count3=0;
int count4=0;
scanf("%d",&numa);
if(numa == 0)
{
break;
}
else
{
for(ii=0; ii<22; ii++)
{
if(flag != 1)
{
scanf("%d",&numa);
}
flag = 0;
int sum;
//scanf("%d",&numa);
scanf("%s",fname);
scanf("%s",wei);
getchar();
gets(s);
int len = strlen(s);
k = 0;
tmp = 0;
sum = 0;
for(j=0; j<len; j++)
{
if(s[j]=='-' || s[j]==' ')
{
tmp1[++k] = tmp;
if(k == 2)
{
sum+=tmp1[k]-tmp1[k-1]+1;
k = 0;
}
tmp = 0;
}
else
tmp = tmp*10+(s[j]-'0');
}
sum+=tmp-tmp1[1]+1;
// printf("%d\n",sum);
if(wei[0] == 'G')
{
a[count1].num1 = numa;
strcpy(a[count1].name1,fname);
a[count1].mark1 = 1;
a[count1].time1 = sum;
count1++;
}
if(wei[0] == 'D')
{
b[count2].num2 = numa;
strcpy(b[count2].name2,fname);
b[count2].mark2 = 2;
b[count2].time2 = sum;
count2++;
}
if(wei[0] == 'M')
{
c[count3].num3 = numa;
strcpy(c[count3].name3,fname);
c[count3].mark3 = 3;
c[count3].time3 = sum;
count3++;
}
if(wei[0] == 'S')
{
d[count4].num4 = numa;
strcpy(d[count4].name4,fname);
d[count4].mark4 = 4;
d[count4].time4 = sum;
count4++;
}
}
scanf("%d-%d-%d",&ta,&tb,&tc);
sort(a,a+count1,cmp1);
sort(b,b+count2,cmp2);
sort(c,c+count3,cmp3);
sort(d,d+count4,cmp4);
if(count1==0 || count2<ta || count3<tb || count4<tc)
{
printf("IMPOSSIBLE TO ARRANGE\n");
}
else
{
int ff = 0;
ans[ff].num5 = a[0].num1;
strcpy(ans[ff].name5,a[0].name1);
ans[ff].mark5 = a[0].mark1;
ans[ff].time5 = a[0].time1;
ff++;
for(i=0; i<ta; i++)
{
ans[ff].num5 = b[i].num2;
strcpy(ans[ff].name5,b[i].name2);
ans[ff].mark5 = b[i].mark2;
ans[ff].time5 = b[i].time2;
ff++;
}
for(i=0; i<tb; i++)
{
ans[ff].num5 = c[i].num3;
strcpy(ans[ff].name5,c[i].name3);
ans[ff].mark5 = c[i].mark3;
ans[ff].time5 = c[i].time3;
ff++;
}
for(i=0; i<tc; i++)
{
ans[ff].num5 = d[i].num4;
strcpy(ans[ff].name5,d[i].name4);
ans[ff].mark5 = d[i].mark4;
ans[ff].time5 = d[i].time4;
ff++;
}
sort(ans,ans+11,cmp5);
/*for(i=0; i<11; i++)
{
printf("%d\n",ans[i].time5);
}*/
NUM[1] = 1;
NUM[2] = ta;
NUM[3] = tb;
NUM[4] = tc;
if(ans[0].mark5 == 1)
{
printf("%d %s G\n",ans[0].num5,ans[0].name5);
NUM[1]--;
}
if(ans[0].mark5 == 2)
{
printf("%d %s D\n",ans[0].num5,ans[0].name5);
NUM[2]--;
}
if(ans[0].mark5 == 3)
{
printf("%d %s M\n",ans[0].num5,ans[0].name5);
NUM[3]--;
}
if(ans[0].mark5 == 4)
{
printf("%d %s S\n",ans[0].num5,ans[0].name5);
NUM[4]--;
}
count1 = 0;
count2 = 0;
count3 = 0;
count4 = 0;
for(i=1; i<11; i++)
{
if(ans[i].mark5 == 1)
{
a[count1].mark1 = ans[i].mark5;
a[count1].num1 = ans[i].num5;
strcpy(a[count1].name1,ans[i].name5);
a[count1].time1 = ans[i].time5;
count1++;
}
if(ans[i].mark5 == 2)
{
b[count2].mark2 = ans[i].mark5;
b[count2].num2 = ans[i].num5;
strcpy(b[count2].name2,ans[i].name5);
b[count2].time2 = ans[i].time5;
count2++;
}
if(ans[i].mark5 == 3)
{
c[count3].mark3 = ans[i].mark5;
c[count3].num3 = ans[i].num5;
strcpy(c[count3].name3,ans[i].name5);
c[count3].time3 = ans[i].time5;
count3++;
}
if(ans[i].mark5 == 4)
{
d[count4].mark4 = ans[i].mark5;
d[count4].num4 = ans[i].num5;
strcpy(d[count4].name4,ans[i].name5);
d[count4].time4 = ans[i].time5;
count4++;
}
}
sort(a,a+count1,cmp1);
sort(b,b+count2,cmp2);
sort(c,c+count3,cmp3);
sort(d,d+count4,cmp4);
for(i=0; i<NUM[1]; i++)
{
printf("%d %s G\n",a[i].num1,a[i].name1);
}
for(i=0; i<NUM[2]; i++)
{
printf("%d %s D\n",b[i].num2,b[i].name2);
}
for(i=0; i<NUM[3]; i++)
{
printf("%d %s M\n",c[i].num3,c[i].name3);
}
for(i=0; i<NUM[4]; i++)
{
printf("%d %s S\n",d[i].num4,d[i].name4);
}
}
}
printf("\n");
}
return 0;
}