Holiday Hotel
 
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8302   Accepted: 3249

Description

Mr. and Mrs. Smith are going to the seaside for their holiday. Before they start off, they need to choose a hotel. They got a list of hotels from the Internet, and want to choose some candidate hotels which are cheap and close to the seashore. A candidate hotel M meets two requirements: 
  1. Any hotel which is closer to the seashore than M will be more expensive than M.
  2. Any hotel which is cheaper than M will be farther away from the seashore than M.

Input

There are several test cases. The first line of each test case is an integer N (1 <= N <= 10000), which is the number of hotels. Each of the following N lines describes a hotel, containing two integers D and C (1 <= D, C <= 10000). D means the distance from the hotel to the seashore, and C means the cost of staying in the hotel. You can assume that there are no two hotels with the same D and C. A test case with N = 0 ends the input, and should not be processed.
 

Output

For each test case, you should output one line containing an integer, which is the number of all the candidate hotels.
 

Sample Input


Sample Output


-------------------------------------------------

  分析:这道题折腾了我一晚上也没AC,直到第二天早上起来才恍然大悟,心里暗骂出题人100遍,网上有人说貌似是楼教主出的,好吧我承认一开始我对题的理解有问题。首先,这道题的模型很简单,即任何比M旅店离海边近的旅店都比M酒店要贵,至于第二个条件,其实就是第一个条件的逆否命题,用来迷惑你的,嘿嘿!现在的关键是我们如何理解这个条件并把它转化为数学语言,你可能二话不说直接写出了这个数学形式的逻辑语句:∀i,if Di<DM ⇒ Ci>CM ,能写出来这个,证明你高一数学学的很好,跟我一样好。但是我们都犯了一个错误,那就是没有结合题意去讨论特殊情形。首先,我们看一下样例输入,发现有两个旅店距离海边都为100,它们同是离海边最近的旅店,但是它们的价格是不一样的,Smith夫妇选旅店会把它们两个同时作为候选项吗?显然不会!距离相同时,它们当然只会把更便宜的作为候选项,贵的那个直接抛弃!而对于我们之前写的表达式,显然它们两个是都满足的,原因是我们并没有考虑到距离相同时的情形,当然如果出题人好心的话也应该指出这种情况。修改后的逻辑语句:∀i,if Di≤DM ⇒ Ci>CM 

    接下来的思路就明朗了,我们依据离海边的距离对所有旅店进行排序,这里我们使用的是修改后的快速排序(即对D进行排序,C跟随,当然把旅店定义成结构体或类更方便),当两个酒店离海边距离相同时,我们把便宜的那个排在前面,然后我们就可以把D看成自变量x,C看成因变量y,初始时就已经有一个满足条件的旅店M(因为没有比排在最前面的那个旅店离海边更近并且还更便宜的了),即初始时candidate_n = 1,我们从第二个旅店向后扫描C,每找到一个CM比之前的C都小,计数器candidate_n就加1,最后输出candidate_n即可。

  源码

#include<iostream>
using namespace std; void exchange(int A[], int i, int j) // 交换A[i]和A[j]
{
int temp = A[i];
A[i] = A[j];
A[j] = temp;
} int partition(int A[], int B[], int left, int right)// 划分函数
{
int pivot_A = A[right];    // 选择子数组最后一个元素作为基准
int pivot_B = B[right];
int tail = left - ;     // tail为小于基准的子数组最后一个元素的索引
for (int i = left; i < right; i++)    // 遍历基准以外的其他元素
{
if (A[i] < pivot_A)   // 把小于基准的元素放到前一个子数组中
{
tail++;
exchange(A, tail, i);
exchange(B, tail, i);
}
if (A[i] == pivot_A && B[i] < pivot_B) // 当两个酒店离海边距离相同时,我们把便宜的那个排在前面
{
tail++;
exchange(A, tail, i);
exchange(B, tail, i);
}
}
exchange(A, tail + , right);    // 最后把基准放到前一个子数组的后边,剩下的子数组既是大于基准的子数组
exchange(B, tail + , right);
return tail + ;    // 返回基准的索引
} void quicksort(int A[], int B[], int left, int right)
{
int pivot_index = A[right];
if (left < right)
{
pivot_index = partition(A, B, left, right);
quicksort(A, B, left, pivot_index - );
quicksort(A, B, pivot_index + , right);
}
} int main()
{
int N,candidate_n = ;
while (scanf("%d", &N) && N) // 输入酒店的个数N,N为0时直接结束程序
{
int* D = (int *)malloc(N * sizeof(int));// 为了节省空间这里我们根据N的大小动态分配内存
int* C = (int *)malloc(N * sizeof(int));
for (int i = ; i < N; i++)
{
scanf("%d%d", &D[i], &C[i]);
}
quicksort(D, C, , N - ); // 调用修改后的快排算法依据D对旅店进行排序,C跟随
int min = C[];
candidate_n = ; // 排在最前面的旅店必然满足条件
for (int i = ; i < N; i++)
{
if (C[i] < min) // 每找到一个旅店比前面的都便宜
{
min = C[i];
candidate_n++; // 计数器加1
}
}
printf("%d\n", candidate_n);
candidate_n = ;
free(D); // 释放空间
free(C);
}
return ;
}

  显然,程序的时间复杂度O(nlogn),运行结果如下:

