POJ1065 Wooden Sticks
Posted 小蒟蒻
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ1065 Wooden Sticks相关的知识,希望对你有一定的参考价值。
Wooden Sticks
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 23403 | Accepted: 10053 |
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 ( 9 , 4 ) , ( 2 , 5 ) , ( 1 , 2 ) , ( 5 , 3 ) , and ( 4 , 1 ) , then the minimum setup time should be 2 minutes since there is a sequence of pairs ( 4 , 1 ) , ( 5 , 3 ) , ( 9 , 4 ) , ( 1 , 2 ) , ( 2 , 5 ) .
(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 ( 9 , 4 ) , ( 2 , 5 ) , ( 1 , 2 ) , ( 5 , 3 ) , and ( 4 , 1 ) , then the minimum setup time should be 2 minutes since there is a sequence of pairs ( 4 , 1 ) , ( 5 , 3 ) , ( 9 , 4 ) , ( 1 , 2 ) , ( 2 , 5 ) .
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 2n 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
Source
1 #include <iostream> 2 #include <cmath> 3 #include <cstring> 4 #include <cstdio> 5 #include <cstdlib> 6 #include <algorithm> 7 8 using namespace std; 9 int l[10101],w[10101]; 10 bool dp[10101]; 11 int main() 12 { 13 int T; 14 scanf("%d",&T); 15 for(int i=1;i<=T;i++) 16 { 17 int n; 18 scanf("%d",&n); 19 //-----------------------分两个数组储存长和宽 (也可以一次输入两个数) 20 for(int j=1;j<=2*n;j++) 21 { 22 int x; 23 scanf("%d",&x); 24 if(j%2==1) l[j/2+1]=x; 25 else if(j%2==0) w[j/2]=x; 26 } 27 //-----------------------按长度排序(从小到大)。特别注意:当长度一样时,要按宽度从小到大排 28 for(int j=1;j<n;j++) 29 for(int k=j+1;k<=n;k++) 30 { 31 if(l[j]>l[k]) {swap(l[j],l[k]);swap(w[j],w[k]);} 32 else if(l[j]==l[k]) 33 { 34 if(w[j]>w[k]) swap(w[j],w[k]); 35 } 36 } 37 //-----------------------关键部分:排序后对宽求不下降子序列的个数 ,即为解 38 int ans=0; 39 for(int j=1;j<=n;j++) 40 { 41 if(dp[j]) continue; //dp数组用来记录当前位置有没有被访问过。访问过的话直接跳过 42 int f=w[j]; 43 dp[j]=1;ans++; //一旦遇到一个未访问过的 ,说明又出现了一个新的不下降子序列 44 //cout<<j<<" "; 45 for(int k=j+1;k<=n;k++) 46 { 47 if(dp[k]) continue; //同样,访问过的要跳过,省时间 48 if(w[k]>=f) {dp[k]=1;f=w[k];/*cout<<w[k]<<" ";*/} //此处注意:每次要记得更新 f的值 49 } 50 //cout<<endl; 51 } 52 printf("%d\n",ans); 53 memset(l,0,sizeof(l)); 54 memset(w,0,sizeof(w)); 55 memset(dp,0,sizeof(dp)); 56 } 57 //system("pause"); 58 return 0; 59 }
以上是关于POJ1065 Wooden Sticks的主要内容,如果未能解决你的问题,请参考以下文章