您的位置:首页 > 其它

POJ 2431 Expedition

2017-07-20 20:32 253 查看
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;

const int maxn = 3e4+5;
struct node{
int dis;
int fue;

bool operator< (const node&I)const { //按照fue由da到xiao排列
return I.fue > fue; }
};
bool com (node a, node b){
return a.dis > b.dis;
}
node a[maxn];
priority_queue <node> pque;

int main (){
int n, l, p, tempdis,i,ans;
scanf("%d",&n);
memset(a,0,sizeof(a));

for (int i = 0; i < n; i++){
scanf("%d%d",&a[i].dis, &a[i].fue );
}
sort(a,a+n,com);
scanf("%d%d", &l, &p);
tempdis = p;
i = 0;
ans = 0;
while (!pque.empty() )	pque.pop() ;
while ( tempdis < l){

while ( i < n && a[i].dis >= l - tempdis){  // 符号写反,wa了1晚上...
pque.push(a[i]);
i++;
}
if (!pque.empty() ){
tempdis += (pque.top()).fue ;
pque.pop() ;
ans++;
}
else break;
}
if (tempdis >= l){
printf("%d\n",ans);
}
else printf("-1\n");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  贪心 优先队列