贪心之Wooden Sticks ---解题报告

来源:互联网 发布:能在淘宝上买貂皮吗 编辑:程序博客网 时间:2024/05/29 10:08

 

Wooden Sticks

Time Limit : 2000/1000ms(Java/Other)   Memory Limit : 65536/32768K (Java/Other)

Total Submission(s) : 7   AcceptedSubmission(s) : 6

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

There is a pile of n wooden sticks. The length and weightof each stick are known in advance. The sticks are to be processed by awoodworking machine in one by one fashion. It needs some time, called setuptime, for the machine to prepare processing a stick. The setup times are associatedwith cleaning operations and changing tools and shapes in the machine. Thesetup 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 willneed no setup time for a stick of length l' and weight w' if l<=l' andw<=w'. Otherwise, it will need 1 minute for setup.

You are to find the minimum setup time to process a given pile of n woodensticks. For example, if you have five sticks whose pairs of length and weightare (4,9), (5,2), (2,1), (3,5), and (1,4), then the minimum setup time shouldbe 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 testcases (T) is given in the first line of the input file. Each test case consistsof two lines: The first line has an integer n , 1<=n<=5000, thatrepresents the number of wooden sticks in the test case, and the second linecontains n 2 positive integers l1, w1, l2, w2, ..., ln, wn, each of magnitudeat most 10000 , where li and wi are the length and weight of the i th woodenstick, respectively. The 2n integers are delimited by one or more spaces.

Output

The output should contain the minimum setup time inminutes, 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

Source

Asia 2001, Taejon(South Korea)



    题目的大概意思是说,有一堆木材需要加工,第一根木材加工需要1分钟,如果其他的木材的长度和宽度都大于或等于已经加工过的木材,则这根木材的加工不费时间,否则时间也为1分钟。求至少需要用多长时间。

解题思路:

    首先对这堆木材按长度先进行排序,再按照其宽度进行比较。用数组A表示输入的木材,数组B表示这堆木材中当前木材不满足条件的木材。也就是说,我们现在要做的就是,比较B中是否有长度和宽度都小于当前木材的元素。一旦发现存在的,就将宽度更大的替换,否则在B数组中增加元素。

代码如下:

#include<iostream>

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

#include<string.h>

#include<algorithm>

 

using namespacestd;

 

struct stick

{

    int x;

    int y;

};

 

int cmp(sticka,stick b)

{

    if(a.x<b.x)

        return 1;

    else if(a.x==b.x&&a.y<b.y)

        return 1;

    else

        return 0;

}

 

stickst[5005],st0[5005];

int main()

{

    int t,n,i,j,m;

    cin>>t;

    while(t--)

    {

        cin>>n;

        for(i=0;i<n;i++)

        {

            cin>>st[i].x>>st[i].y;

        }

        m =1;

        st0[1].x=st0[1].y=0;

        sort(st,st+n,cmp);

//        for(i=0;i<n;i++)

//            cout<<st[i].x<<''<<st[i].y<<endl;

        for(i=0;i<n;i++)

        {

            for(j=1;j<=m;j++)

            {

               if(st[i].x>=st0[j].x&&st[i].y>=st0[j].y)

                {

                    st0[j].x=st[i].x;

                    st0[j].y=st[i].y;

                   //cout<<st0[j].x<<' '<<st0[j].y<<endl;

                    break;

                }

 

            }

            if(j>m)

            {

                m+=1;

                st0[m].x=st[i].x;

                st0[m].y=st[i].y;

            }

 

        }

        cout<<m<<endl;

    }

    return 0;

}