-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy path1055_The World's Richest (25).cpp
112 lines (101 loc) · 2.02 KB
/
1055_The World's Richest (25).cpp
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <cstdio>
#include <iostream>
#include <map>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
using namespace std;
static inline bool get(int &v)
{
int s = 1, c;
while(!isdigit(c = getchar())&&c != '-')
{
if(c == EOF)
break ;
}
if(c == EOF) return 0;
if(c == '-') s = 0 , v = 0;
else v = c^48;
for(;isdigit(c = getchar());v = (v << 1) + (v << 3) + (c ^ 48));
v = (s ? v : -v);
return 1 ;
}
/*
用age为索引,建立结构,对[1-200]的age排序,在用优先队列,找出前num个需要的元素
*/
struct info
{
char name[20];
int age;
int val;
}tmp;
vector<info> elem[205];
priority_queue<info> q;
int addNum[205];
bool operator < (info a, info b)
{
if(a.val == b.val)
{
if(a.age == b.age) return strcmp(a.name,b.name)>=0;
else return a.age > b.age;
}
else return a.val < b.val;
}
int cmp(const info &a, const info &b)
{
if(a.val == b.val)
{
if(a.age == b.age) return strcmp(a.name,b.name)<0;
else return a.age < b.age;
}
else return a.val > b.val;
}
int main()
{
int n,k,i,num,a,b,j,kk;
get(n), get(k);
for(i = 1; i <= n; i++)
{
scanf("%s", tmp.name);
get(tmp.age),get(tmp.val);
elem[tmp.age].push_back(tmp);
}
for(i = 1; i <= 200; i++)
sort(elem[i].begin(), elem[i].end(), cmp);
for(i = 1; i <= k; i++)
{
while(!q.empty()) q.pop();
get(num),get(a),get(b);
printf("Case #%d:\n", i);
int cnt = 0;
for(j = a; j <= b; j++)
{
if( elem[j].size() )//加入所有a-b年龄的第一个元素
{
addNum[j] = 1;
tmp.val = elem[j][0].val;
tmp.age = j;
strcpy(tmp.name, elem[j][0].name);
q.push(tmp);
}
}
while(cnt < num && !q.empty())
{
info fuck = q.top();
q.pop();
if(addNum[fuck.age] < elem[fuck.age].size())
{
tmp.age = fuck.age;
strcpy(tmp.name, elem[fuck.age][addNum[fuck.age]].name);
tmp.val = elem[fuck.age][addNum[fuck.age]].val;
addNum[fuck.age]++;
q.push(tmp);
}
printf("%s %d %d\n", fuck.name, fuck.age, fuck.val);
cnt++;
}
if(cnt == 0) puts("None");
}
return 0;
}