贪心没错,但不是这样贪的.
下面是我通过的代码:
/**
&#pku2431.cpp
@author Eastsun
@version 1.0 2007/10/14
*/
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
struct Stop{
Stop(int d,int f){
dis =d;
fuel =f;
}
int dis;
int fuel;
};
struct CmpByDis:public binary_function<Stop,Stop,bool>{
bool operator()(const Stop& s1,const Stop& s2)const{
return s1.dis >s2.dis;
}
};
struct CmpByFuel:public binary_function<Stop,Stop,bool>{
bool operator()(const Stop& s1,const Stop& s2)const{
return s1.fuel <s2.fuel;
}
};
int main(){
int n;
cin>>n;
vector<Stop> vec;
for(int i =0;i <n;i ++){
int d,f;
cin>>d>>f;
vec.push_back(Stop(d,f));
}
int dist,fuel,count =0;
cin>>dist>>fuel;
sort(vec.begin(),vec.end(),CmpByDis());
priority_queue<Stop,vector<Stop>,CmpByFuel> queue;
vector<Stop>::iterator itr =vec.begin();
for(;;){
for(;itr!=vec.end()&&fuel>=dist -(*itr).dis;itr ++)
queue.push(*itr);
if(queue.empty()){
cout<<-1<<endl;
break;
}
if(fuel>=dist){
cout<<count<<endl;
break;
}
fuel += queue.top().fuel;
queue.pop();
count ++;
}
}