题目链接:http://poj.org/problem?id=1065
解题思路:贪心,先从小到大排序,然后依次检查每个stick后面的哪些stick可以去掉。
///2014.6.21
///poj1065
#include <cstdio>
#include <algorithm>
#include <cstring>
int T,n;
struct stick{
int l,w;
};
stick sti[5100];
bool used[5100];
bool cmp(stick a, stick b){
if(a.l == b.l)
return a.w < b.w;
return a.l < b.l;
}
int main(){
// freopen("in","r",stdin);
// freopen("out","w",stdout);
scanf("%d",&T);
while( T-- ){
scanf("%d",&n);
for(int i=0 ; i<n ; i++)
scanf("%d %d",&sti[i].l,&sti[i].w);
std::sort(sti,sti+n,cmp);
memset(used,false,sizeof(used));
int num = 0;
for(int i=0 ; i<n ; i++){
if( !used[i] ){
num++;
int tempW = sti[i].w;
for(int j=i+1 ; j<n ; j++){
if( !used[j] && sti[j].w >= tempW )
used[j] = true,tempW = sti[j].w;
}
}
}
printf("%d\n",num );
}
return 0;
}
用python写的测试数据生成器:
import random
import sys
fileout = open("in","w")
sys.stdout = fileout
T = int(random.random() * 10)
print T
i = 0
while i<T :
n = int(random.random() * 10)
print n
j = 0
str1 = ""
while j<n :
l = random.randint(0,100)
w = random.randint(0,100)
str1 = str1 + str(l) + ' ' + str(w) + ' '
j = j+1
print str1
i = i+1
附上一组测试数据:
7
4
85 74 56 25 10 30 98 81
2
53 28 34 59
6
35 82 57 36 5 2 84 40 44 58 87 19
6
66 78 60 35 51 72 34 56 98 78 33 100
3
22 62 7 22 59 59
9
38 63 69 75 90 35 33 77 77 74 42 84 10 55 72 40 29 36
8
51 24 23 24 35 20 79 32 79 58 37 14 86 10 16 36
结果:
2
2
4
3
2
4
5