zoukankan      html  css  js  c++  java
  • [POJ 2431]Expedition

    1、题目连接:http://poj.org/problem?

    id=2431

    2、题目大意:你须要开着一辆卡车行驶L单位距离,最開始卡车有P单位汽油,卡车每开一单位距离须要消耗1单位汽油。如果在中途卡车汽油耗尽,卡车就无法继续前行。到不了终点。在途中一共同拥有n个加油站。告诉你每一个加油站距离终点的距离和每一个加油站能够加的油量,如果卡车的油箱是无穷大的,问卡车至少要加多少次油才干到达终点?卡车到不了终点输出-1

    3、题目思路:使用优先队列+贪心思想完毕此题,队列中维护到达眼下的加油站之前没实用过的加油站。保证队首的加油站能够加的油最多。假设当前油箱内的油量不够到达当前加油站。就从优先队列队首取出一个未用过的加油站加油。

    假设须要加油时队列为空,则卡车到不了终点

    4、代码:

    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <queue>
    
    #define MAXN 10010
    
    using namespace std;
    
    struct info
    {
    	int a,b; //a[i]=第i个加油站和起点之间的距离,b[i]=第i个加油站的油量
    }station[MAXN];
    
    priority_queue<int>heap;
    
    bool cmp(info x,info y)
    {
    	return x.a<y.a;
    }
    
    int main()
    {
    	int n,l,p;
    	scanf("%d",&n);
    	for(int i=0;i<n;i++)
    		scanf("%d%d",&station[i].a,&station[i].b);
    	scanf("%d%d",&l,&p);
    	for(int i=0;i<n;i++)
    		station[i].a=l-station[i].a;
    	int ans=0,pos=0,tank=p; //ans-加油次数,pos-当前位置,tank-剩余油量
    	station[n].a=l; //为了便于处理,把终点也看成一个加油站
    	station[n].b=0;
    	n++;
    	sort(station,station+n,cmp);
    	for(int i=0;i<n;i++)
    	{
    		int dist=station[i].a-pos; //dist=接下来要前行的距离
    		while(tank-dist<0) //油箱里的油不够就不断加,仅仅加最大的加油站里的油
    		{
    			if(heap.empty()) //没有可选的加油站。没油走不了了
    			{
    				printf("-1
    ");
    				return 0;
    			}
    			tank+=heap.top();
    			heap.pop();
    			ans++;
    		}
    		tank-=dist; //走到下一站。油箱中的油量降低
    		pos=station[i].a;
    		heap.push(station[i].b);
    	}
    	printf("%d
    ",ans);
    	return 0;
    }
    




    
  • 相关阅读:
    Python OpenCV
    Model忽略模型,不生成表创建语句
    GZSales.Electron生成记录
    Electron-Build打包成安装包错误,下载依赖,下载不来winCodeSign,或者下载很慢
    electron npm install缓存
    electrron npm install报错
    VS 自定义生成 Directory.Build.props Directory.Build.targets
    我的物联网项目(二十二) 微服务分库查询优化
    我的物联网项目专题移到网站:http://51jdk.com
    我的物联网项目(十四) 分布式事务
  • 原文地址:https://www.cnblogs.com/jhcelue/p/6735139.html
Copyright © 2011-2022 走看看