poj1065 Wooden Sticks 贪心,提供测试数据

4 篇文章 0 订阅

题目链接: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




  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值