Sorting
http://acm.hdu.edu.cn/showproblem.php?pid=6281
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2402 Accepted Submission(s): 615
Problem Description
Bobo has n tuples (a1,b1,c1),(a2,b2,c2),…,(an,bn,cn).
He would like to find the lexicographically smallest permutation p1,p2,…,pn of 1,2,…,n such that for i∈{2,3,…,n} it holds that
Input
The input consists of several test cases and is terminated by end-of-file.
The first line of each test case contains an integer n.
The i-th of the following n lines contains 3 integers ai, bi and ci.
Output
For each test case, print n integers p1,p2,…,pn seperated by spaces.
DO NOT print trailing spaces.
## Constraint
* 1≤n≤10^3
* 1≤ai,bi,ci≤2×10^9
* The sum of n does not exceed 10^4.
Sample Input
2
1 1 1
1 1 2
2
1 1 2
1 1 1
3
1 3 1
2 2 1
3 1 1
Sample Output
2 1
1 2
1 2 3
Source
题意
给定n个三元组,求[1...n]的一个字典序最小的排列,排列满足题目的要求
思路
排序,要注意精度问题,不要将long long类型的数转double后比较。
C++代码
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1050;
const double ESP=1e-6;
struct Tuple{
int id;
ll x,y;
bool operator<(const Tuple &h)const
{
ll t1=y*h.x,t2=x*h.y;
return t1==t2?id<h.id:t1>t2;
}
}t[N];
int main()
{
int n;
ll a,b,c;
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
{
scanf("%lld%lld%lld",&a,&b,&c);
t[i].id=i,t[i].x=a+b,t[i].y=c;
}
sort(t+1,t+n+1);
for(int i=1;i<=n;i++)
printf("%d%c",t[i].id,i==n?'\n':' ');
}
return 0;
}