https://vjudge.net/problem/POJ-2431
http://stackoverflow.com/questions/15601973/stl-priority-queue-of-c-with-struct
#include<iostream>
#include<cstdio>
#include<string.h>
#include<math.h>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<queue>
#include<iomanip>
using namespace std;
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
struct S{
int dis,fuel;
}stop[10005];
bool cmp(S a,S b){
return a.dis < b.dis;
}
int main()
{
int n;
while(cin >> n){
for(int i=0;i<n;i++){
cin >> stop[i].dis >> stop[i].fuel;
}
int fdis,lfuel;
cin >> fdis >> lfuel;
for(int i=0;i<n;i++){
stop[i].dis = fdis - stop[i].dis;
}
sort(stop,stop+n,cmp);
priority_queue<int> pq;
int k = 0;
while(k <n && stop[k].dis <= lfuel){
pq.push(stop[k].fuel);
k++;
}
int cnt = 0;
int flag = 0;
while(!pq.empty()){
if(lfuel >= fdis){
cout << cnt << endl;
flag = 1;
break;
}
int temp = pq.top();
pq.pop();
cnt++;
lfuel += temp;
while(k<n && stop[k].dis <= lfuel){
pq.push(stop[k].fuel);
k++;
}
}
if(flag == 0){
cout << -1 << endl;
}
}
}
https://vjudge.net/problem/POJ-3253
http://en.cppreference.com/w/cpp/container/priority_queue
#include<iostream>
#include<cstdio>
#include<string.h>
#include<math.h>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<queue>
#include<iomanip>
using namespace std;
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
int main()
{
int n;
int len[20005];
while(cin >> n){
for(int i=0;i<n;i++){
cin >> len[i];
}
priority_queue<int,vector<int>,greater<int> > pq;
for(int i=0;i<n;i++){
pq.push(len[i]);
}
long long ans = 0;
while(pq.size() > 1){
int l1,l2;
l1 = pq.top();
pq.pop();
l2 = pq.top();
pq.pop();
pq.push(l1+l2);
ans += l1+l2;
}
cout << ans << endl;
}
}