F. Sorting
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
(a[p-1]+b[p-1])/(a[p-1]+b[p-1]+c[p-1]) <= (a[p]+b[p])/(a[p]+b[p]+c[p])
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
题解
- 将n个三元组排序,排序要求见题中公式
- 贪心解决
代码
来自官方的solution
#include <algorithm>
#include <cstdio>
const int N = 100000;
int a[N], b[N], c[N], ord[N];
bool cmp(int i, int j)
{
auto x = c[i] * (1ULL * a[j] + b[j] + c[j]);
auto y = c[j] * (1ULL * a[i] + b[i] + c[i]);
if (x == y) {
return i < j;
}
return x > y;
}
int main()
{
int n;
while (scanf("%d", &n) == 1) {
for (int i = 0; i < n; ++ i) {
scanf("%d%d%d", a + i, b + i, c + i);
ord[i] = i;
}
std::sort(ord, ord + n, cmp);
for (int i = 0; i < n; ++ i) {
printf("%d%c", ord[i] + 1, " \n"[i == n - 1]);
}
}
}
注:
个人拙见,如大家有发现错误,请留言指出,万分感谢!