这题刚开始直接dp求最长非递减子序列,再加上next数组,于是WA。结合网上的代码,才发现,其实就是求最少组的非递减子序列,直接从小到大扫描就可以了。自己还是太水了,后悔大学不努力呀!
/*
* zoj_1025,就是求最少组的非递减子序列,用贪心就可以,不用dp求最长非递减子序列
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 5001
struct Stick {
int len, w;
};
Stick stick[N];
short flag[N];
int cmp( const Stick& a, const Stick& b ) {
return ( a.len < b.len || ( a.len == b.len && a.w < b.w ) );
}
int main()
{
int i, j, tmp, t, n, ans;
scanf("%d", &t );
while ( t -- ) {
scanf("%d", &n);
for ( i = 0; i < n; ++ i )
scanf("%d%d", &stick[i].len, &stick[i].w );
sort( stick, stick+n, cmp );
memset( flag, 0, sizeof(flag) );
ans = 0;
for ( i = 0; i < n; ++ i ) {
if ( 0 == flag[i] ) {
++ ans;
tmp = stick[i].w;
for ( j = i+1; j < n; ++ j ) {
if ( 0 == flag[j] && stick[j].w >= tmp ) {
flag[j] = 1;
tmp = stick[j].w;
}
}
}
}
printf("%d\n", ans );
}
return 0;
}