poj2726

按c排序,c相同则按d排序(均从小到大),从左至右遍历,当发现一个的d比前面出现过的最小的d还小那么它一定是解。

View Code
#include <iostream>
#include
<cstdio>
#include
<cstdlib>
#include
<cstring>
#include
<algorithm>
using namespace std;

#define maxn 10005

struct Hotel
{
int c, d;
} f[maxn];

int n, ans;

bool operator <(const Hotel &a, const Hotel &b)
{
if (a.c == b.c)
return a.d < b.d;
return a.c < b.c;
}

void input()
{
for (int i = 0; i < n; i++)
scanf(
"%d%d", &f[i].c, &f[i].d);
}

void work()
{
ans
= 0;
int maxd = 100000000;
for (int i = 0; i < n; i++)
if (f[i].d < maxd)
{
ans
++;
maxd
= f[i].d;
}
}

int main()
{
//freopen("D:\\t.txt", "r", stdin);
while (scanf("%d", &n) != EOF && n != 0)
{
input();
sort(f, f
+ n);
work();
printf(
"%d\n", ans);
}
return 0;
}
原文地址:https://www.cnblogs.com/rainydays/p/1993056.html