http://acm.hdu.edu.cn/showproblem.php?pid=1051

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
 
题解:贪心 先排序
时间复杂度:$O(N ^ 2 + N * logN)$
代码:

#include <bits/stdc++.h>
using namespace std; const int maxn = 1e5 + 10;
int N; struct Node {
int l;
int w;
int flag;
}node[maxn]; bool cmp(const Node& a, const Node& b) {
return a.l == b.l ? a.w < b.w : a.l < b.l;
} int main() {
int T;
scanf("%d", &T);
while(T --) {
memset(node, 0, sizeof(node));
scanf("%d", &N);
for(int i = 1; i <= N; i ++) {
scanf("%d%d", &node[i].l, &node[i].w);
node[i].flag = 0;
} int cnt = 0;
sort(node + 1, node + 1 + N, cmp);
for (int k = 1; k <= N;)
{
cnt ++;
int L = 0, W = 0;
for (int i = 1; i <= N; i ++) {
if (!node[i].flag)
if (node[i].l >= L && node[i].w >= W) {
node[i].flag = 1;
L = node[i].l;
W = node[i].w;
k ++;
}
}
}
printf("%d\n", cnt);
}
return 0;
}

  

HDU 1005 Wooden Sticks的更多相关文章

  1. HDU 1051 Wooden Sticks (贪心)

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  2. HDU 1051 Wooden Sticks 贪心||DP

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  3. HDU - 1051 Wooden Sticks 贪心 动态规划

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

  4. hdu 1051:Wooden Sticks(水题,贪心)

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  5. HDU 1051 Wooden Sticks

    题意: 有 n 根木棒,长度和质量都已经知道,需要一个机器一根一根地处理这些木棒. 该机器在加工过程中需要一定的准备时间,是用于清洗机器,调整工具和模板的. 机器需要的准备时间如下: 1.第一根需要1 ...

  6. HDU 1051 Wooden Sticks【LIS】

    题意:给出n个木头的重量wi,长度li,如果满足w[i+1]>=w[i]且l[i+1]>=l[i],则不用耗费另外的加工时间,问至少需要多长时间加工完这些木头. 第一次做这一题目也没有做出 ...

  7. HDU 1051 Wooden Sticks 造木棍【贪心】

    题目链接>>> 转载于:https://www.cnblogs.com/Action-/archive/2012/07/03/2574800.html  题目大意: 给n根木棍的长度 ...

  8. HDU 1051 Wooden Sticks 贪心题解

    本题一看就知道是最长不减序列了,一想就以为是使用dp攻克了. 只是那是个错误的思路. 我就动了半天没动出来.然后看了看别人是能够使用dp的,只是那个比較难证明其正确性,而其速度也不快.故此并非非常好的 ...

  9. hdu 1051 wooden sticks (贪心+巧妙转化)

    #include <iostream>#include<stdio.h>#include<cmath>#include<algorithm>using ...

随机推荐

  1. 打造开源GIS方案

    现在GIS用途较多,最近要有所接触,所以决定自己打造一个已经又的方案.均以Java作为开发 二位地图:客户端:geotools,swing,geoserver; web:openlayer,geose ...

  2. js函数只触发一次

    如何让js中的函数只被执行一次?我们有时候会有这种需求,即让一个函数只执行一次,第二次调用不会返回任何有价值的值,也不会报错.下面将通过三个小demo展示使用的方法,当做个人笔记. 1.通过闭包来实现 ...

  3. MySql学习笔记06

    课程回顾 一对一关联 案例1:查询每个员工的名字和主管领导的名字 select e.ename 员工姓名,m.ename 领导姓名 from emp e join emp m on e.mgr=m.e ...

  4. 【poe设备加电配置】

    开启接口的poe功能: [interface_name]: 配置poe端口的最大功率: [interface_name[: 配置poe的端口工作模式: [interface_name[: 配置poe端 ...

  5. js实现前端的搜索历史记录

    最近在对接前台页面(WEB端)时,产品要求需记录下客户的搜索记录,我们是前后台完全分离的项目,根本不能保存的session域中,没办法,虽然作为后台开发,遇到需求就自己研究了一通,先看一下最终效果图, ...

  6. 介绍几个PHP 自带的加密解密函数

    PHP 自带的加密解密函数 目前经常使用的加密函数有:md5(), sha1(), crypt(), base64_encode(), urlencode() . 其中 md5(), sha1(), ...

  7. CPS---(Cyber-Physical Sytem,信息物理融合系统)

    1.CPS定义 CPS是连接计算机虚拟世界与物理现实世界的系统.---We refer to systems that bridge the cyber-world of computing and ...

  8. python网络编程的坑(持续更新)

    初学python,踩了许多坑...每天都学一点吧..(大佬绕过) 1.session的用法: session是python requests库中的一个重要功能.session可以存储用户的数据并且存储 ...

  9. 2-Linux C语言指针与内存-学习笔记

    Linux C语言指针与内存 前面我们对于: c语言的基本用法 makeFile文件的使用 main函数的详解 标准输入输出流以及错误流管道 工具与原理 指针与内存都是c语言中的要点与难点 指针 数组 ...

  10. sudo mount -o loop pm.img /mnt/floppy

    sudo mount -o loop pm.img /mnt/floppy 最近在学<一个操作系统的实现>,由于这本书比较老了,所以有一些对于软盘的操作指令现在用会出现一些错误,当我进行虚 ...