POJ 1456 - Supermarket

Link: http://poj.org/problem?id=1456

把物品丟到時間軸上,反過來做就變成水題了,用priority_queue即可
滿有趣的一題,關鍵在於能不能想到把時間反轉解決

AC code
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
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
vector<int> a[10010];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);

int n;
while(cin>>n)
{
for(int i=1;i<=10000;i++)
a[i].clear();
for(int i=0;i<n;i++)
{
int p,q;
cin>>p>>q;
a[q].push_back(p);
}
priority_queue<int> pq;
int ans=0;
for(int i=10000;i>=1;i--)
{
for(int j=0;j<a[i].size();j++)
pq.push(a[i][j]);
if(pq.size())
ans+=pq.top(), pq.pop();
}
cout<<ans<<'\n';
}
}