F. Sorting (排序,浮点数精度) [2017 CCPC China-Hunan Invitional]

Description

Bobo has ntuples(a1,b1,c1),(a2,b2,c2),,(an,bn,cn) . He would like to find the lexicographically smallest permutation p1,p2,,pnof1,2,,n such that fori2,3,,n it holds that api1+bpi1api1+bpi1+cpi1api+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 nlines contains 3integers ai,bi andci.

Output

For each test case, print nintegersp1,p2,,pn seperated by spaces. DO NOT print trailing spaces.

Constraint

  • 1n103
  • 1ai,bi,ci2×109
  • The sum of ndoes not exceed104.

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个三元组,要求给出这 n个三元组的一个排列,使得对Missing or unrecognized delimiter for \left 都满足题给关系。

分析

题给关系中的不等式只涉及 pi1pi 两个元素,实际上相当于对三元组重载了小于号。因此,本题实际上是对读入的三元组序列进行排序。因此,我们用一个Node结构体存储三元组的三个变元和读入时在原序列中的位置即可。

struct Node {
    long long a, b, c;
    int id;
}

同时,在比较三元组关系时,因为本题数据卡精度较严,因此不能用double,而应交叉相乘再比较,同时在这过程中约去一些相同的项(避免爆long long)。 (api1+bpi1)(api+bpi+cpi)(api+bpi)(api1+bpi1+cpi1))

(api1+bpi1)cpi(api+bpi)cpi1

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXN = 1010;
typedef long long ll;

struct Node {
    ll a, b, c;
    int id;

    bool operator < (const Node& x) const {
        ll q1 = c * (x.a + x.b);
        ll q2 = x.c * (a + b);

        if(q2 == q1) return id < x.id;
        else return q2 < q1;
    }
} t[MAXN];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n = 0;
    while(cin >> n) {
        for(int i = 1; i <= n; i++) {
            cin >> t[i].a >> t[i].b >> t[i].c;
            t[i].id = i;
        }

        sort(t + 1, t + n + 1);

        for(int i = 1; i <= n; i++)
            if(i != n) cout << t[i].id << " ";
            else cout << t[i].id << endl;
    }

    return 0;
}
updatedupdated2023-03-112023-03-11