HDU3193 Find the hotel【RMQ】

Find the hotel

http://acm.hdu.edu.cn/showproblem.php?pid=3193

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


 

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

Author

ZSTU

 

Source

HDU 2009-12 Programming Contest

题意

每个房子有两个属性:价格、距离,现给出 n 个房子的价格和距离,定义目标房子:对于当前的房子 i ,若没有其他的任何一个房子的价格和距离同时小于当前房子 i 的价格和距离,要求先输出目标房子的个数,然后按照价格从小到大的顺序输出所有目标房子的价格和距离,若价格相同,则按照距离从小到大的顺序输出

思路

这里有一个技巧,当前房子不是目标房子当且仅当至少有一个房子的价格和距离同时小于它,由于要保证这两个属性描述的是同一个房子,因此我们不能直接单独查询是否有房子的价格或距离小于它,由于距离和价格都是非负数,因此我们可以将其中的一个属性表示为下标,程序用 f[i]=d 表示价格为i的房子的最小距离为d,编号为i的hotel的价格和距离分别为p[i]和d[i],因此我们需要在价格小于p[i](即[0,p[i]-1])的hotel中查找距离最小的hotel,如果此距离小于p[i],则不是目的hotel,否则是 

C++程序

#include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>

using namespace std;

const int INF=1000000;
const int N=10010;

int d[N+1],p[N+1],f[N+1],dp[N+1][15];//f[i]=d表示价格为i的房子的最小距离为d 

void ST(int n)
{
	for(int i=0;i<=n;i++) dp[i][0]=f[i];
	for(int j=1;(1<<j)<=n;j++)
	  for(int i=0;i+(1<<j)-1<=n;i++)
	  	dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}

int query(int l,int r)
{
	if(l>r) return INF;//方便处理 
	int k=(int)(log((double)(r-l+1))/log(2.0));
	return min(dp[l][k],dp[r-(1<<k)+1][k]);
}

struct ANS{
	int p,d;
	ANS(int p,int d):p(p),d(d){}
	bool operator <(const ANS &h)const
	{
		return p==h.p?d<h.d:p<h.p;
	}
};

vector<ANS>v;//存放结果 

int main()
{
	int n;
	while(~scanf("%d",&n))
	{
		v.clear();
		for(int i=0;i<=N;i++) f[i]=INF;//初始化价格为i的房子的最短距离为INF 
		int maxp=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d",&p[i],&d[i]);
			f[p[i]]=min(f[p[i]],d[i]);
			maxp=max(maxp,p[i]);
		}
		ST(maxp);
		for(int i=1;i<=n;i++)
		{
			/*
			编号为i的hotel的价格和距离分别为p[i]和d[i]
			由于f[i]=j 表示价格为i的hotel的最小距离为j
			因此我们需要在价格小于p[i](即[0,p[i]-1])的hotel中查找距离最小
			的hotel,如果此距离小于p[i],则不是目的hotel,否则是 
			*/
			if(query(0,p[i]-1)<d[i]) continue;
			v.push_back(ANS(p[i],d[i]));
		}
		sort(v.begin(),v.end());
		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、付费专栏及课程。

余额充值