ACM题解——贪心之木头加工

题目描述

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 (4,9), (5,2), (2,1), (3,5), and (1,4), then the minimum setup time should be 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 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 n 2 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根木头,加工第一根1min,当后面的木头长宽都大等前面一根时,加工时间为0min,否则加工时间为1min;分别给出这n根木头长和宽,求加工所需的最短时间。

题解

开始我的思路是木头存放在一个结构体中,结构体按照长宽都最小的排到最前面,后面只能排长宽都比前一个大的,直到排完之后然后排长宽有交错(长宽有一个大于前面,有一个小于前面),再次进行同样的排序,然后从前往后找,找到交错的位置就时间加1,但是结构体排序无法实现这样的排序方式就是无法将交错的木头放到最后。因此需要手动实现:

首先现将结构体按长(或宽)从小大到大排序,结构体中除了存放这根木头的长和宽,还要存放访问标记,初始化为0(未访问);然后用for循环从前往后找,找到第一个标记为0的点,把标记改为已访问,时间计数器加一,并设置当前长curr_L和当前宽curr_W,从这个点开始,凡是后面木头访问标记为0且长宽都大于当前长curr_L和当前宽curr_W的,就把其访问标记设为已访问,然后当前长curr_L和当前宽curr_W更新为这个木头的长和宽,直到所有木头都遍历;一圈扫完之后就是完成了以第一个最小木头打头的双递增排列。

然后跳出内层循环,再次进入外层循环,继续寻找当前第一个访问标记为0的木头,其实就是那些没有被加入上一个双递增排列的木头,开始第二个双递增排列,以这个木头为头,从其后面开始再次内循环,重复上面的操作即可。直到外层循环找不到标记点为0的木头就结束,输出计数器的值即为答案。

代码

#include<iostream>
#include<algorithm>
using namespace std;
struct wood
{int len;int wid;int add;
};
bool cmp2(wood x,wood y)
{if(x.len==y.len)return x.wid<y.wid;elsereturn x.len<y.len;
}int main()
{int t=0;cin>>t;                       //t个casefor(int i=0;i<t;i++){int n=0;cin>>n;wood *mt=new wood[n];que *Q=new que[n];bool *book=new bool[n];   //可以把访问标记直接放到结构体里面for(int j=0;j<n;j++){cin>>mt[j].len>>mt[j].wid;book[j]=0;}sort(mt,mt+n,cmp2);            int cur_l=0,cur_w=0,coun=0;//当前长、宽和时间计数器for(int k=0;k<n;k++){if(book[k]==0){cur_l=mt[k].len;cur_w=mt[k].wid;book[k]=1;coun++;for(int j=k+1;j<n;j++){if(book[j]==0 && mt[j].len>=cur_l && mt[j].wid>=cur_w){cur_l=mt[j].len;cur_w=mt[j].wid;book[j]=1;}}}}cout<<coun<<endl;}return 0;
}

ACM题解——贪心专题——木头加工相关推荐

  1. ACM题解——动态规划专题——G天上掉馅饼

    ACM题解--动态规划专题--G.天上掉馅饼 题目描述 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼别处都不掉 ...

  2. 武汉ACM集训——贪心-7

    武汉ACM集训--贪心-7 洛谷 P6878 [JOI 2020 Final] JJOOII 2 题解 首先将字符串中各字母的下标按照字母种类分类存储. 接着依次遍历字母 J, O, I 当符合情况时 ...

  3. 【刷题笔记】--lintcode木头加工(java)

    木头加工 题目描述 有一些原木,现在想把这些木头切割成一些长度相同的小段木头,需要得到的小段的数目至少为 k.当然,我们希望得到的小段越长越好,你需要计算能够得到的小段木头的最大长度. 注意事项 木头 ...

  4. ACM题解——训练赛2_D - The Beatles

    ACM题解--训练赛2_D - The Beatles 题目描述 Examples Input 2 3 1 1 Output 1 6 Input 3 2 0 0 Output 1 3 Input 1 ...

  5. PAT甲级1125 Chain the Ropes:[C++题解]贪心、优先队列、合并果子

    文章目录 题目分析 题目来源 题目分析 来源:acwing 板子题:合并果子合并果子优先队列 分析:贪心策略是: 每次取最短的两条绳子a和b.该两条绳子合并为1条绳子,且长度变为a+b2\frac{a ...

  6. PAT甲级1038 Recover the Smallest Number (30 分):[C++题解]贪心、排列成最小的数、字符串

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 贪心: 对于字符串a和b,如果 a+b < b+a (这里+代表字符串中的连接)代表字典序更小.举例 a = 321 , b ...

  7. PAT甲级1037 Magic Coupon:[C++题解]贪心

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 贪心. 两个数列分别从大到小排列.从前往后遍历,如果a数组和b数组前k个数都是正数,就相乘累加到res中: 从后往前遍历,如果a数组 ...

  8. ACM题解系列之一:刘汝佳:《算法竞赛入门经典》(第2版)

    题是书中的题,解法参照了书中的解法,不少解法都做了简化和改进. 做程序,就要努力做到自己的程序是最好的! 第3章例题 POJ1488 UVA272 UVALive5381 TEX Quote[输入输出 ...

  9. [贪心专题]CF549G,CF351E,CF226D,CF1276C,CF1148E,CF798D

    文章目录 T1:CF1276C Beautiful Rectangle title solution code T2:CF226D The table title solution code T3:C ...

最新文章

  1. Intellij IDEA 远程debug、远程tomcat部署项目
  2. 字节取消大小周,部分员工:心疼,每个月少拿 1W 块
  3. python教育学_跟着老男孩教育学Python开发【第三篇】:Python函数
  4. 数据库代码编写_如何将您的职业转变为数据科学-即使您今天不编写代码。
  5. python数据分布_Python中的数据分布
  6. C语言之gcc编译过程(十二)
  7. 23 android多线程
  8. hadoop put命令的格式_【Hadoop篇】--Hadoop常用命令总结
  9. imp命令导入指定表_转载:Oracle导出导入dmp文件(exp.imp命令行)
  10. 面试官问:你的缺点是什么,这么回答漂亮!(真实案例)
  11. 计算机网络之子网掩码,子网掩码
  12. Kubernetes Pod 健康检查机制 LivenessProbe 与 ReadinessProbe
  13. 微信为什么收不到服务器的红包,收不到别人的微信红包是怎么回事?该怎么办?...
  14. window系统换为ubuntu系统
  15. github上传自己的代码
  16. 如何在Django中优雅的使用pyecharts设计可视化BI系统(多图表)
  17. Java 从lambda 表达式引用的本地变量必须是最终变量或实际上的最终变量问题解决
  18. 抖音小程序实践一:申请初始化
  19. 查询每个部门工资最低的两个员工的编号,姓名,工资
  20. Linux中创建快捷方式

热门文章

  1. Codeforces Round #694 (Div. 1) 部分简要题解
  2. 【洛谷题解】P2404 自然数的拆分问题
  3. 萌新小白萌新中软实习day7
  4. 萌新必看——10种客户端存储哪家强,一文读尽!
  5. 备份恢复 --已整理
  6. ecshop数据字典
  7. Boll布林带突破策略
  8. 20190121——不羡神仙 Java抽象工厂模式
  9. itunes cannot read the contents of the iphone
  10. 苹果个人账户转成公司账户