hdu 3193 Find the hotel(RMQ)

Find the hotel

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


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
 



给出n个hotel的价格和距离 问有多少个hotel 满足一个要求 要求是找不到其他的hotel比这个的价格和距离都小

对于一个hotel的价格p 找出1-p-1范围中d值最小的 如果p对应的d比这个还小 则找不到符合要求的 

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

struct node
{
    int p,d;
    bool operator<(const node &nd)const
    {
        if(p==nd.p)
            return d<nd.d;
        return p<nd.p;
    }
};
node no[MAXN],no2[MAXN];
int dp[MAXN][30],a[MAXN];

void RMQ_init()
{
    for(int i=0;i<10005;i++)
        dp[i][0]=a[i];
    for(int j=1;(1<<j)<=10005;j++)
        for(int i=0;i+(1<<j)-1<10005;i++)
            dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}

int RMQ(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()
{
//    fread;
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<10005;i++)
            a[i]=10010;
        for(int i=0;i<n;i++)
        {
            int p,d;
            scanf("%d%d",&p,&d);
            p++; d++;
            a[p]=min(a[p],d);
            no[i].p=p; no[i].d=d;
        }
        RMQ_init();
        int cnt=0;
        for(int i=0;i<n;i++)
        {
            int p=no[i].p;
            int d=no[i].d;

            if(p==1)
            {
                no2[cnt++]=no[i];
            }
            else
            {
                int mi=RMQ(1,p-1);
                if(d<=mi)
                    no2[cnt++]=no[i];
            }
        }
        printf("%d\n",cnt);
        sort(no2,no2+cnt);
        for(int i=0;i<cnt;i++)
        {
            printf("%d %d\n",no2[i].p-1,no2[i].d-1);
        }
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值