PAT 1025

题目 : PAT Ranking (25)

分值 : 25
难度 : 简单结构题排序题
思路 : 弄个结构体分部排序,先小排,再大排就可以了
坑点 : 分部排序,在sort的起点终点要设置清楚
评语 : 只要你写的正确一点,没什么边界可找麻烦

具体代码如下

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
#include <iostream>
#include <string>
#include <algorithm>
using namespace std ;
typedef struct Node
{
string id ;
int score ;
int location ;
int global_rank ;
int local_rank ;
}Nodes ;
Nodes data[30001] ;
bool cmp1(Node a , Node b )
{
if(a.score==b.score)
return b.id > a.id ;
return a.score > b.score ;
}

int main() {
int N ;
cin >> N ;
int cur = 0 ;
string id ,score ;
for(int i = 0 ; i< N ;i++)
{
int count ;
cin>> count ;
for(int j = 0 ; j< count ; j++)
{
cin >> data[cur].id >> data[cur].score;
data[cur].location = i +1 ;
cur ++ ;
}
sort( data+cur-count , data+cur ,cmp1);
for(int j = 0 ;j < count; j++ )
{
int x = j+ cur-count ;
if( j && data[x].score == data[x-1].score)
data[x].local_rank = data[x-1].local_rank;
else data[x].local_rank = j+1 ;
}
}
sort(data , data+cur , cmp1) ;
cout <<cur <<endl ;
for(int i = 0 ; i< cur ;i++)
{
if(i && data[i].score == data[i-1].score)
data[i].global_rank = data[i-1].global_rank;
else data[i].global_rank = i+1 ;
cout <<data[i].id <<" "<<data[i].global_rank<<" "<<data[i].location<<" "<<data[i].local_rank<<endl ;
}
return 0 ;
}