uvalive 2322 Wooden Sticks(贪心)

942 篇文章 2 订阅
119 篇文章 0 订阅

题目连接:2322 Wooden Sticks


题目大意:给出要求切的n个小木棍 , 每个小木棍有长度和重量,因为当要切的长度和重量分别大于前面一个的长度和重量的时候可以不用调整大木棍直接切割, 否则要进行调整。现在要求求出一个序列, 使得调整的次数最少, 输出调整的次数。


解题思路:将n个小木棍先按照 长度和重量的大小排序,然后按照顺序将小木棍分堆,可入堆的要求是长度和重量大于当前这个堆的长度和重量,入堆之后, 要将新的木棍的属性赋值个这个堆, 如果当前所有堆都没法放下这个木棍, 就得单独放成一个堆。最后的堆数就是要求的调整次数。


#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 5005;

struct stick {
    int len;
    int wei;
}tmp[N], rec[N];

bool cmp(const stick& a, const stick& b) {
    if (a.len != b.len)
	return a.len < b.len;
    else
	return a.wei < b.wei;
}

int main() {
    int cas, n, cnt, begin;
    scanf("%d", &cas);
    while (cas--) {
	cnt = 0;
	memset(tmp, 0, sizeof(tmp));
	memset(rec, 0, sizeof(rec));

	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	    scanf("%d %d", &tmp[i].len, &tmp[i].wei);

	sort(tmp, tmp + n, cmp);

	for (int i = 0; i < n; i++) {
	    int flag = 1;
	    for (int j = 0; j < cnt; j++) {
		if (tmp[i].len >= rec[j].len && tmp[i].wei >= rec[j].wei) {
		    rec[j] = tmp[i];
		    flag = 0;
		    break;
		}
	    }

	    if (flag)	rec[cnt++] = tmp[i];
	}
	printf("%d\n", cnt);
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值