zoukankan      html  css  js  c++  java
  • Wooden Sticks

    Wooden Sticks

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 6563    Accepted Submission(s): 2707


    Problem Description
    There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some time, called setup time, for the machine to prepare processing a stick. The setup times are associated with cleaning operations and changing tools and shapes in the machine. The setup times of the woodworking machine are given as follows: 

    (a) The setup time for the first wooden stick is 1 minute. 
    (b) Right after processing a stick of length l and weight w , the machine will need no setup time for a stick of length l' and weight w' if l<=l' and w<=w'. Otherwise, it will need 1 minute for setup. 

    You are to find the minimum setup time to process a given pile of n wooden sticks. For example, if you have five sticks whose pairs of length and weight are (4,9), (5,2), (2,1), (3,5), and (1,4), then the minimum setup time should be 2 minutes since there is a sequence of pairs (1,4), (3,5), (4,9), (2,1), (5,2).
     
    Input
    The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case consists of two lines: The first line has an integer n , 1<=n<=5000, that represents the number of wooden sticks in the test case, and the second line contains n 2 positive integers l1, w1, l2, w2, ..., ln, wn, each of magnitude at most 10000 , where li and wi are the length and weight of the i th wooden stick, respectively. The 2n integers are delimited by one or more spaces.
     
    Output
    The output should contain the minimum setup time in minutes, one per line.
     
    Sample Input
    3 5 4 9 5 2 2 1 3 5 1 4 3 2 2 1 1 2 2 3 1 3 2 2 3 1
     
    Sample Output
    2 1 3
     
     
    此题排序之后,问题就可以简化,(假设按照长度不等时长度排,长度等是按照重量排,我假设按照从大到小来排!)即求排序后的所有的重量值最少能表示成几个集合。长度就不用再管了,从数组第一个数开始遍历,只要重量值满足条件,那么这两个木棍就满足条件!
    还是需要注意一些问题的,比如第一次的代码里面标记就弄的很麻烦用single来标记,结果过不了。在第二次里面就用flag[]的数组来标记简化了,然而发现还是过不了。找了半天结果在输入想 1 5 1 6  和1 6 1 5的结果不一样,前一个是1,后一个是2.这才发现是快排的cmp()函数写错了,忽略了一种情况(当长度相同时,体重也要比较)。
    第一次的麻烦代码:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define SIZE 5005
     struct Sticks
    {
        int length;
        int weight;
    };
     Sticks  stick[SIZE];
     int cmp(const void *a,const void *b)
     {
          return (*(Sticks *)a).length>(*(Sticks *)b).length ? 1:-1;
     }
    int main()
    {
        int T,n,i,j;
        int time;
        int max,single;
        scanf("%d",&T);
        while(T--){
            memset(stick,0,sizeof(struct Sticks)*SIZE);
            time=0;
            scanf("%d",&n);
            for(i=1;i<=n;i++){
                scanf("%d %d",&stick[i].length,&stick[i].weight);
            }
            qsort(stick+1,n,sizeof(stick[0]),cmp);
            for(i=1;i<=n;i++)
            {
                max=stick[i].weight;
                single=0;
                for(j=i;j<=n;j++)
                {
                    if(max<=stick[j].weight&&max!=0){
                        max=stick[j].weight;
                        stick[j].weight=0;
                        single=1;
                    }
                    if(max==stick[i].weight&&max!=0){single=1;}
                }
                if(single==1){time++;}
            }
            printf("%d
    ",time);
        }
    }
    View Code

    第二次改进后的代码:

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define SIZE 5005
     struct Sticks
    {
        int length;
        int weight;
    };
     Sticks  stick[SIZE];
     int cmp(const void *a,const void *b)
     {
          return (*(Sticks *)a).length>(*(Sticks *)b).length ? 1:-1;
     }
    int main()
    {
        int T,n,i,j;
        int time;
        int max;
        int flag[SIZE];
        scanf("%d",&T);
        while(T--){
            memset(stick,0,sizeof(struct Sticks)*SIZE);
            time=0;
            scanf("%d",&n);
            for(i=1;i<=n;i++){
                scanf("%d %d",&stick[i].length,&stick[i].weight);
                flag[i]=0;
            }
            qsort(stick+1,n,sizeof(stick[0]),cmp);
            for(i=1;i<=n;i++)
            {
                if(flag[i]==1)  continue;
                max=stick[i].weight;
                for(j=i+1;j<=n;j++)
                {
                    if(max<=stick[j].weight&&flag[j]==0){
                        max=stick[j].weight;
                        flag[j]=1;
                    }
    
                }
                time++;
            }
            printf("%d
    ",time);
        }
        return 0;
    }
    View Code

    第三次的正确代码:

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define SIZE 5005
     struct Sticks
    {
        int length;
        int weight;
    };
     Sticks  stick[SIZE];
     /*int cmp(const void *a,const void *b)
     {
          return (*(Sticks *)a).length>(*(Sticks *)b).length ? 1:-1;
     }*/
     int cmp(const void *a,const void *b){
        Sticks c=*(Sticks *)a;
        Sticks d=*(Sticks *)b;
        if(c.length==d.length)
            return c.weight-d.weight;
        return c.length-d.length;
    }
    int main()
    {
        int T,n,i,j;
        int time;
        int max;
        int flag[SIZE];
        scanf("%d",&T);
        while(T--){
            memset(stick,0,sizeof(struct Sticks)*SIZE);
            time=0;
            scanf("%d",&n);
            for(i=1;i<=n;i++){
                scanf("%d %d",&stick[i].length,&stick[i].weight);
                flag[i]=0;
            }
            qsort(stick+1,n,sizeof(stick[0]),cmp);
            for(i=1;i<=n;i++)
            {
                if(flag[i]==1)  continue;
                max=stick[i].weight;
                for(j=i+1;j<=n;j++)
                {
                    if(max<=stick[j].weight&&flag[j]==0){
                        max=stick[j].weight;
                        flag[j]=1;
                    }
    
                }
                time++;
            }
            printf("%d
    ",time);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    gc buffer busy解释
    验证db time
    如何优化log file sync
    客户数据库出现大量cache buffer chains latch
    一份awr分析
    Statspack报告中Rollback per trans过高怎么办
    awr分析要点记录
    Oracle AWR报告及统计数据之DB Time说明
    Oracle 相关视图tips
    struts2 action 乱码
  • 原文地址:https://www.cnblogs.com/renqiqiang/p/5317634.html
Copyright © 2011-2022 走看看