poj 1065 Wooden Sticks

  
    
/*
先按l排序 再每次找出一段最长的递增序列!
*/
#include < stdio.h >
#include
< stdlib.h >
#include
< string .h >
struct stick{
int l, w;
}st[
50010 ];

bool used[ 50010 ];

int cmp( const void * a, const void * b){
stick
* s1 = (stick * )a, * s2 = (stick * )b;
if (s1 -> l == s2 -> l)
return s1 -> w - s2 -> w;
return s1 -> l - s2 -> l;
}
int main(){
int t ,n;
scanf(
" %d " , & t);
while ( t -- ){
memset(used,
false , sizeof (used));
scanf(
" %d " , & n);
for ( int i = 0 ; i < n; ++ i){
scanf(
" %d%d " , & st[i].l, & st[i].w);
}
qsort(st, n,
sizeof (st[ 0 ]), cmp);
// for(int i = 0; i < n; ++i){
// printf("%d %d\n",st[i].l, st[i].w);
// }
int i,j, ans = 0 ;
stick temp;
for (i = 0 ; i < n; ++ i){//关键代码
if ( ! used[i] ){
ans
++ ;
used[i]
= true ;
temp
= st[i];
for (j = i + 1 ; j < n; j ++ ){
if (st[j].l >= temp.l && st[j].w >= temp.w && ! used[j]){
temp
= st[j];
used[j]
= true ;
}
}
}
}
printf(
" %d\n " ,ans);
}
return 0 ;
}

你可能感兴趣的:(poj)