博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
贪心 --- 排序 + 贪心
阅读量:6295 次
发布时间:2019-06-22

本文共 2910 字,大约阅读时间需要 9 分钟。

 

 

Wooden Sticks

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 11023    Accepted Submission(s): 4530

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

 

【题目来源】

【题目大意】

有一堆木棒,这些木棒的长度和重量是给定的。这些木棒将被机器一根接一根的进行处理。
机器处理每一根木棒需要一些时间,这个时间被叫做建立时间。
这个时间是这样定义的:
1.第一根木棒的建立时间是1分钟;
2.在处理一根长度为l、重量为w的木棒之后,如果木棒的l'<=l、w<=w',那么这个机器将不需要建立时间。否则,这个机器将需要1分钟的建立时间。
你需要找到处理完这些木头的最短建立时间。题目中的例子看懂就知道怎么做了。

【题目分析】

首先按照l排序,当l相同的时候,再按照w排序,然后就是按照顺序一遍一遍的标记并统计,直至所有的都被标记,最后的count就是答案。

奇怪的是,我的代码在测试样例的时候是错的,我提交后竟然ac了。

 

#include
#include
#include
using namespace std;struct Node{ int l,w; bool mark;};bool cmp(Node a,Node b){ if(a.l==b.l) { return a.w
=cl&&node[j].w>=cw) { node[j].mark=1; cl=node[j].l; cw=node[j].w; } } j=1; while(node[j].mark) j++; i=j; if(i==n) break; cnt++; node[i].mark=1; cl=node[i].l; cw=node[i].w; } printf("%d\n",cnt); } return 0;}

 

 

 

 

 

转载于:https://www.cnblogs.com/crazyacking/p/3754177.html

你可能感兴趣的文章
话说模式匹配(5) for表达式中的模式匹配
查看>>
《锋利的SQL(第2版)》——1.7 常用函数
查看>>
jquery中hover()的用法。简单粗暴
查看>>
线程管理(六)等待线程的终结
查看>>
spring boot集成mongodb最简单版
查看>>
DELL EqualLogic PS存储数据恢复全过程整理
查看>>
《Node.js入门经典》一2.3 安装模块
查看>>
《Java 开发从入门到精通》—— 2.5 技术解惑
查看>>
Linux 性能诊断 perf使用指南
查看>>
实操分享:看看小白我如何第一次搭建阿里云windows服务器(Tomcat+Mysql)
查看>>
Sphinx 配置文件说明
查看>>
数据结构实践——顺序表应用
查看>>
python2.7 之centos7 安装 pip, Scrapy
查看>>
机智云开源框架初始化顺序
查看>>
Spark修炼之道(进阶篇)——Spark入门到精通:第五节 Spark编程模型(二)
查看>>
一线架构师实践指南:云时代下双活零切换的七大关键点
查看>>
ART世界探险(19) - 优化编译器的编译流程
查看>>
玩转Edas应用部署
查看>>
music-音符与常用记号
查看>>
sql操作命令
查看>>