F. Sorting
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 contains 3 inteegers ai,bi,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
题意:
有n个三元组,下标依次为1至n,给出三元组排序的方式
求出所代表分数,求解最小字典序的排列方式
思路:
式子可以化简为,( a(i-1) + b(i-1) * c(i) <= ( a(i) + b(i) ) * c(i-1) )
数据比较小,冒泡排序一遍即可
#include <iostream> #include <cstdio> using namespace std; #define LL long long #define N 2005 struct node { LL a,b,c; }no[N]; int ans[N]; int main() { int n; while(scanf("%d",&n)!=EOF) { for(int i=1;i<=n;i++) { ans[i] = i; scanf("%lld%lld%lld",&no[i].a,&no[i].b,&no[i].c); } for(int i=1;i<n;i++) { for(int j=i+1;j<=n;j++) { if( (no[j].a + no[j].b) * no[i].c < (no[i].a + no[i].b) * no[j].c ) //冒泡排序 swap(ans[i],ans[j]); else if( (no[j].a + no[j].b) * no[i].c == (no[i].a + no[i].b) * no[j].c){ //如果相等,则下标小的优先 if(ans[j] < ans[i]) swap(ans[i],ans[j]); } } } for(int i=1;i<=n;i++) printf(i==1?"%d":" %d",ans[i]); puts(""); } return 0; }