hdu1051贪心

题目链接点击打开链接


第一个木头需要1min的启动时间,

其余的木头如果满足 l<=l' and w<=w'则不需要启动时间

贪心,首先按照(1)n1.l < n2.l

                         (2)n1.w < n2.w

进行排序

然后贪心的去找每一个满足 l<=l' and w<=w'的序列,用vis[i]标记已经找过的

如{1,2},{1,3},{2,2},{3,5},{5,3}

第一遍找出{1,2},{1,3},{3,5}

第二遍找出{2,2},{3,5}

#include <stdio.h>
#include <algorithm>
#include <memory.h>

using namespace std;

const int maxn = 5000 + 5;

struct node{
    int l, w;
    
}sticks[maxn];

bool vis[maxn];

bool cmp(const node &n1, const node &n2)
{
    if (n1.l < n2.l)return true;
    else if (n1.l == n2.l){
        return n1.w < n2.w;
    }
    return false;
}

int main()
{
    //freopen("in.txt", "r", stdin);
    int T, n, ans;
    scanf("%d", &T);
    while (T--){
        ans = 0;
        memset(vis, 0, sizeof(vis));
        scanf("%d", &n);
        for (int i = 0; i < n; ++i){
            scanf("%d %d", &sticks[i].l, &sticks[i].w);
        }
        sort(sticks, sticks + n, cmp);

        for (int i = 0; i < n; ++i){
            if (!vis[i]){
                int weight = sticks[i].w;
                for (int j = i + 1; j < n; ++j){
                    if (!vis[j] && weight <= sticks[j].w) {
                        vis[j] = true;
                        weight = sticks[j].w;
                    }
                }
                ans++;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值