priority_queue practice

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;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值