HDU1051 Wooden Sticks 【贪婪】
Wooden Sticks
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).
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.
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
2
1
3
非常典型的贪心题。
题意:能够简化成给定n个物品,每一个物品有两项參数a,b。求这n个物品能组成的序列的最小个数使得每一个序列的每两项參数都是非减的。
题解:先依照递增的顺序给物品排序。然后从前往后推断每一个物品有多少个能与它组成合法序列,最后输出序列个数即是。
这题和南阳上那道矩形嵌套有些像,但矩形嵌套是求最长的序列,不能用贪心。得用DP。
#include <stdio.h>
#include <string.h>
#include <algorithm> #define maxn 5010 struct Node {
int x, y;
} arr[maxn];
bool vis[maxn]; bool cmp(Node a, Node b) {
return a.x < b.x;
} int main() {
int t, n, i, j, k, ans;
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
for(i = 0; i < n; ++i)
scanf("%d%d", &arr[i].x, &arr[i].y);
std::sort(arr, arr + n, cmp);
memset(vis, 0, sizeof(bool) * n);
ans = 0;
for(i = 0; i < n; ++i) {
if(vis[i]) continue;
++ans; k = i; vis[i] = 1;
for(j = i + 1; j < n; ++j) {
if(vis[j]) continue;
if(arr[j].x >= arr[k].x && arr[j].y >= arr[k].y)
vis[k = j] = 1;
}
}
printf("%d\n", ans);
}
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
HDU1051 Wooden Sticks 【贪婪】的更多相关文章
- Hdu1051 Wooden Sticks 2017-03-11 23:30 62人阅读 评论(0) 收藏
Wooden Sticks Problem Description There is a pile of n wooden sticks. The length and weight of each ...
- hdu1051 Wooden Sticks(贪心+排序,逻辑)
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu1051 Wooden Sticks
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1051 大意:求最少升序序列的个数. #include <cstdio> #include &l ...
- HDU1051:Wooden Sticks
Problem Description There is a pile of n wooden sticks. The length and weight of each stick are know ...
- Wooden Sticks(hdu1051)
Wooden Sticks Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submi ...
- HDU-1051/POJ-1065 Wooden sticks 木棍子(动态规划 LIS 线型动归)
嘤嘤嘤,实习半年多的小蒟蒻的第一篇博客(题解) 英文的: There is a pile of n wooden sticks. The length and weight of each stick ...
- HDOJ 1051. Wooden Sticks 贪心 结构体排序
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- POJ 1065 Wooden Sticks
Wooden Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16262 Accepted: 6748 Descri ...
- HDU ACM 1051/ POJ 1065 Wooden Sticks
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
随机推荐
- [转]Qt 智能指针学习
从内存泄露开始? 很简单的入门程序,应该比较熟悉吧 ^_^ #include <QApplication> #include <QLabel> int main(int arg ...
- Linux在简短而经常使用的命令
Linux组成: 内核:的心脏.是执行程序和管理像磁盘和打印机等硬件设备的核心程序. shell:是系统的用户界面,提供了用户和内核进行交互操作的一种接口.它接收用户输入的命令并把它送入内核去执行.是 ...
- 机械革命X5(MECHREVO MR-X5)开包检验
不废话.直接的问题,左右X5没有更具体的信息.为了通过有机会了解后续的选择,具体的数据被释放约: 首先看包装: 1.快递包裹,基于卖方这可以是不同的,包装各不相同 watermark/2/text/a ...
- BZOJ-2115-Xor-WC2011
叙述性说明 分析 我把文库里的粘了过来. 仅仅知道点1到点N的一条路径和图中若干个环.就能通过异或,表示成全部路径.那么.须要多少环才干保证必然能表示成全部路径呢?事实上.并不须要非常多, 由于一些环 ...
- uva 1331 - Minimax Triangulation(dp)
option=com_onlinejudge&Itemid=8&page=show_problem&category=514&problem=4077&mosm ...
- JS call与apply
JS的call与apply call和apply是JS中比较重要的两个方法, 一般在框架和组件设计中用的较多,比如jQuery Code. 那么这两个方法是做什么的呢,下面我们通过代码来了解: 1 f ...
- .NET 4 并行(多核)编程系列之四 Task的休眠
原文:.NET 4 并行(多核)编程系列之四 Task的休眠 .NET 4 并行(多核)编程系列之四 Task的休眠 前言:之前的几篇文章断断续续的介绍了Task的一些功能:创建,取消.本篇介绍Tas ...
- 在SQL Server引用dll的流程
原文:在SQL Server引用dll的流程 在SQL Server中引用dll分为两个步骤 1.创建一个dll文件 2.把dll文件放进SQL Server的程序集中.然后定义一个Function, ...
- 成不了天才,但为何也没成"人材"?(转)
长期以来,"软件业"一直被视为"智力密集"型的"朝阳"产业,大多数从业者都受过高等教育,其平均素质居于社会各行业的前列,这个产业的顶尖人物被 ...
- 《Hadoop》对于高级编程Hadoop实现构建企业级安全解决方案
本章小结 ● 理解企业级应用的安全顾虑 ● 理解Hadoop尚未为企业级应用提供的安全机制 ● 考察用于构建企业级安全解决方式的方法 第10章讨论了Hadoop安全性以及Hadoop ...