POJ1065

该博客详细介绍了POJ1065问题,这是一个关于木棍加工的贪心算法问题。博主首先解析了题目要求,即寻找最小的机器设置时间来处理一组木棍,木棍按长度和重量排序,长且重的木棍在前可以减少设置时间。接着,博主给出了样例输入和输出,并解释了题意,指出最佳序列是下降子序列最少的序列。博主通过排序和遍历木棍的方法找到了解题思路,并提供了两种不同的解决方案:暴力求解和动态规划。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

POJ1065–贪心问题

一、题目链接

题目来源–POJ1065

二、题目内容

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根木棍等待加工,每根木棍由长度和宽度构成,加工器在放入第一根木棍的时候有一分钟的准备时间,然后在后面塞入木棍的长和宽都比上一根木棍小的木棍将不需要准备时间,否则就需要重新准备一分钟。请你找到处理给定的n根木棍的最短设置时间。例如,如果您有五根长度和重量对分别为(9,4),(2,5),(1、2),(5,3)和(4,1)的摇杆,则最小设置时间应该是2分钟,因为有对(4,1),(5,3),(9,4),(1,2),(2,5)对的序列。

三、题意拆解

  • 题目需要我们找到最佳方案,使得准备时间最短然后输出最短时间。
  • 首先,我们会得到一批有两个属性的木棍,然后我们需要自己把它排序好 (注意:题目给的序列是没有排序好的木棍序列) ,然后我们需要根据这个最佳排序序列输出最少准备时间。
  • 那么什么是最佳序列? 当后面的木棍都比序列前一个木棍小(长、宽都小即为小)的子序列最少 就是最佳序列,这种子序列我称它为下降子序列。
  • 比如:4 9 5 2 2 1 3 5 1 4 这个序列
    最佳排序应该是4 9 3 5 1 4| 5 2 2 1
    因为这样子他的下降子序列只有两个,是最佳方案。

四、解题思路

因为题目要求我们找到最快速度的方法,那么我们直接把获取到的木棍根据它单个属性排序,那么第一个木棍就是最“大”的那个木棍,它必须成为下降子序列的首元素,然后我们遍历木棍,将它的序列排出来,得到第一个序列,而后继续重新遍历这些木棍,把剩余木棍里最大的木棍拿出来成为新的下降子序列。
例如:

4-9 5-2 2-1 3-5 1-4

我们先将其排序,我这里按照长度来排序,排序后结果是

5-2 4-9 3-5 2-1 1-4

然后我们找出第一个下降子序列

5-2 2-1

然后剩下的序列就是

4-9 3-5 1-4

这也同时是第二个下降子序列,所以结果是2。

五、参考代码

做法一:暴力

//
// Created by Verber.
//
#include<cstdio>
#include<algorithm>
#include
### POJ 平台上的常见问题及解决方案 #### 贪心算法的应用场景 贪心算法是一种通过局部最优选择来达到全局最优解的方法。然而,这种方法并不总是能够提供正确的解答,尤其是在某些复杂情况下[^1]。为了有效利用贪心算法解决问题,必须深入理解问题的本质以及其适用条件。 #### POJ 上的经典贪心问题及其解决思路 以下是几个经典的 POJ 问题及其对应的解决方法: ##### 1. **POJ 1700 - Crossing River** 该问题是关于一组人过河的时间优化问题。核心在于如何合理安排人员渡河以最小化总时间。 - 解决方案:按照每次运送最快的人返回的原则设计贪心策略。具体实现可以通过模拟每一步的选择过程完成。 ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n; cin >> n; vector<int> times(n); for (auto &t : times) cin >> t; sort(times.begin(), times.end()); int time = 0; while (!times.empty()) { if (times.size() >= 2) { time += times[1]; // Send the two fastest people. if (times.size() > 2) { time += times.front(); // Return one of them back with the boat. } } else { time += times.back(); } times.erase(times.begin()); // Remove processed elements. } cout << time; } ``` ##### 2. **POJ 1017 - Packets** 此问题涉及将不同大小的数据包放入固定容量的盒子中,目标是最小化使用的盒子数量。 - 解决方案:采用优先队列或者排序的方式处理数据包尺寸,并尝试尽可能多地填充每个盒子。 ```cpp #include <bits/stdc++.h> using namespace std; int main(){ int k, m; cin>>k>>m; priority_queue<int,vector<int>,greater<int>> pq; // Min heap to store packets' sizes. for(int i=0;i<m;i++){ int size; cin>>size; pq.push(size); } int boxesUsed = 0; while(!pq.empty()){ int currentBoxCapacity = k; bool filled=false; while(!pq.empty() && !filled){ if(pq.top()<=currentBoxCapacity){ currentBoxCapacity -= pq.top(); pq.pop(); } else{ break; } if(currentBoxCapacity==0 || pq.empty()) filled=true; } ++boxesUsed; } cout<<boxesUsed; } ``` ##### 3. **POJ 1065 - Wooden Sticks** 这是一个典型的区间覆盖问题,目的是找到最少的数量使得所有木棍被完全覆盖。 - 解决方案:先按长度升序排列所有的木棍,再依次遍历并记录当前区间的最大右端点位置。 ```cpp #include<stdio.h> struct Stick { double l,w;}; bool cmp(const Stick& a,const Stick& b){return a.l<b.l;} Stick sticks[5000]; int main(){ int N,i,j,k,cnt; scanf("%d",&N); for(i=0;i<N;i++)scanf("%lf%lf",&sticks[i].l,&sticks[i].w); sort(sticks,sticks+N,cmp); cnt=k=1; for(i=1;i<N;i++) if(sticks[k-1].w<sticks[i].w){cnt++;k=i;} printf("%d\n",cnt); } ``` #### 关键注意事项 在使用贪心算法时需要注意以下几点: - 验证所选策略是否满足无后效性和可证明的正确性。 - 对于无法直接验证的情况,考虑动态规划或其他更通用的技术作为备选方案。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值