POJ 3339 & HDU 2409 Team Arrangement(结构体)

题目链接:

PKU:http://poj.org/problem?id=3339

HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2409

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 year1year‘1 year2year‘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

Source

Tehran 2006

题意:

给出22个球员的各种信息,要求按照给出的阵形选择球员和队长,给出的阵形连守门员一共十一人;

选择球员的规则是:同角色的球员按照编号从小到大选择,直到选够此角色的人数;

选择队长的规则是:在所有已经被选择为某角色的球员中,选择服役时间最长的那个球员,

如果服役时间时间相同,就选择编号较大的那一个球员;

PS:此题有点麻烦!

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
struct ROLE
{
    int num;
    char name[47];
    char R;
    int time;
    int flag;
} p[47];
bool cmp1(ROLE a, ROLE b)
{
    return a.num < b.num;
}
bool cmp2(ROLE a, ROLE b)
{
    return a.flag < b.flag;
}
int main()
{
    char s[700];
    while(scanf("%d",&p[0].num)!=EOF)
    {
        if(p[0].num == 0)
            break;
        for(int i = 0; i < 27; i++)
            p[i].flag = 0;
        for(int i = 0; i < 22; i++)
        {
            memset(s,'\0',sizeof(s));
            int sum = 0;
            if(i > 0)
                scanf("%d",&p[i].num);
            scanf("%s",p[i].name);
            getchar();
            scanf("%c",&p[i].R);
            gets(s);
            for(int j = 0; j < strlen(s)-4; j++)
            {
                if(s[j]==' ')
                    continue;
                int t1 = 0, t2 = 0;
                for(int l = j; l < j+4; l++)
                {
                    t1=t1*10+s[l]-'0';
                }
                j+=4;
                if(s[j] == '-')
                {
                    for(int l = j+1; l < j+5; l++)
                    {
                        t2=t2*10+s[l]-'0';
                    }
                }
                j+=5;
                sum+=t2-t1+1;
            }
            p[i].time = sum;
            //printf("%d....\n",sum);
        }
        sort(p,p+22,cmp1);
        char ss[17];
        scanf("%s",ss);
        int a, b, c;
        a = ss[0]-'0';
        b = ss[2]-'0';
        c = ss[4]-'0';
        //printf("%d %d %d\n",a,b,c);
        int l = 1;
        for(int i = 0; i < 22; i++)
        {
            if(p[i].R == 'G')
            {
                p[i].flag = l;
                l++;
                break;
            }
        }
        for(int i = 0; i < a; i++)
        {
            for(int j = 0; j < 22; j++)
            {
                if(p[j].R == 'D' && p[j].flag==0)
                {
                    p[j].flag = l;
                    l++;
                    break;
                }
            }
        }
        for(int i = 0; i < b; i++)
        {
            for(int j = 0; j < 22; j++)
            {
                if(p[j].R == 'M' && p[j].flag==0)
                {
                    p[j].flag = l;
                    l++;
                    break;
                }

            }
        }
        for(int i = 0; i < c; i++)
        {
            for(int j = 0; j < 22; j++)
            {
                if(p[j].R == 'S' && p[j].flag==0)
                {
                    p[j].flag = l;
                    l++;
                    break;
                }
            }
        }
        if(l!=12 ||(a+b+c!=10))
        {
            printf("IMPOSSIBLE TO ARRANGE\n\n");
            continue;
        }
        sort(p,p+22,cmp2);
        int maxx = -1;
        int pos = 0;
        for(int i = 0; i < 22; i++)
        {
            if(p[i].flag && p[i].time > maxx)
            {
                maxx = p[i].time;
                pos = i;
            }
            else if(p[i].flag && p[i].time == maxx)
            {
                if(p[pos].num < p[i].num)
                    pos = i;
            }
        }
        printf("%d %s %c\n",p[pos].num,p[pos].name,p[pos].R);
        for(int i = 0; i < 22; i++)
        {
            if(p[i].flag && i!=pos)
                printf("%d %s %c\n",p[i].num,p[i].name,p[i].R);
        }
        printf("\n");
    }
    return 0;
}
时间: 2024-10-12 10:56:49

POJ 3339 & HDU 2409 Team Arrangement(结构体)的相关文章

HDU 2409 Team Arrangement (结构体排序)

题目链接:HDU 2409 Team Arrangement 题意:给出22个人(编号,名字,踢的位置,在队里的时间),让我们选DD-MM-SS的阵型+一个守门员.在选出队长(时间在最久的就是队长,时间相同编号大为队长),选人的顺序是从编号小的开始. 结构体排序就好了,注意出输出是按队长,D,M,S的顺序,选队长记录队长的编号(而不是下标,我的代码之后还要排序). AC代码: #include<stdio.h> #include<string.h> #include<algo

【结构体排序】hdu 2409 Team Arrangement

[结构体排序]hdu 2409 Team Arrangement 题目链接:hdu 2409 Team Arrangement 题目大意 给出22个球员的各种信息,要求按照给出的阵形选择球员和队长共11人: 选择球员的规则是:同角色的球员按照编号从小到大选择,直到选够此角色的人数: 选择队长的规则是:在所有已经被选择为某角色的球员中,选择服役时间最长的那个球员, 如果服役时间时间相同,就选择编号较大的那一个球员: 2006 Asia Regional Tehran(伊朗首都德黑兰)的第一题,题意

hdu 2093 考试排名(结构体排序)

对"到文件结束"理解 代码: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct node { char name[20]; int num; int t; void init() { t=0; num=0; } }; int cmp(const node a,const node b) { if(a.num==b.num&&am

HDU 1236 排名 (排序+结构体)

#include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> using namespace std; struct stu { char test[25]; int numm; int right[10]; int total; }student[1001]; int cmp(stu a,stu b) { if(a.total != b.total) retu

hdu 4941 Magical Forest(STL map &amp; 结构体运用)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4941 Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 220    Accepted Submission(s): 105 Problem Description There is a forest c

HDU 1084 [What Is Your Grade?] 结构体排序

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1084 题目大意:共5道题,N个学生.做出5道满分,0道50分.做出1-4道的同学,若在前50%(向下取整),则获得95.85.75.65,若在后50%则获得90.80.70.60. 关键思想:结构体排序 //结构体排序,考虑边界 #include <iostream> #include <algorithm> #include <cmath> #include <me

POJ: 2413 ——优先队列——结构体cmp函数一级排序

我要翻译题目!!! /* A group of cows grabbed a truck and ventured on an  expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to run over a rock and puncture the truck's fuel tank. The truck now leaks one unit of fuel ev

poj 2243 bfs 利用 结构体中的step成员保存步数 ,STL的队列

//BFS #include <iostream> #include <queue> using namespace std; bool used[8][8]; int move[8][2]={1,2, -1,2, -2,1, -2,-1, -1,-2, 1,-2, 2,-1, 2,1}; struct position { int i,j; int step; position(int a,int b,int c) { i=a; j=b; step=c; } }; int mai

HDU 2187 悼念512汶川大地震遇难同胞——老人是真饿了(结构体排序,背包????)

悼念512汶川大地震遇难同胞--老人是真饿了 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11747    Accepted Submission(s): 4883 Problem Description 时间:2008年5月16日(震后第4天) 地点:汶川县牛脑寨 人物:羌族老奶奶 [转载整理]牛脑寨是一个全村600多人的羌族寨子