我找不出来哪里错了.
c++做法, 用了一个sort排序(懒懒排序), 其他的用C语言也可以实现.希望这个代码能帮到你.
#include <cstdio>
#include <algorithm>
const int maxn = 5005;
struct node{int l, w;} a[maxn];
int cmp(const node &x, const node &y) {
if(x.l == y.l) return x.w < y.w;
return x.l < y.l;
}
int main() {
int t, n;
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
for(int i = 0; i < n; i++) scanf("%d%d", &a[i].l, &a[i].w);
std::sort(a, a + n, cmp);//此处用了c++的sort排序, 其他的都是能用c语言实现的
int ans = 0;
for(int i = 0; i < n; i++) {
if(a[i].w) {
ans++;
int p = a[i].w;
for(int j = i+1; j < n; j++) {
if(p <= a[j].w) {
p = a[j].w;
a[j].w = 0;
}
}
}
}
printf("%d\n", ans);
}
return 0;
}