poj 2726 Holiday Hotel (排序&&单调栈)
来源:互联网 发布:淘宝 买药 编辑:程序博客网 时间:2024/05/18 00:18
Holiday Hotel
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 8098 Accepted: 3176
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:
- Any hotel which is closer to the seashore than M will be more expensive than M.
- 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
5300 100100 300400 200200 400100 5000
Sample Output
2
Source
Beijing 2005
一个简单的排序题,读懂题意之后应该就差不多了,要理解题目给定的两个条件,一个排序就可以做出来;
题目的原意是,比m离海边更近的旅馆,价钱比m更贵,比m离海边更远的旅馆,价钱比m便宜;
题目意思可以转化为,两个旅馆距离不相等时,
那么任意比m距离近的旅馆都比m的价格贵的m 一定是candidate hotel 的,
两个旅馆距离相等的时候,就保留价格低的那个;
这样我们就能够得出一次排序的排序方法;
下面是代码;
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=10001;struct edge{ int dis,cost;}map[maxn];bool cmp(edge x,edge y)//按照距离从小到大排序,距离相等的按照价格排序{ if(x.dis == y.dis){ return x.cost < y.cost; } return x.dis < y.dis;}int main(){ int n,i,ans,count; while(scanf("%d",&n)&&n) { ans=maxn; count=0; for(i=0;i<n;i++) scanf("%d%d",&map[i].dis,&map[i].cost); sort(map,map+n,cmp); for(i=0;i<n;i++) { if(map[i].cost<ans) { count++; ans=map[i].cost; } } printf("%d\n",count); } return 0;}不用排序的算法;
只要将距离相同的hotel中价格最低的存下来,数组的序号就是距离。hotel[distance]=min(price);再统计数列中非递增的个数就是结果。
#include <cstdio>#include <cstring>const int maxn=10001;int map[maxn];int main(){ int n,i,dis,cost,len,j,count,min; while(scanf("%d",&n)&&n) { len=0; count=1; for(i=0;i<maxn;i++) map[i]=maxn; for(i=0;i<n;i++) { scanf("%d%d",&dis,&cost); if(map[dis]>cost) map[dis]=cost;//保存最低的价格 if(len<dis) len=dis; } i=0; while(map[i]==maxn) i++; min=map[i]; for(j=i+1;j<=len;j++) { if(map[j]<min) { count++;//求非递增的个数 min=map[j]; } } printf("%d\n",count); } return 0;}维护一个单调栈,先按照
距离从小到大排序,距离相等的按照价格排序
#include <cstdio>#include <cstring>#include <algorithm>#include <stack>using namespace std;const int maxn=10001;struct edge{ int dis,cost;}map[maxn];bool cmp(edge x,edge y){ if(x.dis == y.dis){return x.cost < y.cost;}return x.dis < y.dis;}int main(){ int n,i; while(scanf("%d",&n)&&n) { stack<edge>s; for(i=0;i<n;i++) scanf("%d%d",&map[i].dis,&map[i].cost); sort(map,map+n,cmp); s.push(map[0]); for(i=1;i<n;i++) { if(s.size()&& map[i].cost<s.top().cost)//比较与栈顶元素的大小,比栈顶元素小就进栈 s.push(map[i]); } printf("%d\n",s.size()); } return 0;}
1 0
- poj 2726 Holiday Hotel (排序&&单调栈)
- POJ-2726-Holiday Hotel
- poj 2726Holiday Hotel
- POJ 2726 Holiday Hotel 可能会
- POJ 2726 Holiday Hotel 一道水题
- (使用STL自带的排序功能进行排序7.3.7)POJ 2726 Holiday Hotel(结构体排序)
- POJ2726 Holiday Hotel ACM解题报告(贪心)
- Poj 2082(单调栈)
- poj 2559(单调栈)
- POJ 2082 (单调栈)
- 单调栈(poj -- 2559)
- (poj 3667) Hotel(区间更新)
- POJ 3667 Hotel(线段树)
- POJ 3667 Hotel(线段树)
- poj 3667 Hotel(线段树)
- poj 3667 线段树(Hotel)
- poj 3667 Hotel(线段树)
- poj 3667 Hotel(区间合并)
- Unity 编译顺序及原理
- UVA10714
- Shell常识--校验和、核实以及加密--总结自《Linux Shell 脚本攻略》
- Spiral Matrix
- [Android View] QuickReturn (自动隐藏header/footer view)
- poj 2726 Holiday Hotel (排序&&单调栈)
- Openstack中创建虚拟机不能够ping通虚拟路由端口
- poj 3268 Silver Cow Party
- N的阶乘 有多少个末尾0
- 操作系统、计算机网络、设计模式、Linux编程,数据结构总结
- 程序员的人生也需要有高人点化
- 数据结构-【栈】的链式存储和顺序存储
- sdut 面向对象程序设计上机练习五(简单类和对象)
- zoj 3817 Chinese Knot(hash+暴力)