zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Python并发编程-concurrent包
    Python并发编程-多进程
    Python并发编程-GIL全局解释器锁
    .net解析csv(C#导表工具)
    为游戏适配刘海屏
    Lua rawget rawset newindex 函数定义和例子
    lua_local变量在new时不会被清空
    Lua查找表元素过程(元表、__index方法是如何工作的)
    为什么不使用github的wiki而是使用mkdocs做文档管理?
    Unity重置Animator到初始状态和重复播放同一个Animation
  • 原文地址:https://www.cnblogs.com/111qqz/p/4953383.html
Copyright © 2011-2022 走看看