您的位置:首页 > 其它

(使用STL自带的排序功能进行排序7.3.7)POJ 2726 Holiday Hotel(结构体排序)

2013-11-03 22:38 525 查看
/*
* POJ_2726.cpp
*
*  Created on: 2013年11月3日
*      Author: Administrator
*/

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

const int maxn = 10010;

struct hotel{
int dist;
int cost;
}h[maxn];

bool com(const hotel& a, const hotel& b){//以距离dist为第一关键字,以费用cost为第二关键字进行排序
if(a.dist == b.dist){
return a.cost < b.cost;
}

return a.dist < b.dist;
}

int main(){
int n;
while(scanf("%d",&n)!=EOF,n){

int i;
for(i = 0 ; i < n ; ++i){
scanf("%d%d",&h[i].dist,&h[i].cost);
}

sort(h,h+n,com);

int min = INT_MAX;

int ans = 0;
for(i = 0 ; i < n ; ++i){//主要是要理解题意...如果a的dist比b小,那么b的cost一定要比a的cost小,这样b才能作为候选宾馆
if(h[i].cost < min){
ans++;
min = h[i].cost;
}
}

printf("%d\n",ans);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