zoukankan      html  css  js  c++  java
  • [Luogu] P1233 木棍加工

    题目描述

    一堆木头棍子共有n根,每根棍子的长度和宽度都是已知的。棍子可以被一台机器一个接一个地加工。机器处理一根棍子之前需要准备时间。准备时间是这样定义的:

    第一根棍子的准备时间为1分钟;

    如果刚处理完长度为L,宽度为W的棍子,那么如果下一个棍子长度为Li,宽度为Wi,并且满足L>=Li,W>=Wi,这个棍子就不需要准备时间,否则需要1分钟的准备时间;

    计算处理完n根棍子所需要的最短准备时间。比如,你有5根棍子,长度和宽度分别为(4, 9),(5, 2),(2, 1),(3, 5),(1, 4),最短准备时间为2(按(4, 9)、(3, 5)、(1, 4)、(5, 2)、(2, 1)的次序进行加工)。

    输入输出格式

    输入格式:

    第一行是一个整数n(n<=5000),第2行是2n个整数,分别是L1,W1,L2,w2,…,Ln,Wn。L和W的值均不超过10000,相邻两数之间用空格分开。

    输出格式:

    仅一行,一个整数,所需要的最短准备时间。

    输入输出样例

    输入样例#1: 

    5
    4 9 5 2 2 1 3 5 1 4

    输出样例#1:
    2
     

    思路解析

    贪心就好了,特殊办法排个序然后求多个最长不下降子序列。

    很简单适合练手

    Code

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    
    const int MAXN = 5000 + 5;
    
    int n,ans;
    struct Stick {
        int w,l;
    } s[MAXN];
    bool vis[MAXN];
    
    bool cmp(Stick x,Stick y) {
        if(x.l == y.l) {
            return x.w > y.w;
        } return x.l > y.l;
    }
    
    int main() {
        scanf("%d",&n);
        for(int i = 1;i <= n;i++) {
            scanf("%d%d",&s[i].l,&s[i].w);
        }
        sort(s+1,s+1+n,cmp);
        int lastl,lastw;
        for(int i = 1;i <= n;i++) {
            if(vis[i]) continue;
            ans++;
            lastl = s[i].l;
            lastw = s[i].w;
            vis[i] = true;
            for(int j = 1;j <= n;j++) {
                if(vis[j]) continue;
                if(s[j].l <= lastl && s[j].w <= lastw) {
                    lastl = s[j].l;
                    lastw = s[j].w;
                    vis[j] = true;
                }
            }
        }
        printf("%d",ans);
    }
  • 相关阅读:
    JDK 5 ~ 10 新特性倾情整理!
    软件设计师-面向对象
    软件设计师-系统开发基础
    软件设计师-网络与信息安全
    软件设计师-数据库系统
    软件设计师-操作系统
    软件设计师-计算机系统知识
    DbParameter
    软件设计师-算法
    android studio快捷键
  • 原文地址:https://www.cnblogs.com/floatiy/p/9465070.html
Copyright © 2011-2022 走看看