思路
非常简单的模拟题,唯一的难点在于如何处理并列,这里有两点:
- sort函数中的cmp函数怎么处理并列?
- 输出的时候如何判断是否并列?
要知道,cmp函数判断的是严格小于,即一定要有一个大小关系,如果是并列的话,就直接不动就行,即return false;
由于要求是根据原序号输出对应的位次(题中加粗强调),我们可以对排序后的函数进行两两的比较,如果相等,就赋同样的值并继续往后比,同时用一个cnt 记录并列的人数
在全部记录排名之后,再更具原坐标排序并输出即可
难度:中位橙
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| #include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std;
struct cj { int x ,y ,z; int idx; int rank; };
bool cmp(const cj& p,const cj& q){ int sum1p = p.x + p.y + p.z; int sum1q = q.x + q.y + q.z; if(sum1p == sum1q){ int sum2p = p.x + p.y; int sum2q = q.x + q.y; if (sum2p == sum2q){ int maxp = max(p.x,p.y); int maxq = max(q.x,q.y); if(maxp == maxq){ return false; } return maxp > maxq; } return sum2p > sum2q; } return sum1p > sum1q; }
bool issame(const cj& p,const cj& q){ return p.x + p.y + p.z == q.x + q.y + q.z && p.x + p.y == q.x + q.y && max(p.x,p.y) == max(q.x,q.y); }
bool cmp2(const cj &p, const cj &q){ return p.idx < q.idx; }
signed main() { ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); int n; cin >> n; vector<cj> a(n); for(int i = 0 ; i < n ; i++){ cin >> a[i].x >> a[i].y >> a[i].z; a[i].idx = i; } sort(a.begin(),a.end(),cmp); int p = 1; for(int i = 1 ; i <= n ; i++){ a[i - 1].rank = p; int cnt = 1; while(i < n && issame(a[i - 1],a[i])){ a[i].rank = p; i++; cnt++; } p+= cnt; } sort(a.begin(),a.end(),cmp2); for(int i = 0 ; i < n ; i++){ cout << a[i].rank << endl; } return 0; }
|