Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 8098 | Accepted: 3176 |
Description
Input
Output
Sample Input
5 300 100 100 300 400 200 200 400 100 500 0
Sample Output
2
Source
那么任意比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; }