Sorting
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 0 Accepted Submission(s): 0
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
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
api−1+bpi−1api−1+bpi−1+cpi−1≤api+bpiapi+bpi+cpi.
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.
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≤103
* 1≤ai,bi,ci≤2×109
* The sum of n does not exceed 104.
DO NOT print trailing spaces.
## Constraint
* 1≤n≤103
* 1≤ai,bi,ci≤2×109
* The sum of n does not exceed 104.
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
题解:相邻两组的a[i-1],b[i-1],c[i-1]和a[i],b[i],c[i]满足上面的公式,如果相等的话就不交换两组的顺序。
代码:一开始我把大于和小于两种情况一起考虑,在排序的时候把相等的交换了位置,然后一直在wa。。。,果然还是太菜了。。
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstdlib>
using namespace std;
const int MAX=10010;
struct node{
long double x,y,z;
}a[MAX];
int b[MAX];
int main(){
int n;
while(cin >> n){
for(int i = 1;i<=n;++i){
cin >> a[i].x >> a[i].y >> a[i].z;
b[i] = i;
}
for(int i = 1;i<=n;++i){
for(int j = i+1;j<=n;++j){
if(a[j].z*(a[i].x+a[i].y)>a[i].z*(a[j].x+a[j].y)){
swap(b[i],b[j]);
swap(a[i],a[j]);
}
else if(a[j].z*(a[i].x+a[i].y)==a[i].z*(a[j].x+a[j].y) && b[i]>b[j]){
swap(b[i],b[j]);
swap(a[i],a[j]);
}
}
}
for(int i = 1;i<=n;++i){
printf(i==1?"%d":" %d",b[i]);
}
cout << endl;
}
return 0;
}