cf #320 B. Finding Team Member(优先队列)
2015-11-10 16:12
357 查看
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
output
input
output
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。。。。
View Code
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。。。。
/************************************************************************* > File Name: code/cf/#320/B.cpp > Author: 111qqz > Email: rkz2013@126.com > Created Time: 2015年11月10日 星期二 15时47分29秒 ************************************************************************/ #include<iostream> #include<iomanip> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<string> #include<map> #include<set> #include<queue> #include<vector> #include<stack> #include<cctype> #define fst first #define sec second #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define ms(a,x) memset(a,x,sizeof(a)) using namespace std; const double eps = 1E-8; const int dx4[4]={1,0,0,-1}; const int dy4[4]={0,-1,1,0}; typedef long long LL; const int inf = 0x3f3f3f3f; const int N = 8E2+7; int n; int ans ; struct node { int x,y; int val; bool operator<(const node &a)const{ return val<a.val; } }e; int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif ms(ans,-1); scanf("%d",&n); priority_queue<node>q; for ( int i = 2 ; i <= 2*n ; i++) for ( int j = 1 ; j < i ; j++) { int tmp; scanf("%d",&tmp); e.x = i; e.y = j; e.val = tmp; q.push(e); } while (!q.empty()) { node pre = q.top();q.pop(); // cout<<"val:"<<pre.val<<endl; if (ans[pre.x]==-1&&ans[pre.y]==-1) { ans[pre.x] = pre.y; ans[pre.y] = pre.x; } } for ( int i = 1 ; i <= 2*n ; i++) if (i!=2*n) printf("%d ",ans[i]); else printf("%d\n",ans[i]); #ifndef ONLINE_JUDGE #endif fclose(stdin); return 0; }
View Code
相关文章推荐
- css 菜单搜索栏 随滚动条滚动 浮起来
- JasperReport学习笔记3-在浏览器生成PDF文件
- d3 词云 重叠问题
- QtoolBox的使用
- C#之数据类型转换
- MySQL中文乱码解决方案
- 在GridView中设置每个单元格的数据
- JedisPool高并发
- 使用Visual Leak Detector检查内存泄露
- 黑马程序员——基础知识——集合
- Oracle中给表主键、外键收集
- android--sqlite数据库创建的真正时候
- TensorFlow试用
- 从头开发技巧之全局获取Context
- 机房收费系统----类图
- codeforces 587B(dp)
- JDBC PreparedStatement & Statement
- hadoop安装之-hbase
- JS模拟事件操作
- [JQuery]几个易混淆概念的总结