题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805410555346944
这一题思路很清晰,就是结构体排序,但是要读懂题目,我开始以为小人是品德低于H,结果出现了很多错误。
for (int i = 0; i < N; i++) {
person a;
cin >> a.ID >> a.VG >> a.TG;
if (a.VG < L || a.TG < L)
continue;
if (a.VG >= H && a.TG >= H)
a.cluster = 1; //sage
else if (a.VG >= H)
a.cluster = 2; //nobleman
else if (a.TG >= H)
a.cluster = 4; //small man
else
a.cluster = 3; //fool man
P[num_sort] = a;
num_sort++;
}
后来修改了就好了,下面是源码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>
#include<stack>
#include<queue>
//A1062 Talent and Virtue (25 分)
using namespace std;
const int maxn = 100010;
typedef char ElemT;
struct person {
int ID;
int VG; //Virtue_Grade
int TG; //Talent_Grade
int cluster=5; //"1" is sage; "2" is nobleman; "3" is a "fool man;
}P[maxn];
//N (≤10^5), the total number of people to be ranked;
//L(≥60), the lower bound of the qualified grades -- that is, only the ones whose grades of talentand virtue are both not below this line will be ranked;
//and H(< 100), the higher line of qualification
int N, L, H;
bool cmp(person a, person b) {
if (a.cluster != b.cluster)
return a.cluster < b.cluster;
if (a.VG + a.TG != b.VG + b.TG)
return a.VG + a.TG > b.VG + b.TG;
if (a.VG != b.VG)
return a.VG > b.VG;
return a.ID < b.ID;
}
int main() {
cin >> N >> L >> H;
int num_sort = 0;
for (int i = 0; i < N; i++) {
person a;
cin >> a.ID >> a.VG >> a.TG;
if (a.VG < L || a.TG < L)
continue;
if (a.VG >= H && a.TG >= H)
a.cluster = 1; //sage
else if (a.VG >= H)
a.cluster = 2; //nobleman
else if (a.TG > a.VG)
a.cluster = 4; //small man
else
a.cluster = 3; //fool man
P[num_sort] = a;
num_sort++;
}
sort(P, P + num_sort, cmp);
cout << num_sort << endl;
for (int i = 0; i < num_sort; i++) {
cout << P[i].ID << " " << P[i].VG << " " << P[i].TG << endl;
}
return 0;
}