HDU Find the hotel(RMQ)
Find the hotel
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 611 Accepted Submission(s): 201
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.
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.
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
題意:
找特定旅館的數量,這個旅館的p和d不能都比其他旅館大。可以相等。
POINT:
先給p從小到大排個序,然後選定一個旅館p0 d0,從價格0-(p0-1)的範圍內找到d最小的,和d0比較,符不符合要求就是了。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
#define LL long long
const int N = 10000+5;
struct node
{
int p,d;
}h[N],ans[N];
int dp[N][20];
int n;
bool cmd(node a,node b)
{
if(a.p!=b.p)
{
return a.p<b.p;
}
else
return a.d<b.d;
}
void prermq()
{
for(int i=1;i<=n;i++)
{
dp[i][0]=h[i].d;
}
for(int j=1;1<<j<=n;j++)
{
for(int i=1;i-1+(1<<j)<=n;i++)
{
dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
}
}
int query(int l,int r)
{
int k=(int)log2((double)(r-l+1));
return min(dp[l][k],dp[r+1-(1<<k)][k]);
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
{
scanf("%d %d",&h[i].p,&h[i].d);
}
sort(h+1,h+1+n,cmd);
prermq();
int cnt=0;
for(int i=1;i<=n;i++)
{
int j=i-1;
while(j!=0&&h[j].p==h[i].p)
{
j--;
}
if(j==0)
ans[++cnt]=h[i];
else
{
int d=query(1,j);
if(h[i].d<=d)
{
ans[++cnt]=h[i];
}
}
}
printf("%d\n",cnt);
sort(ans+1,ans+1+cnt,cmd);
for(int i=1;i<=cnt;i++)
{
printf("%d %d\n",ans[i].p,ans[i].d);
}
}
}
相關文章
- HDU 3183 A Magic Lamp (RMQ)LAMPMQ
- HDU 2888 Check Corners(二維RMQ)MQ
- hdu 1754 【線段樹/RMQ】I Hate ItMQ
- HDU 3486 Interviewe(RMQ+二分)ViewMQ
- hdu 4123 樹形DP+RMQMQ
- Hotel G
- HDU 4002Find the maximum(尤拉函式)函式
- RMQMQ
- RMQ模板MQ
- HDU 4622 Reincarnation( 任意區間子串的長度, 字尾陣列+RMQ)陣列MQ
- 淺談RMQMQ
- RMQ求lcaMQ
- RMQ演算法MQ演算法
- 線段樹--RMQMQ
- hdu5289||2015多校聯合第一場1002貪心+RMQMQ
- DarkHotel定向攻擊樣本分析
- 3339: Rmq ProblemMQ
- $RMQ$問題($ST$表)MQ
- RMQ的SSL配置最佳實踐MQ
- RMQ_第一彈_Sparse TableMQ
- RMQ問題的各種解法MQ
- 【RMQ】poj 3264 Balanced LineupMQ
- 學習筆記----RMQ演算法筆記MQ演算法
- POJ3264 Balanced Lineup【RMQ】MQ
- 資料結構——RMQ(ST表)問題資料結構MQ
- Hotel Tonight:移動應用是旅遊業的未來
- jQuery find()jQuery
- find 命令
- find命令
- POJ 3264 Balanced Lineup【RMQ問題】MQ
- UVA 11235-Frequent values(RMQ)MQ
- HotelTrail:酒店如何通過智慧方法來分析顧客反饋?AI
- find -newer命令
- JavaScript find()方法JavaScript
- Linux findLinux
- How to find dependency
- find process by port
- POJ 3264 Balanced Lineup(簡單的RMQ)MQ