HDU 6281 sorting (湘潭邀请赛F题)

版权声明: https://blog.csdn.net/Adusts/article/details/82021674

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≤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

这就是一个排序题,注意把题目中的公式化简成乘法。

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int MAXN = 1005;
typedef long long ll;
struct P{
	ll a, b, c;
	int count;
}p[MAXN];
bool cmp(P a, P b) {
	ll t = b.c*(a.a+a.b);
	ll k = a.c*(b.a+b.b);
	if(t == k) {
		return a.count<b.count;
	} else {
		return t<k;
	}
}
int main() {
	int n;
	while(scanf("%d", &n) != EOF) {
		for(int i = 1; i <= n; i++) {
			scanf("%lld %lld %lld", &p[i].a, &p[i].b, &p[i].c);
			p[i].count = i;
		}
		sort(p+1, p+1+n, cmp);
		for(int i = 1; i <= n; i++) {
			i==n?printf("%d\n", p[i].count):printf("%d ", p[i].count);
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Adusts/article/details/82021674