POJ-2726-Holiday Hotel的更多相关文章

  1. POJ 题目3667 Hotel(线段树,区间更新查询,求连续区间)

    Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13805   Accepted: 5996 Descriptio ...

  2. POJ 3667 & 1823 Hotel (线段树区间合并)

    两个题目都是用同一个模板,询问最长的连续未覆盖的区间 . lazy代表是否有人,msum代表区间内最大的连续长度,lsum是从左结点往右的连续长度,rsum是从右结点往左的连续长度. 区间合并很恶心啊 ...

  3. POJ 2726

    #include <iostream> #include <algorithm> #define MAXN 10005 using namespace std; struct ...

  4. A过的题目

    1.TreeMap和TreeSet类:A - Language of FatMouse ZOJ1109B - For Fans of Statistics URAL 1613 C - Hardwood ...

  5. 线段树(区间合并) POJ 3667 Hotel

    题目传送门 /* 题意:输入 1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 输入 2 a b:将[a,a+b-1]的房间清空 线段树(区间合并):lsum[]统计从左端点起最长连续空房间 ...

  6. poj 3667 Hotel (线段树)

    http://poj.org/problem?id=3667 Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 94 ...

  7. POJ P3667 Hotel——solution

    Description The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and e ...

  8. POJ 3667 Hotel(线段树)

    POJ 3667 Hotel 题目链接 题意:有n个房间,如今有两个操作 1.找到连续长度a的空房间.入住,要尽量靠左边,假设有输出最左边的房间标号,假设没有输出0 2.清空[a, a + b - 1 ...

  9. 整理下线段树吧 poj hotel

    除了上次的新学的有 区间更新 延迟更新  区间合并 先说下区间更新以及延迟更新吧 既然是对区间的维护 在求解一些问题的时候 有的时候没有必要对所有的自区间都进行遍历 这个时候 延迟标记就派上用场了 ( ...

随机推荐

  1. C++ 高质量编程附录试题

    附录B :C++/C试题 本试题仅用于考查C++/C程序员的基本编程技能.内容限于C++/C常用语法,不涉及数据结构.算法以及深奥的语法.考试成绩能反映出考生的编程质量以及对C++/C的理解程度,但不 ...

  2. ddl dml dcl

    DCL数据控制语言 创建临时表空间 create temporary tablespace user_temp tempfile 'E:/oracle/product/10.1.0/oradata/o ...

  3. Android 系统ID介绍

    Android上系统ID有很多,本文只介绍常用的ANDROID ID.DEVICE ID.IMEI/MEID.WIFI/BT ADDRESS等几个,本文介绍这些ID的数据格式.长度及一些基本知识. 一 ...

  4. HTML 标题<h1>-<h6>

    定义和用法 <h1> - <h6> 标签可定义标题.<h1> 定义最大的标题.<h6> 定义最小的标题. 由于 h 元素拥有确切的语义,因此请您慎重地选 ...

  5. You Only Live Once

    从做 PreAngel 以来,每年我都会抽空去美国一两次,主要是在硅谷(湾区)一带见见当地的朋友,他们主要有 VC.创业者.斯坦福和伯克利的学生创业组织负责人.无线科技领域的各种组织机构负责人等,我一 ...

  6. 爱壁纸 站立会议(六)--Spring阶段总结会议

    爱壁纸 站立会议(六)--Spring阶段总结会议 一.会议时间 2014年4月15日 星期三 21:00-21:20 今天是spring阶段最后一天,大家都对这一星期的任务概况做出了总结所以时间稍微 ...

  7. Codeforces Round #379 (Div. 2) 解题报告

    题目地址 本次CF是在今天早上深夜进行,上午有课就没有直接参加.今天早上上课坐到后排参加了virtual participation.这次CF前面的题目都非常的水,不到10分钟就轻松过了前两题,比较郁 ...

  8. (DFS)codevs1004-四子连棋

    题目地址 方法是建立dfs,并在其中加入pre变量,记录之前移动的是W还是B.外面套for循环,从1步开始逐次递增,直到在i步时可以走完(dfs返回1),break退出循环,即为最短步. 本题的关键主 ...

  9. C语言基础--二维数组

    二维数组概念: 数组中的每一个元素又是一个数组, 那么这个数组就称之为二维数组,二维数组是特殊的一维数组. 二维数组格式: 元素类型 数组名称[一维数组的个数][每个一维数组的元素个数]; 元素类型 ...

  10. sqoop将关系型数据库的表导入hive中

    1.sqoop 将关系型数据库的数据导入hive的参数说明: