您的位置:首页 > 其它

HDU-3193 Find the hotel

2016-06-17 11:54 369 查看
原题链接


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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: