zoukankan      html  css  js  c++  java
  • 洛谷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
    分析:这个题我们可以用贪心来解,题目要求是长宽都小的可以用继续,我们很难做到两个方面同时比较,就可以先固定好长度排序,然后根据宽度来确定次数
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 struct node{
     4     int l,w;
     5 }a[5010];
     6 bool cmp(const node &a,const node &b){
     7     if(a.l==b.l)    return a.w>b.w;
     8     return a.l>b.l;
     9 }
    10 int b[5010];
    11 int main(){
    12     int n;
    13     scanf("%d",&n);int wnow=0;
    14     for(int i=0;i<n;i++)scanf("%d%d",&a[i].l,&a[i].w);
    15     int ans=1;//cout<<233;
    16     sort(a,a+n,cmp);    
    17     for(int i=0;i<n;i++){
    18         if(!b[i]){
    19         wnow=a[i].w;//cout<<233;
    20         for(int j=i+1;j<n;j++){
    21             if(!b[j]){//保证这个时候这个木棍还没用 
    22                 if(wnow>=a[j].w){
    23                     wnow=a[j].w;
    24                     b[j]=1;
    25                 } 
    26             }
    27         }
    28     }}
    29     //cout<<2333<<endl;
    30     for(int i=1;i<n;i++){
    31         if(!b[i])    ans++;
    32     }
    33     cout<<ans<<endl;
    34     return 0;
    35 }
  • 相关阅读:
    python-super方法
    python--mixin
    python中将输出写入文件
    一致性hash算法
    mysql之触发器trigger
    python内置函数
    python3.5+Django2.2+pymysql+mysql
    File "D:PythonPython37-32libsite-packagesdjangoviewsdebug.py", line 332, in get_traceback_html   t = DEBUG_ENGINE.from_string(fh.read())
    RuntimeError: You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining POST data. Change your form to po
    python:面向对象
  • 原文地址:https://www.cnblogs.com/qingjiuling/p/9702583.html
Copyright © 2011-2022 走看看