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 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.
思路,将所有的hotel按距离排序,选择价格小于所有排在其前的hotel的
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
struct Hotel
{
int dist;
int cost;
bool operator <(const Hotel& h) const
{
if(dist==h.dist)
return cost<h.cost;
return dist<h.dist;
}
};
int main()
{
int N;
while(scanf("%d",&N)&&N)
{
vector<Hotel> hotels(N);
for(int i=0;i<N;i++)
scanf("%d%d",&hotels[i].dist,&hotels[i].cost);
sort(hotels.begin(),hotels.end());
int minCost=100000;
int ret=0;
for(int i=0;i<N;i++)
{
if(hotels[i].cost<minCost)
{
ret++;
minCost=hotels[i].cost;
}
}
printf("%d\n",ret);
}
return 0;
}