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 ) .

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

Taejon 2001

 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

时间: 2025-01-05 00:37:43

POJ1065 Wooden Sticks的相关文章

poj1065 Wooden Sticks[LIS or 贪心]

地址戳这.N根木棍待处理,每根有个长x宽y,处理第一根花费1代价,之后当处理到的后一根比前一根长或者宽要大时都要重新花费1代价,否则不花费.求最小花费代价.多组数据,N<=5000 本来是奔着贪心来做的.首先按照套路想到排序,长优先宽再次从小到大.由于要不浪费,尽量按照顺序去找,第一次把花费仅为1的最长子序列抽出来,标记之后,再循环找下一个未被标记的最长子序列保证只花费1,这样应该是最优的.但是鉴于$N$的范围和多组数据没敢这样做,虽然后来发现数据水这样也可以过.然后瞎想到把数对$(x,y)$抽

HDU 1051:Wooden Sticks

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11605    Accepted Submission(s): 4792 Problem Description There is a pile of n wooden sticks. The length and weight of each stick a

Wooden Sticks POJ - 1065

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

HDU 1051 Wooden Sticks 贪心||DP

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 22000    Accepted Submission(s): 8851 Problem Description There is a pile of n wooden sticks. The length and weight of each stick ar

HDU 1051 Wooden Sticks (贪心)

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11249    Accepted Submission(s): 4629 Problem Description There is a pile of n wooden sticks. The length and weight of each stick a

ZOJ 1025. Wooden Sticks 贪心 结构体排序

Wooden Sticks Time Limit: 2 Seconds      Memory Limit: 65536 KB 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

HDOJ 1051. Wooden Sticks 贪心 结构体排序

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15564    Accepted Submission(s): 6405 Problem Description There is a pile of n wooden sticks. The length and weight of each stick a

(hdu step 1.3.6)Wooden Sticks(求长度和重量都严格递增的数列的个数)

题目: Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2374 Accepted Submission(s): 921   Problem Description There is a pile of n wooden sticks. The length and weight of each stick are

1051 Wooden Sticks(贪心-3)

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