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

来源:互联网 发布:java xe hystrix 编辑:程序博客网 时间:2024/05/19 02:18

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个人找队友 ,每个人会找当前能找的和自己搭配分数最高的人; 开始想的时候觉得介个题目是有点复杂,好像怎么搞都不对;

后来发现, 窝还是太native。 其实只要对已有的配对,直接拿当前最大的一对 ,他们肯定会配对

蓝儿,给的下三角矩阵不可能每次都遍历最大,浴室胡 ,窝把下三角处理成连接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
原创粉丝点击