题目描述
库特君的面条
时间限制:C/C++语言 1000MS;其他语言 3000MS
内存限制:C/C++语言 131072KB;其他语言 655360KB
题目描述:
库特君在吃面条!
他将面条放在了数轴上,每根面条对应数轴上的两个点a和b,他想知道在任意两根面条不重叠(端点可以重叠)的情况下最多能选出多少根面条。
1 <= n <= 100
-999 <= a
代码
C++代码
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
#define process(i,a,b) for(i=a;i<=b;i++)
const int maxn = 100007;
int i, j, k, l, t, n, m, dst, r;
struct NODE{
int a, b, c;
}a[maxn];
bool cmp(NODE x, NODE y)
{
return x.b<y.b || x.b == y.b&&x.a<y.a;
}
int main()
{
scanf("%d", &n);
process(i, 1, n)scanf("%d%d", &a[i].a, &a[i].b);
sort(a + 1, a + n + 1, cmp);
r = a[1].b;
process(i, 2, n)
{
if (a[i].a >= r){
dst++;
r = a[i].b;
}
}
printf("%d\n", dst + 1);
return 0;
}
Python代码
if __name__ == "__main__":
n = int(raw_input())
d = []
for i in range(n):
(a, b) = (int(i) for i in raw_input().split())
(a, b) = (a, b) if a < b else (b, a)
d.append((a, b))
r = sorted(d, key=lambda x: x[1])
previous = 0
result = 0
for j in range(1, n):
if r[j][0] >= r[previous][1]:
result += 1
previous = j
print(result+1)