• cf #320 B. Finding Team Member(优先队列)


    B. Finding Team Member
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 test(s)
    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
    Note

    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, 4will be 2, 1, 4, 3 respectively.

    对于这种要频繁求最大值的题,首先想到优先队列。。

    要注意,一定要定义cmp函数。。。没有默认。

    还有一点。。。优先队列的cmp函数的符号是相反的。。。就是如果要大的在前面。。。那么要定义成 a<b。。。。

     1 /*************************************************************************
     2     > File Name: code/cf/#320/B.cpp
     3     > Author: 111qqz
     4     > Email: rkz2013@126.com 
     5     > Created Time: 2015年11月10日 星期二 15时47分29秒
     6  ************************************************************************/
     7 
     8 #include<iostream>
     9 #include<iomanip>
    10 #include<cstdio>
    11 #include<algorithm>
    12 #include<cmath>
    13 #include<cstring>
    14 #include<string>
    15 #include<map>
    16 #include<set>
    17 #include<queue>
    18 #include<vector>
    19 #include<stack>
    20 #include<cctype>
    21 #define fst first              
    22 #define sec second      
    23 #define lson l,m,rt<<1
    24 #define rson m+1,r,rt<<1|1
    25 #define ms(a,x) memset(a,x,sizeof(a))
    26 using namespace std;
    27 const double eps = 1E-8;
    28 const int dx4[4]={1,0,0,-1};
    29 const int dy4[4]={0,-1,1,0};
    30 typedef long long LL;
    31 const int inf = 0x3f3f3f3f;
    32 const int N = 8E2+7;
    33 int n;
    34 int ans[N];
    35 struct node
    36 {
    37     int x,y;
    38     int val;
    39     bool operator<(const node &a)const{
    40     return val<a.val;
    41     }
    42 }e;
    43 int main()
    44 {
    45   #ifndef  ONLINE_JUDGE 
    46    freopen("in.txt","r",stdin);
    47   #endif
    48    
    49    ms(ans,-1);
    50    scanf("%d",&n);
    51    priority_queue<node>q;
    52    for ( int i = 2 ; i <= 2*n ; i++)
    53        for ( int j = 1 ; j < i ; j++)
    54        {
    55        int tmp;
    56        scanf("%d",&tmp);
    57        e.x = i;
    58        e.y = j;
    59        e.val = tmp;
    60        q.push(e);
    61        }
    62 
    63    while (!q.empty())
    64     {
    65     node pre = q.top();q.pop();
    66 //    cout<<"val:"<<pre.val<<endl;
    67 
    68     if (ans[pre.x]==-1&&ans[pre.y]==-1)
    69     {
    70         ans[pre.x] = pre.y;
    71         ans[pre.y] = pre.x;
    72     }
    73     }
    74     for ( int i = 1 ; i <= 2*n ; i++)
    75     if (i!=2*n) printf("%d ",ans[i]);
    76     else printf("%d
    ",ans[i]);
    77   
    78    
    79  #ifndef ONLINE_JUDGE  
    80   #endif
    81   fclose(stdin);
    82     return 0;
    83 }
    View Code
  • 相关阅读:
    selenium判断元素是否为空
    charles 设置弱网测试(转)
    python爬虫初步认知
    Ubuntu运行*.sh文件出现 bash: ./a.sh: /bin/bash^M: bad interpreter: No such file or directory问题解决方案
    Unity3D【安装...........】
    世界上最早的区块链——中国麻将【........】
    Cypher 概述与基本语法
    Win10系统还原与恢复出厂教程【启动系统还原功能】
    增加首页音乐播放器APlayer
    Fabric 持久化
  • 原文地址:https://www.cnblogs.com/111qqz/p/4953383.html
Copyright © 2020-2023  润新知