Find the hotel
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 539 Accepted Submission(s): 159
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 <p i, d i> for a hotel h i, where p i stands for the price and d i 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 h 1, if there is a hotel h i, 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.
But there are so many of them! Flynn gets tired to look for any. It’s your time now! Given the <p i, d i> for a hotel h i, where p i stands for the price and d i 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 h 1, if there is a hotel h i, 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 (p i, d i) for the i-th hotel.
The number will be non-negative and less than 10000.
Each case begin with N (1 <= N <= 10000), the number of the hotel.
The next N line gives the (p i, d i) 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
Recommend
lcy
题意:给出n个hotel的价格和距离,求有多少个hotel是满足找不到其他的hotel比这个的价格和距离都小
解题思路:对于一个hotel的价格p 找出1~(p-1)范围中d值最小的 如果p对应的d比这个还小,则找不到符合要求的
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std;
#define LL long long
const int MAXN=10010;
int x[MAXN],dp[MAXN][20];
int n;
struct node
{
int p,d;
friend bool operator <(node a,node b)
{
return a.p<b.p||(a.p==b.p&&a.d<b.d);
}
}a[MAXN],b[MAXN];
void init()
{
for(int i=1;i<MAXN;i++) dp[i][0]=x[i];
for(int j=1;(1<<j)<=MAXN;j++)
{
for(int i=1;i+(1<<j)-1<=MAXN;i++)
dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
}
int get(int l,int r)
{
int k=0;
while((1<<(k+1))<=r-l+1) k++;
return min(dp[l][k],dp[r-(1<<k)+1][k]);
}
int main()
{
while(~scanf("%d",&n)&&n)
{
for(int i=1;i<MAXN;i++)
x[i]=10009;
for(int i=1;i<=n;i++)
{
scanf("%d %d",&a[i].p,&a[i].d);
a[i].p++;a[i].d++;
x[a[i].p]=min(x[a[i].p],a[i].d);
}
init();
int cnt=0;
for(int i=1; i<=n; i++)
{
if(a[i].p==1) b[cnt++]=a[i];
else
{
int mi=get(1,a[i].p-1);
if(a[i].d<=mi) b[cnt++]=a[i];
}
}
sort(b,b+cnt);
printf("%d\n",cnt);
for(int i=0; i<cnt; i++)
printf("%d %d\n",b[i].p-1,b[i].d-1);
}
return 0;
}