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

来源:互联网 发布:汉仪字体下载包 mac 编辑:程序博客网 时间:2024/05/27 00:48
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 aredistinct.

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 ai1ai2, ... , 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
261 23 4 5
output
2 1 4 3
input
34870603831 161856845957 794650 97697783847 50566 691206 498447698377 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 1234 will be2143 respectively.

这道题的意思是给你N个人,后面是个三角形,n下面从第2行开始算起,第 i 行第 j 个表示第 i 个人和第 j 个人组合在一起的力量值,因为每个人都想要获得最大的力量值,问你要怎么组合才能让双方都满意。

其实做法很简单,就是把输入的值从大到小排列好,然后就可以知道他们的伙伴是谁了,这道题的关键是数组要开多大,因为没有看好题目,RE了四发,后来发现,按照我代码里的输入方法,极限数据是800*799/2,因为第一个for循环的最大值是2*n,第二个for循环的最大值是2*n-1,所以叠在一起就是800*799,又因为没有重复组队,所以还要除以2,这样的话,结构体的数组就要开到319600。

#include<iostream>#include<string.h>#include<stdio.h>#include<algorithm>using namespace std;struct node{int x;int y;int a;};bool cmp(node c,node d){return c.a>d.a;}node num[400000];int ans[1000];int flag[1000];int main(){int n;scanf("%d",&n);int k=0;for(int i=2;i<=2*n;i++){for(int j=1;j<=i-1;j++){scanf("%d",&num[k].a);num[k].x=i;num[k].y=j;k++;}}sort(num,num+k,cmp);for(int i=0;i<k;i++){if(!flag[num[i].x]&&!flag[num[i].y]){ans[num[i].x]=num[i].y;ans[num[i].y]=num[i].x;flag[num[i].x]=1;flag[num[i].y]=1;}}int f=0;for(int i=1;i<=2*n;i++){if(f)  printf(" %d",ans[i]);else{f=1;printf("%d",ans[i]);} }printf("\n");return 0;}


0 0
原创粉丝点击