HDU-3193 Find the hotel
2016-06-17 11:54
369 查看
原题链接
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;
}
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;
}
相关文章推荐
- 内容 + 推广,网站盈利的必要模式!哪些你还没做?
- 浅谈CSS在前端优化中一些值得注意的关键点
- android 网络请求总结
- 合并一个表格多个竖列的td,主要在后台改数据结构
- MyBatis的返回参数类型
- jmeter 函数助手之StringFromFile
- 解决eclipse publish项目后tomcat-webapps-root-lib为空的问题
- Ionic Js十:加载动作
- 共用体union用法讲解
- java 动态代理
- Android开发之给控件设置圆角边框
- 我的微信公众平台开通啦!
- 查看Oracle数据库名和实例名
- 剑指offer-面试题21:包含min函数的栈
- maven资源与使用
- BZOJ 2653 middle
- 11-198 使用atoi()函数把字符串转换为整数
- directx11编程中遇到的错误及解决方法
- 判断字符串为空的方法详解
- GCD(Grand Central Dispatch)和Block 使用-浅析