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

题意:给n个带有长度和重量属性的木棒。处理第一根木棒花费时间为1,其后如果处理的木棒的长度和重量分别大于上一根已经处理的木棒的长度和重量,就不用花时间,否则花费时间加一。还是仔细读一下题吧。题解:排完序后处理方式就明显了,贪心,每次从当前位置理到尾,并标记已经处理过的木棒,那么当更新当前位置时,如果没有处理,则花费时间加1.(为什么贪心一定正确,其实在没有排序的时候,那么处理到比当前长度或者重量大的时候,可以把这跟木棒插入到适合处理它的位置,所以排序后贪心就一定能得到最小值)
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6
 7 const int maxn=5005;
 8
 9 struct node{
10     int l,w;
11     bool operator<(const node& i)const{
12         if(l==i.l) return w<i.w;
13         return l<i.l;
14     }
15 }a[maxn];
16
17 int n;
18 bool vis[maxn];
19
20 void solve()
21 {   memset(vis,0,sizeof(vis));
22     int ans=0;
23     for(int i=1;i<=n;i++){
24         if(vis[i]) continue;
25         ans++;
26         int teml=a[i].l,temw=a[i].w;
27         for(int j=i+1;j<=n;j++){
28             if(vis[j]) continue;
29             if(a[j].l>=teml&&a[j].w>=temw){
30                 teml=a[j].l;
31                 temw=a[j].w;
32                 vis[j]=true;
33             }
34         }
35         vis[i]=true;
36     }
37     cout<<ans<<endl;
38 }
39
40 int main()
41 {   int cases;
42     cin>>cases;
43     while(cases--){
44         cin>>n;
45         for(int i=1;i<=n;i++) scanf("%d%d",&a[i].l,&a[i].w);
46         sort(a+1,a+n+1);
47         solve();
48     }
49 }
时间: 2024-12-31 21:28:47

Wooden Sticks POJ - 1065的相关文章

挑战程序设计竞赛2.3: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

Wooden Sticks POJ 1065(简单dp)

原题 题目链接 题目分析 题意很明确,就是要维护单调递增的序列,最后看有多少种单调序列即可,设定一个dp数组,cnt表示数组大小,初始化为0,然后把所有木头从小到大排个序,当遍历到木头i时,如果dp数组里有比木头i还小的,就代替它,否则就dp[cnt++]=木头i,最后cnt就是答案. 代码 1 #include <iostream> 2 #include <algorithm> 3 #include <utility> 4 #include <cstdio>

POJ 1065. Wooden Sticks 贪心 结构体排序

Wooden Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19992   Accepted: 8431 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 woodworkin

POJ 1065 Wooden Sticks Greed,DP

排序后贪心或根据第二关键字找最长下降子序列 #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<iostream> #include<sstream> #include<cmath>

POJ 1065 Wooden Sticks

POJ 1065 Wooden Sticks n根木材长l_i重w_i,前一根木材大于后一根的话要浪费一分钟准备机器,求最省方案? 我们把问题抽象出来,那就是:把一个数列划分成最少的最长不升子序列 Dilworth定理:对于一个偏序集,最少链划分等于最长反链长度. 这个问题是二元组的最少链划分,那么我们以a[]为关键字大小进行排序,如果a[]中相同就按照b[]排序,根据 Dilworth定理,然后题目就变成了求b[]序列中最长严格下降子序列长度了. 1 #include <iostream>

poj 1065 Wooden Sticks / hdu 1257 最少拦截系统 DP 贪心

参考链接:http://blog.csdn.net/xiaohuan1991/article/details/6956629 (HDU 1257 解题思路一样就不继续讲解) POJ 1065题意:给你n个木块,分别给出其长度和重量,然后要对这些木块进行加工,如果木块1的长度和重量都不大于木块2, 那么这两个木块可以放在一起加工,从而只用花费1个时间单位.问要如何进行加工从而能够花费最少时间单位. 知识点: 偏序集:若一个集合A为偏序集,那么满足:1.任意一个元素X属于集合,那么这个元素X≤X 2

Wooden Sticks(贪心)

Wooden Sticks. win the wooden spoon:成为末名. 题目地址:http://poj.org/problem?id=1065 There is a pile of n wooden sticks. a woodworking machine:木工机器. setup time:启动时间. my codes: #include<iostream> #include<cstdio> #include<cstring> #include<al

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

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