E - Wooden Sticks

Subject

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 areassociated with cleaning operations and changing tools and shapes in themachine. 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 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

***********************************************************************************************

题目:
  主要讲的就是加工木板,加工一块木板要一分钟,但是现在就是给出木板的长度和重量。如果长度和重量同时满足小于或者等于某一块木板的时候,他们可以一起加工,时间为1分钟。求最少要多久加工完?


思路就是最大递增子序列的问题,写代码。

***********************************************************************************************

代码如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>

using namespace std;

struct node
{
	int n,m,k;
}a[10005];

bool cmp(node n,node m)
{
	if(n.n!=m.n)
	{
		return n.n<m.n;
	}
	return n.m<m.m;
}

int main()
{
	int n; 
	int m;
    scanf("%d",&n);
	while(n--)
	{
	   scanf("%d",&m);
	   memset(a,0,sizeof(a));
	   for(int i=0;i<m;i++)
	   {
	      scanf("%d%d",&a[i].n,&a[i].m);
	   }
	   sort(a,a+m,cmp);
	   int ans =0;
          //能一起加工的木板
	   for(int i=0;i<m;i++)
	   {
	   	if(a[i].k==1) continue;
	   	  int max = a[i].n;
		  int min = a[i].m;
		  int q = 0;
	   	  for(int j=i+1;j<m;j++)
	   	  {
	   	  	 if(max<=a[j].n && min<=a[j].m && a[j].k==0)
	   	  	 {
	   	  	 	max = a[j].n;
	   	  	 	min = a[j].m;
	   	  	 	a[j].k = 1;
	   	  	 	if(q==0)
	   	  	 	{
	   	  	 		  ans++;
	   	  	 		  a[i].k=1;
	   	  	 		  q=1;
	   	  	 	}
	   	  	 }
	   	  }
	   }
           //不能一起加工的木板
	    for(int i=0;i<m;i++)
	    {
	    	if(a[i].k==0){
	    		ans++;
	    	}
	    }
	   printf("%d\n",ans);
	}
   return 0;
}
***********************************************************************************************
posted @ 2017-07-13 08:33  让你一生残梦  阅读(143)  评论(0编辑  收藏  举报