Codeforces Round #320 (Div. 2) 579B(水)

题目意思如下:
There is a programing contest named SnakeUp, 2n people want to compete for it. In order to attend this contest, people need to form teams of exactly two people. You are given the strength of each possible combination of two people. All the values of the strengths are distinct.

Every contestant hopes that he can find a teammate so that their team’s strength is as high as possible. That is, a contestant will form a team with highest strength possible by choosing a teammate from ones who are willing to be a teammate with him/her. More formally, two people A and B may form a team if each of them is the best possible teammate (among the contestants that remain unpaired) for the other one.

Can you determine who will be each person’s teammate?

Input
There are 2n lines in the input.

The first line contains an integer n (1 ≤ n ≤ 400) — the number of teams to be formed.

The i-th line (i > 1) contains i - 1 numbers ai1, ai2, … , ai(i - 1). Here aij (1 ≤ aij ≤ 106, all aij are distinct) denotes the strength of a team consisting of person i and person j (people are numbered starting from 1.)

Output
Output a line containing 2n numbers. The i-th number should represent the number of teammate of i-th person.

Sample Input
Input
2
6
1 2
3 4 5
Output
2 1 4 3
Input
3
487060
3831 161856
845957 794650 976977
83847 50566 691206 498447
698377 156232 59015 382455 626960
Output
6 5 4 3 2 1
Hint
In the first sample, contestant 1 and 2 will be teammates and so do contestant 3 and 4, so the teammate of contestant 1, 2, 3, 4 will be 2, 1, 4, 3 respectively.

题意:

先输入需要组队的数目
再输入n个人组合形成的
(2, 1)
(3, 1)(3, 2)
(4, 1)(4, 2)(4, 3)
。。。。
6
1 2
3 4 5
上边括号里的数字代表人的序号,下边的组合为strength值

求每个人的最优组队组合;

思路很简单,把组合里的strength值排序,最优组合就是最大的,依次减小。

小贴士: 时间给了2秒,数据范围小,暗示可以暴力查找,也可以sort

#include <stdio.h>
#include <string.h>
#include <algorithm>
const int maxn = 805;
int arr[maxn][maxn];
int a[maxn];
int dx,dy;

using namespace std;

void searchmax(int N){
    int maxx=0;
    for(int i=2;i<=N;i++){
         for(int j=1;j<i;j++){
            if(arr[i][j]>maxx){
                maxx = arr[i][j];
                dx = i;
                dy = j;
            }
         }
    }
}


void clearxy(int i,int j,int N){
    for(int k=1;k<N;k++)
        arr[i][k] = 0;
    for(int k=2;k<=N;k++)
        arr[k][i] = 0;
    for(int k=1;k<N;k++)
        arr[j][k] = 0;
    for(int k=2;k<=N;k++)
        arr[k][j] = 0;

}
void printff(int N)
{
    for(int i=2;i<=N;i++){
        for(int j=1;j<i;j++){
            printf("%d ",arr[i][j]);
        }
    printf("\n");
    }
    printf("\n");
}

int main(){
    int n;
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    while(scanf("%d",&n)!=EOF){
        int N = n*2;
        memset(arr,0,sizeof(arr));
        memset(a,0,sizeof(a));
        for(int i=2;i<=N;i++)
            for(int j=1;j<i;j++)
                scanf("%d",&arr[i][j]);

                int jiafa=0;
                for(int g=0;g<n;g++){
                    searchmax(N);
                    a[dy] = dx;
                    a[dx] = dy;
                    clearxy(dx,dy,N);
                }
                for(int i=1;i<=N;i++)
                    printf("%d ",a[i]);
                printf("\n");

        }
        return 0;
}

个人小结:
额,,,理解完题意了觉得简单。。。但是死坑的我没有理解清楚题意,英语不好吗(哭!=)。。。应该为输出对应序号从1到n的人所最适合的队友。。。。我一直以为是输出最大的几个组合就完了。。。

细节方面:
1)由于最近体会到写自定义函数能让代码看起来更舒服的好处,都尽量写自定义函数。但是有时候由于改动了输入时for的边界值,而忘记改自定义函数里的边界值,导致错误,需要考虑全局

2)数据存储方面,是从0开始存储,输出时再相应的加减下标呢,还是直接存在相应的位置呢,如本题从下标2开始存。 其实应该对应存比较好,本题又是输入2维的数组,应该优先考虑直接按下标存,但不管如何,都要考虑所有调用到这个数组的操作的边界是否符合存储的边界,是否小于边界或者是否越界(数组一般初始化为零,但如果某个比较操作访问到了越界的数据如0,就很容易出错)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值