题解
采用堆临时存储,acwing上过,PAT上段错误
C++ 代码
#pragma GCC optimize(2)
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int N = 1e5 + 10, MAX_AGE = 210;
int n, k;
struct Node {
string name;
int age, asset;
};
struct cmp {
bool operator() (const Node &a, const Node &b){
if (a.asset != b.asset) return a.asset > b.asset;
if (a.age != b.age) return a.age < b.age;
return a.name < b.name;
}
};
struct cmp_heap {
bool operator() (const Node &a, const Node &b){
if (a.asset != b.asset) return a.asset < b.asset;
if (a.age != b.age) return a.age > b.age;
return a.name > b.name;
}
};
vector<Node> ages[MAX_AGE];
int idx[MAX_AGE];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i ++ )
{
string name;
int age, asset;
cin >> name >> age >> asset;
Node x = {name, age, asset};
ages[age].push_back(x);
}
for (int i = 1; i <= MAX_AGE; i ++ )
{
if (ages[i].size())
{
sort(ages[i].begin(), ages[i].end(), cmp());
// for (auto x : ages[i]) cout << i << " " << x.age << " " << x.asset << endl;
}
}
int cnt = 1;
while (k -- )
{
int m, l, r;
cin >> m >> l >> r;
cout << "Case #" << (cnt ++ ) << ":" << endl;
priority_queue<Node, vector<Node>, cmp_heap> heap;
memset(idx, 0, sizeof idx);
for (int i = l; i <= r; i ++ )
{
if (ages[i].size())
{
heap.push(ages[i][0]);
idx[i] ++ ;
// for (auto x : ages[i]) cout << i << " " << x.age << " " << x.asset << endl;
}
}
if (!heap.size()) cout << "None" << endl;
while (m -- && heap.size())
{
Node x = heap.top();
heap.pop();
int age = x.age;
// cout << "idx " << idx[age] << endl;
if (idx[age] < ages[age].size())
{
heap.push(ages[age][idx[age]]);
// cout << "debug " << ages[age][idx[age]].name << " " << ages[age][idx[age]].age << endl;
idx[age] ++ ;
}
cout << x.name << " " << x.age << " " << x.asset << endl;
}
}
return 0;
}
你的case3都多了一个行空格,所以pat会导致段错误