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 ...
随机推荐
- decorate pattern 装饰模式
[装饰模式的优缺点]装饰模式的优点:1.比静态继承更灵活:2.避免在层次结构高层的类有太多的特征装饰模式的缺点:1.使用装饰模式会产生比使用继承关系更多的对象.并且这些对象看上去都很想像,从而使得查错 ...
- 采用ACE登录设施(一)HelloWorld
(1)开始使用日志设施 使用日志设施,总是要包括头文件: #include "ace/Log_Msg.h" ACE日志的Hello World #ifdef _DEBUG #pra ...
- dm8148 jpeg编解码器测试
测试过程: 1)于A8将jpeg传送到videoM3解码,然后,videoM3编码.在编译jpeg图像传输到A8,主要是测试jpeg编码的图像需要多少时间: 1000w像素: 编码时间:43ms. ...
- 《炉石传说》建筑设计欣赏(6):卡&在执行数据时,组织能力
上一篇文章我们看到了<炉石传说>核心存储卡的数据,今天,我们不断探索卡&身手. 基本的类 通过之前的分析,卡牌&技能涉及到几个类体系:Entity.Actor.Card.S ...
- WPF和Expression Blend开发实例:一个样式实现的数字输入框
原文:WPF和Expression Blend开发实例:一个样式实现的数字输入框 今天来一个比较奇淫技巧的手法,很少人用,同时也不推荐太过频繁的使用. 先上样式: <Style x:Key=&q ...
- Android - match_parent 和 fill_parent差异
Android - match_parent 和 fill_parent差异 本文地址: http://blog.csdn.net/caroline_wendy match_parent 和 fill ...
- 【Java GUI】Java面板基础:JPanel
有两个面板,常见的面板(JPanel)和滚动面板(JScrollPane) Jpanel 面板是一种常见的容器,JPanel的作用是实现接口层次结构,面放入一些组件.也能够在上面绘画,将放有组件和有画 ...
- PowerDesigner 为sql生成凝视
在使用pd时发现生成的语句中没有行凝视,这样就得手动来加这些凝视,后来在网上找了一些方法.确实得到了解决,分享一下. 在Database-->edit Current DBMS... 设置dbm ...
- 【网络流量最大流量】poj3281Dining
/* EK算法版本号,哦,慢.....见下文dinic版本号 ----------------------------------------- 最大的问题是网络流量问题 -------------- ...
- [原创].NET 业务框架开发实战之八 业务层Mapping的选择策略
原文:[原创].NET 业务框架开发实战之八 业务层Mapping的选择策略 .NET 业务框架开发实战之八 业务层Mapping的选择策略 前言:在上一篇文章中提到了mapping,感觉很像在重新实 ...