3662 - 加油

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 128 MB

一辆卡车要行驶L单位距离。最开始时,卡车上有P单位汽油,

<span style="font-size:14px;">每向前行驶1单位距离消耗1单位汽油。如果在途中车上的汽油耗尽,卡车就无法继续前行,即无法到达终点。</span>

<span style="font-size:14px;">途中共有N个加油站,加油站提供的油量有限,卡车的油箱无限大,无论加多少油都没问题。</span>

<span style="font-size:14px;">给出每个加油站距离终点的距离和能够提供的油量,问卡车从起点到终点至少要加几次油?如果不能到达终点,</span>

<span style="font-size:14px;">输出-1。</span>

题目输入

第一行 N;
之后N行:第一个整数为每行终点距离到每个加油站的距离,,第二个整数为提供的油量;
之后一行:第一个整数为终点到起点距离L,第二个整数为初始油量P。

题目输出

每一行输出最少加几次油。

输入/输出样例

输入格式

4
4 4
5 2
11 5
15 10
25 10

输出格式

2

C++解答

#include <stdio.h>
#include <queue>
#include <algorithm>
using namespace std;
struct stop
{
	int zhan;
	int you;
}s[10005];
int n,p,l;
priority_queue <int> que;
bool comp(stop s1,stop s2)
{
	return s1.zhan<s2.zhan;
}
int solve()
{
	int ans=0 ,pos=0,tank=p;
	for(int i=0;i<n;i++)
	{
		int d=s[i].zhan-pos;
		while(tank-d<0)
		{
			if(que.empty())
			{
				return -1;
			}
			tank+=que.top();
			que.pop();
			ans++;
		}
		tank-=d;
		pos=s[i].zhan;
		que.push(s[i].you);
	}
	return ans;
}
int main()
{
	int i,a;
     while(scanf("%d",&n)!=EOF)
     {
     	for(i=0;i<n;i++)
     	{
     		scanf("%d%d",&s[i].zhan,&s[i].you);
     	}
     	scanf("%d%d",&l,&p);
     	for(i=0;i<n;i++)
     	{
     		s[i].zhan=l-s[i].zhan;
     	}
     		s[n].zhan=l;
     	s[n++].you=0;
     	while(!que.empty())
     	que.pop();
     sort(s,s+n,comp);
     a=solve();
     printf("%d\n",a);
 }
 return 0;
}