POJ 2431 贪心+优先队列

题意:一辆卡车距离重点L,现有油量P,卡车每前行1米耗费油量1,途中有一些加油站,问最少在几个加油站加油可使卡车到达终点或到达不了终点。

思路:运用优先队列,将能走到的加油站的油量加入优先队列中,油不够时加入优先队列中数值最大的油,如果油不够时队列里为空则到达不了。

#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
priority_queue<int> que;
struct Stop {
    int dis,fuel;
}stop[10010];
bool cmp (Stop a,Stop b) {
    if(a.dis==b.dis) return a.fuel>b.fuel;
    return a.dis<b.dis;
}
int main() {
    int n,L,p;
    while(~scanf("%d",&n)) {
        for(int i=0;i<n;i++) {
            scanf("%d%d",&stop[i].dis,&stop[i].fuel);
        }
        scanf("%d%d",&L,&p);
        for(int i=0;i<n;i++) {
            stop[i].dis=L-stop[i].dis;
        }
        sort(stop,stop+n,cmp);
        stop[n].dis=L;stop[n].fuel=0;
        int cnt=0,pos=0,flag=0;
        for(int i=0;i<=n;i++) {
            int d=stop[i].dis-pos;
            while(p<d) {
                if(!que.empty()) {
                    p+=que.top();
                    que.pop();
                    cnt++;
                }else break;
            }
            if(p<d) {
                flag=1;
                puts("-1");break;
            }
            p-=d;
            que.push(stop[i].fuel);
            pos=stop[i].dis;
        }
        if(flag==0) printf("%d\n",cnt);
        while(!que.empty()) que.pop();
    }
    return 0;
}
时间: 2024-10-08 17:02:13

POJ 2431 贪心+优先队列的相关文章

poj 3190 贪心 + 优先队列

题意:一群牛,每头牛有固定的时间[a,b]让农夫去挤牛奶,农夫也只能在对应区间对指定奶牛进行挤奶, 求最少要多少个奶牛棚,在每个棚内的奶牛的挤奶时间不冲突. 算法:1.第一个想法就是贪心,对每头牛的挤奶时间[a,b]按a和b都从小排序,接着从左边开始找地一头牛, 然后再往右边找能够不冲突的牛再一个奶牛棚内.这个算法事件复杂度为n*n,由于最多5000头牛 所以后面还是TLE了. 2.还是自己太弱了,原来可以用优先队列进行优化,可以把当前冲突的牛放入优先队列,然后每次都能够冲优先队列 里面取出a最

poj 2431 Expedition(优先队列)

题目链接 http://poj.org/problem?id=2431 Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9243   Accepted: 2700 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor driver

poj 3253 贪心+优先队列【哈夫曼思想】

题目链接 题目大意: 大意:需要把一根长木棍锯成一些短木棍短木棍的长度是告诉你的每一次锯的花费为要锯的改段的长度问最小花费比如n个小木棍长度分别5 8 8也就是相当于你有一根21的木棍 现在需要把它锯成 上述的三段每次只能把一个木棍锯成两段比如21可以锯成13 和 8 但是由于选择的是21 所以花费为21第二次把13 锯成5和8 花费 为13总花费为21 + 13 = 34 分析:其实我们可以逆向思维想在有n跟木棍现在想要把他们拼成一跟每一次的花费就是这两段的和那么我们根据贪心的思想肯定是每次选

POJ 2431(优先队列)

A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to run over a rock and puncture the truck's fuel tank. The truck now leaks one unit of fuel every unit of di

Sunscreen (poj 3614 贪心+优先队列)

Language: Default Sunscreen Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4499   Accepted: 1565 Description To avoid unsightly burns while tanning, each of the C (1 ≤ C ≤ 2500) cows must cover her hide with sunscreen when they're at th

Stall Reservations(POJ 3190 贪心+优先队列)

Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4434   Accepted: 1588   Special Judge Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time in

poj 2431 Expedition (贪心+优先队列)

Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6890   Accepted: 2065 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to

POJ 3190 Stall Reservations(贪心+优先队列优化)

Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a reserv

POJ 1862 Stripies 贪心+优先队列

http://poj.org/problem?id=1862 题目大意: 有一种生物能两两合并,合并之前的重量分别为m1和m2,合并之后变为2*sqrt(m1*m2),现在给定n个这样的生物,求合并成一个的最小重量 思路: m1+m2 >=  2*sqrt(m1*m2) 所以每次取大的去合并,能变小. 直接优先队列就可以啦. #include<cstdio> #include<cmath> #include<queue> using namespace std;