Expedition POJ - 2431

版权声明:随意转载 https://blog.csdn.net/nuiniu/article/details/84201678

传送门

Expedition

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 distance it travels. 

To repair the truck, the cows need to drive to the nearest town (no more than 1,000,000 units distant) down a long, winding road. On this road, between the town and the current location of the truck, there are N (1 <= N <= 10,000) fuel stops where the cows can stop to acquire additional fuel (1..100 units at each stop). 

The jungle is a dangerous place for humans and is especially dangerous for cows. Therefore, the cows want to make the minimum possible number of stops for fuel on the way to the town. Fortunately, the capacity of the fuel tank on their truck is so large that there is effectively no limit to the amount of fuel it can hold. The truck is currently L units away from the town and has P units of fuel (1 <= P <= 1,000,000). 

Determine the minimum number of stops needed to reach the town, or if the cows cannot reach the town at all. 

Input

* Line 1: A single integer, N 

* Lines 2..N+1: Each line contains two space-separated integers describing a fuel stop: The first integer is the distance from the town to the stop; the second is the amount of fuel available at that stop. 

* Line N+2: Two space-separated integers, L and P

Output

* Line 1: A single integer giving the minimum number of fuel stops necessary to reach the town. If it is not possible to reach the town, output -1.

Sample Input

4
4 4
5 2
11 5
15 10
25 10

Sample Output

2

这里只是提出几个在做题的时候遇到的几个问题:

q1:

struct date
{
	int a,b;
};

priority_queue<date> p;
//这里会提示你一下问题:
/*
  template<typename _Tp>
    struct less : public binary_function<_Tp, _Tp, bool>
    {
      bool
      operator()(const _Tp& __x, const _Tp& __y) const
      { return __x < __y; }
    };
说这里没有重载"<"运算符([Error] no match for 'operator<' (operand types are 'const date' and 'const date'))
*/

q2:这个也不算是个问题吧,应该说是知识储备量不足,一下是降序的优先队列(不过这一题根本不需要这个)

struct cmp1
{
    template<typename T, typename U>
    bool operator()(T const& left, U const &right) {
        if (left.second < right.second) return true;
        return false;
    }
};

priority_queue<pair<int,int>, vector<pair<int, int> >, cmp1 > s;

这个题只要priority_queue<int,vecort<int>,greater<int> > s;//注意空格

或者直接用默认的排序方式:priority_queue<int> s;

之所提出来是补充一下自己的不足。

参考博客:https://blog.csdn.net/u014257954/article/details/78623215

解题思路:

看油箱中所带的油能让车子走多远,途中经过的stop,(假装)有多少油加多少(用一个东西存着,当油少了就往里面取最多的油直到油够到下一个stop),就这样。。。

code:

//https://vjudge.net/problem/POJ-2431
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<queue>

using namespace std;


typedef struct date
{
	int d;
	int f;
}date;
date a[100005];

int cmp(const date c,const date b)
{
	return c.d > b.d;
}

struct cmp1
{
    template<typename T, typename U>
    bool operator()(T const& left, U const &right) {
        if (left.second < right.second) return true;
        return false;
    }
};

priority_queue<pair<int,int>, vector<pair<int, int> >, cmp1 > s;

int main()
{
	int N;
	int i;
	
	scanf("%d",&N);
	
	i=0;
	while(i<N)
	{
		scanf("%d%d",&a[i].d,&a[i].f);
		i++;
	}
	
	int D,F;
	scanf("%d%d",&D,&F);
	
	sort(a,a+N,cmp);
	
	a[N].d=0;
	a[N].f=0;
	
//	for(i=0;i<N;i++) printf("%d %d\n",a[i].d,a[i].f);
	
	int ans=0,flag=1;
	for(i=0;i<=N;)
	{
		if(F>=D-a[i].d)
		{
			pair<int,int> q;
			q.first=a[i].d;
			q.second=a[i].f;
			
			s.push(q);
			i++;
		}
		else if(s.empty())
		{
			flag=0;
			break;
		}
		else
		{
			ans++;
			pair<int,int> q=s.top();
			F+=q.second;
			s.pop();
		}
	}
	
	if(flag)printf("%d\n",ans);
	else printf("-1\n");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/nuiniu/article/details/84201678