2018 ccpc湘潭邀请赛 F -Sorting [思维+排序]

Sorting

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 415    Accepted Submission(s): 114


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

1n103
1ai,bi,ci2×109
* The sum of  n does not exceed  104.
 

Sample Input
 
   
21 1 11 1 221 1 21 1 131 3 12 2 13 1 1
 

Sample Output
 
   
2 11 21 2 3
 

Source
CCPC2018-湖南全国邀请赛-重现赛(感谢湘潭大学)

题目:就是先根据给定那个公式排序,相等的话按照下标排序,这样子只要把那个公式给化解一下就可以啦。。。

代码:

#include
#include
#define ll unsigned long long
using namespace std;
const int maxn = 1e3+7;
int n;
struct node{
    ll a, b, c;
    int pos;
} p[maxn];

bool cmp(node a, node b)
{
    ll x = (a.a + a.b)*b.c;
    ll y = (b.a + b.b)*a.c;
    if(x == y) return a.pos < b.pos;
    return x < y;
}
int main()
{
    //freopen("in.txt", "r", stdin);
    while(scanf("%d", &n)!=EOF)
    {
        for(int i = 0; i < n; i++)
        {
            scanf("%lld%lld%lld", &p[i].a, &p[i].b, &p[i].c);
            p[i].pos = i+1;
        }
        stable_sort(p, p+n, cmp);
        printf("%d", p[0].pos);
        for(int i = 1; i < n; i++)
        {
            printf(" %d", p[i].pos);
        }
        printf("\n");
    }
}

 

你可能感兴趣的:(其他,其他---------思维,HDU)