Codeforces Round #320 (Div. 2) 579B Finding Team Member

来源:互联网 发布:淘宝网天猫女手提包 编辑:程序博客网 时间:2024/05/24 23:14

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?


介个题目一开始就是说 有2n个人选队友,给出了下三角矩阵来表示每两个人之间的分数。

然后题目又说每个人都要选当前可选择的人里面和自己合作分数最高的

浴室胡,就是看如何去找最大分数的一对了。

如果每一次都遍历矩阵去找最大的,那莫对稍微大一点的n都是妥妥的超时的。但是这个题目一就是不用多次便利的,其实一次都不用。

窝把记录的下三角矩阵 记成有i到j的一条无向有权值的边,这个题目就成了找当前可以选择的最大边了

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;struct Team{    int u,v,c;};bool operator < (const Team &a, const Team &b ) {    return a.c > b.c;}Team p[1000000];int vis[1000];int n;int main(){    scanf("%d",&n);    for(int i = 1; i <= 2*n; i++) vis[i] = 0;    int cnt = 1;    for(int i = 2; i <= 2*n; i++)    {        for(int j = 1; j < i; j++)        {            p[cnt].u = i;            p[cnt].v = j;            scanf("%d",&p[cnt++].c);        }    }    sort(p+1,p+cnt+1);    for(int i = 1; i <= cnt; i++){        if(!vis[p[i].u]&&!vis[p[i].v])        {            vis[p[i].u] = p[i].v;            vis[p[i].v] = p[i].u;        }    }    for(int i = 1; i < 2*n; i++)    {        printf("%d ",vis[i]);    }    printf("%d\n",vis[2*n]);    return 0;}


0 0