Skip to content

Commit 4cc5259

Browse files
committed
pat
1 parent 839157d commit 4cc5259

File tree

1 file changed

+51
-111
lines changed

1 file changed

+51
-111
lines changed

PAT/PAT_A/pat1055.cpp

Lines changed: 51 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -47,121 +47,61 @@ Cindy 76 76000
4747
Case #4:
4848
None
4949
*/
50-
#include<string.h>
51-
#include<vector>
52-
#include<stdio.h>
53-
#include<algorithm>
50+
#include <iostream>
51+
#include <string>
52+
#include <algorithm>
53+
#include <set>
54+
#include <vector>
55+
#include <stdio.h>
5456

5557
using namespace std;
5658

57-
#define min(a,b) (a<b)?a:b
58-
59-
class Person
60-
{
61-
public:
62-
char name[19];
63-
int age;
64-
int worth;
65-
66-
void print()
67-
{
68-
printf("%s %d %d\n", name, age, worth);
69-
}
70-
71-
bool operator<(const Person &person) const
72-
{
73-
if( worth == person.worth)
74-
{
75-
if( age == person.age)
76-
{
77-
return strcmp(name, person.name) < 0;
78-
}
79-
return age < person.age;
80-
}
81-
else
82-
{
83-
return worth > person.worth;
84-
}
85-
}
59+
struct P {
60+
string name;
61+
int age, value;
62+
bool operator <(const P& p) const {
63+
if (p.value != value)
64+
return value > p.value;
65+
else if (p.age != age)
66+
return age < p.age;
67+
return name < p.name;
68+
}
8669
};
8770

71+
vector<P> tab[201];
72+
8873
int main()
8974
{
90-
int nperson, nquery;
91-
92-
scanf("%d %d", &nperson, &nquery);
93-
vector<vector<Person>> table(201);
94-
vector<int> index(201, 0);
95-
96-
for(int i=0; i<nperson; i++)
97-
{
98-
Person p;
99-
scanf("%s %d %d", p.name, &p.age, &p.worth);
100-
table[p.age].push_back(p);
101-
}
102-
103-
for( int i=0,size=table.size(); i<size; i++)
104-
{
105-
sort(table[i].begin(), table[i].end());
106-
}
107-
108-
for(int i=1; i<=nquery; i++)
109-
{
110-
int agemin, agemax, k;
111-
112-
scanf("%d %d %d", &k, &agemin, &agemax);
113-
114-
printf("Case #%d:\n", i);
115-
116-
int K = 0;
117-
118-
for(int j=agemin; j<=agemax; j++)
119-
{
120-
index[j] = 0;
121-
}
122-
123-
Person minp;
124-
int minindex;
125-
126-
while(true)
127-
{
128-
bool reachend = true;
129-
minp.worth = -1000001;
130-
for( int j=agemin; j<=agemax; j++)
131-
{
132-
if( index[j] >= table[j].size())
133-
{
134-
}
135-
else
136-
{
137-
reachend = false;
138-
if( table[j][index[j]] < minp)
139-
{
140-
minindex = j;
141-
minp = table[j][index[j]];
142-
}
143-
}
144-
}
145-
146-
if( reachend)
147-
{
148-
if( !K)
149-
{
150-
printf("None\n");
151-
}
152-
break;
153-
}
154-
else
155-
{
156-
minp.print();
157-
index[minindex]++;
158-
if( ++K == k)
159-
{
160-
break;
161-
}
162-
}
163-
}
164-
}
165-
166-
return 0;
167-
}
75+
int n, m;
76+
cin >> n >> m;
77+
for (int i=0; i<n; ++i) {
78+
string name;
79+
int age, value;
80+
cin >> name >> age >> value;
81+
tab[age].push_back({name, age, value});
82+
}
83+
for (auto &x : tab)
84+
sort(x.begin(), x.end());
85+
for (int i=0; i<m; ++i) {
86+
int k, lb, ub;
87+
cin >> k >> lb >> ub;
88+
printf("Case #%d:\n", i+1);
89+
set<pair<P,int>> pq;
90+
for (int j=lb; j<=ub; ++j)
91+
if (j>0 && j<=200 && tab[j].size())
92+
pq.insert(make_pair(tab[j][0], 0));
93+
if (pq.size() == 0)
94+
cout << "None" << endl;
95+
else
96+
for (int l=0; l<k && pq.size(); ++l) {
97+
auto p = pq.begin();
98+
printf("%s %d %d\n", p->first.name.c_str(),
99+
p->first.age, p->first.value);
100+
int age = p->first.age, idx = p->second;
101+
pq.erase(p);
102+
if (++idx < tab[age].size())
103+
pq.insert(make_pair(tab[age][idx], idx));
104+
}
105+
}
106+
return 0;
107+
}

0 commit comments

Comments
 (0)