HDU 3193 Find the hotel(RMQ)

题目地址:点击打开链接


题意:给你n个旅馆的p(价格)和d(距离值),然后要你输出所有符合条件的旅馆。条件是没有其他任何一个旅馆的p和d

时小于该旅馆的p和d。( 1 <= n <= 10000, 0 <= p, d <= 10000)


思路:一看数据量不是特别大,暴力n^2也许会过。。于是写了发真过了,数据太水吧。


正解应该是RMQ预处理(nlogn), 价格区间的最小距离。a[p]=d表示所有价格为p的旅店中最短的距离是d。要求是找到

p和d没有同时小于该旅店的旅店,那么枚举到某个旅店时只要查询价格为(0,p-1)内的距离最小值判断是否小于自

身的d就以知道该旅店是否满足要求


注意价格可能为0,查询是(0,p-1)会RE,所以一开始就把p和d都加1.


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1e4+5;
const int INF = 0x3f3f3f3f;
int a[maxn], p[maxn], d[maxn], mm[maxn], dpMin[maxn][20], n;
struct node
{
    int p, d;
    node() {}
    node(int pp, int dd): p(pp), d(dd) {}
    bool operator <(const node &a) const
    {
        if(p == a.p) return d < a.d;
        return p < a.p;
    }
}ans[maxn];

void initRMQ()
{
    for(int i = 0; i < maxn; i++)
        dpMin[i][0] = a[i];
    for(int j = 1; j <= mm[maxn-1]; j++)
        for(int i = 0; i+(1<<j)-1 < maxn; i++)
            dpMin[i][j] = min(dpMin[i][j-1], dpMin[i+(1<<(j-1))][j-1]);
}

int RMQ(int l, int r)
{
    int k = mm[r-l+1];
    return min(dpMin[l][k], dpMin[r-(1<<k)+1][k]);
}

int main(void)
{
    mm[0] = -1;
    for(int i = 1; i < maxn; i++)
        mm[i] = ((i&(i-1))==0) ? mm[i-1]+1 : mm[i-1];
    while(cin >> n)
    {
        memset(a, INF, sizeof(a));
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d", &p[i], &d[i]);
            p[i]++, d[i]++;
            a[p[i]] = min(a[p[i]], d[i]);
        }
        initRMQ();
        int index = 0, cnt = 0;
        for(int i = 1; i <= n; i++)
        {
            int tmin = RMQ(0, p[i]-1);
            if(tmin >= d[i]) ans[index++] = node(p[i], d[i]), cnt++;
        }
        sort(ans, ans+index);
        printf("%d\n", cnt);
        for(int i = 0; i < index; i++)
            printf("%d %d\n", ans[i].p-1, ans[i].d-1);
    }
    return 0;
}


Find the hotel

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


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.
 

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.
 

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
 



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值