HDU-3193 Find the hotel

原题链接

Find the hotel

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 492    Accepted Submission(s): 140


Problem Description
  Summer again! Flynn is ready for another tour around. Since the tour would take three or more days, it is important to find a hotel that meets for a reasonable price and gets as near as possible! 
  But there are so many of them! Flynn gets tired to look for any. It’s your time now! Given the <pi, di> for a hotel hi, where pi stands for the price and di is the distance from the destination of this tour, you are going to find those hotels, that either with a lower price or lower distance. Consider hotel h1, if there is a hotel hi, with both lower price and lower distance, we would discard h1. To be more specific, you are going to find those hotels, where no other has both lower price and distance than it. And the comparison is strict.
 

Input
There are some cases. Process to the end of file.
Each case begin with N (1 <= N <= 10000), the number of the hotel.
The next N line gives the (pi, di) for the i-th hotel.
The number will be non-negative and less than 10000.
 

Output
First, output the number of the hotel you find, and from the next line, print them like the input( two numbers in one line). You should order them ascending first by price and break the same price by distance.
 

Sample Input
  
  
3 15 10 10 15 8 9
 

Sample Output
  
  
1 8 9

先用结构体node[]存储每个hotel的价钱和距离,用价钱从小到大排序,若价钱相同,则按距离从小到大排序。

再用ST预处理区间上旅馆距离的最小值。排序后第一个hotel肯定入选,从第二个hotel开始遍历到n.假设此时判断node[i]代表的hotel是否入选,用二分找到1到i 中第一旅馆node[j]的price小于等于node[i].price, 在判断1到j-1中旅馆距离的最小值是否大于等于node[i].d,是则入选。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <stack>
#include <queue> 
#include <cmath>
#include <algorithm>
#define maxn 10005 
#define INF 1e9
typedef long long ll;
using namespace std;

struct Node{
	Node(){
	}
	Node(int a, int b){
		p = a;
		d = b;
	}
	friend bool operator < (const Node&a, const Node&b){
		return (a.p == b.p && a.d < b.d) || a.p < b.p; 
	}
	int p, d;
}node[maxn];
int d[maxn][20], n;
vector<Node> v;
void ST(){
	
	for(int i = 0; i < n; i++)
	  d[i][0] = node[i].d;
	for(int j = 1; (1<<j) <= n; j++)
	  for(int i = 0; i+(1<<j) <= n; i++)
	   d[i][j] = min(d[i][j-1], d[i+(1<<(j-1))][j-1]);
}
int main(){
	
//	freopen("in.txt", "r", stdin);
	while(scanf("%d", &n) == 1){
		
	    v.clear();
		for(int i = 0; i < n; i++)
		 scanf("%d%d", &node[i].p, &node[i].d);
		sort(node, node+n);
		ST();
		v.push_back(node[0]);
		for(int i = 1; i < n; i++){
			int l = 0, r = i;
			while(l < r){
				int mid = (l + r) >> 1;
				if(node[mid].p >= node[i].p)
				   r = mid;
				else
				 l = mid + 1;
			}
			if(l == 0){
				v.push_back(node[i]);
				continue;
			}
			int k = 0;
			while(1<<(k+1) <= l)
			 k++;
			int m = min(d[0][k], d[l-(1<<k)][k]);
			if(m >= node[i].d){
				v.push_back(node[i]);
			}
		}
		printf("%d\n", v.size());
		for(int i = 0; i < v.size(); i++){
			printf("%d %d\n", v[i].p, v[i].d);
		} 
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值