1062 Talent and Virtue 25
问题思路
我尝试了两种思路:
-
根据每个人的分数确定其是哪个级别(rank)的人,以总分(德分,学号)排序一次,再以rank排序一次
-
根据题目要求规定一种比较规则,一次排序解决问题
思路1将问题复杂化了,是我最开始基于次位优先方法想到的一种思路,列出来的主要原因是提醒自己:
- 基于MSD和LSD的排序方法要求每次排序使用稳定的排序算法
- C++
sort
函数内部是快排实现,不稳定,要用stable_sort
源码
// 思路1
#include <cstdio>
#include <vector>
#include <algorithm>
using std::vector;
class People{
public:
int talent;
int virtue;
int total;
int id;
int rank;
static int lower;
static int higher;
People(int id_i, int talent_i, int virtue_i):id(id_i), talent(talent_i), virtue(virtue_i), total(talent_i+virtue_i)
{
if(virtue < lower || talent < lower)
rank = 5;
else if(virtue < higher && talent > virtue)
rank = 4;
else if(talent < higher && virtue < higher)
rank = 3;
else if(talent < higher)
rank = 2;
else
rank = 1;
}
People(){}
static bool cmp_total(People a, People b)
{
if(a.total > b.total || (a.total == b.total && a.virtue > b.virtue) || (a.total == b.total && a.virtue == b.virtue && a.id < b.id))
return true;
else
return false;
}
static bool cmp_rank(People a, People b)
{
if(a.rank < b.rank)
return true;
else
return false;
}
};
int People::lower = 0;
int People::higher = 0;
int main()
{
int n;
vector<People> v;
scanf("%d %d %d", &n, &People::lower, &People::higher);
for(int i = 0; i < n; i++)
{
int id, talent, virtue;
scanf("%d %d %d", &id, &virtue, &talent);
v.push_back(People(id, talent, virtue));
}
sort(v.begin(), v.end(), People::cmp_total);
stable_sort(v.begin(), v.end(), People::cmp_rank);
int m=0;
for(auto s = v.rbegin(); s != v.rend(); s++)
{
if(s->rank == 5)
m++;
else
break;
}
printf("%d\n", n-m);
for(int i = 0; i < n-m; i++)
{
printf("%08d %d %d\n", v[i].id, v[i].virtue, v[i].talent);
}
return 0;
}
// 思路2
#include <cstdio>
#include <vector>
#include <algorithm>
using std::vector;
class People{
public:
int talent;
int virtue;
int total;
int id;
int rank;
static int lower;
static int higher;
People(int id_i, int talent_i, int virtue_i):id(id_i), talent(talent_i), virtue(virtue_i), total(talent+virtue)
{
if(virtue < lower || talent < lower)
rank = 5;
else{
if(virtue < higher && talent > virtue)
rank = 4;
else if(talent < higher && virtue < higher)
rank = 3;
else if(talent < higher)
rank = 2;
else
rank = 1;
}
}
People(){}
static bool cmp(People a, People b)
{
if(a.rank != b.rank)
return a.rank < b.rank;
else if(a.total != b.total)
return a.total > b.total;
else if(a.virtue != b.virtue)
return a.virtue > b.virtue;
else if(a.id != b.id)
return a.id < b.id;
}
};
int People::lower = 0;
int People::higher = 0;
int main()
{
int n;
vector<People> v;
scanf("%d %d %d", &n, &People::lower, &People::higher);
for(int i = 0; i < n; i++)
{
int id, talent, virtue;
scanf("%d %d %d", &id, &virtue, &talent);
v.push_back(People(id, talent, virtue));
}
sort(v.begin(), v.end(), People::cmp);
int m=0;
for(auto s = v.rbegin(); s != v.rend(); s++)
{
if(s->rank == 5)
m++;
else
break;
}
printf("%d\n", n-m);
for(int i = 0; i < n-m; i++)
{
printf("%08d %d %d\n", v[i].id, v[i].virtue, v[i].talent);
}
return 0;
}