HDU 1051(ZZULI 1609) Wooden St…

来源:互联网 发布:yii2.0框架源码下载 编辑:程序博客网 时间:2024/05/16 08:04

WoodenSticks

Time Limit:1000MS  MemoryLimit:65536K
Total Submit:33 Accepted:9

Description

There is a pile of nwooden sticks. The length and weight of each stick are known inadvance. The sticks are to be processed by a woodworking machine inone by one fashion. It needs some time, called setup time, for themachine to prepare processing a stick. The setup times areassociated with cleaning operations and changing tools and shapesin the machine. The setup times of the woodworking machine aregiven 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 , themachine will need no setup time for a stick of length l' and weightw' 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 nwooden sticks. For example, if you have five sticks whose pairs oflength and weight are ( 9 , 4 ) , ( 2 , 5 ) , ( 1 , 2 ) , ( 5 , 3 ), and ( 4 , 1 ) , then the minimum setup time should be 2 minutessince there is a sequence of pairs ( 4 , 1 ) , ( 5 , 3 ) , ( 9 , 4) , ( 1 , 2 ) , ( 2 , 5 ) .

Input

The input consists of Ttest cases. The number of test cases (T) is given in the first lineof the input file. Each test case consists of two lines: The firstline has an integer n , 1 <= n <=5000 , that represents the number of wooden sticks in the testcase, and the second line contains 2n positive integers l1 , w1 ,l2 , w2 ,..., ln , wn , each of magnitude at most 10000 , where liand wi are the length and weight of the i th wooden stick,respectively. The 2n integers are delimited by one or morespaces.

Output

The output should containthe minimum setup time in minutes, one per line.

SampleInput

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

SampleOutput

213

Source

 

这题纠结的我都不知道什么是贪心,什么是二分匹配了,好郁闷,为了研究这题我还和三多,老三,研究了一整个晚上,最后才弄明白,以后绝不要犯错……

 

贪心嘛,不解释

 

代码:

(这是三多的思想)

C语言: 高亮代码由发芽网提供
#include<stdio.h>
#include<stdlib.h>

structnode{
    int l,w;
}stick[5008];

intcmp(const void *a,const void *b)
{
    struct node *c=(struct node *)a;
    struct node *d=(struct node *)b;
    if(c->l!=d->l)
       returnc->l -d->l;
    else return c->w -d->w;
}

intmain()
{
    int t,i,j,k,n,flag[5008];
    scanf("%d",&t);
    while(t--)
    {
       scanf("%d",&n);
       for(i=0;i<n;i++)
       {
           scanf("%d %d",&stick[i].l,&stick[i].w);
           flag[i]=0;
       }
       qsort(stick,n,sizeof(stick[0]),cmp);
       k=0;
       for(i=0;i<n;i++)
       {
           for(j=0;j<i;j++)
               if(!flag[j]&&stick[i].l>=stick[j].l&&stick[i].w>=stick[j].w)
               {
                   stick[j].l=stick[i].l;
                   stick[j].w=stick[i].w;
                   flag[i]=1;
                   break;
               }
       }
       for(i=0;i<n;i++)
           if(!flag[i])k++;
       printf("%d\n",k);
    }
    return 0;
}
(这个是我的代码)
C语言: 高亮代码由发芽网提供
#include<stdio.h>
#include<stdlib.h>

structnode{
    int l,w;
}stick[5008],map[5008];

intcmp(const void *a,const void *b)
{
    struct node *c=(struct node *)a;
    struct node *d=(struct node *)b;
    if(c->l!=d->l)
       returnc->l -d->l;
    else return c->w -d->w;
}
intmain()
{
    int t,i,j,k,n;
    scanf("%d",&t);
    while(t--)
    {
       scanf("%d",&n);
       for(i=0;i<n;i++)
           scanf("%d %d",&stick[i].l,&stick[i].w);
       qsort(stick,n,sizeof(stick[0]),cmp);
       k=0;
       for(i=0;i<n;i++)
       {
           for(j=0;j<k;j++)
               if(stick[i].l>=map[j].l&&stick[i].w>=map[j].w)
               {
                   map[j].l=stick[i].l;
                   map[j].w=stick[i].w;
                   break;
               }
           if(j>=k)
           {
               map[k].l=stick[i].l;
               map[k].w=stick[i].w;
               k++;
           }
       }
       printf("%d\n",k);
    }
    return 0;
}
PS:三多的62MS,我的31MS ,小得瑟一下


原创粉丝点击