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. ios 判断用户是否开启权限---并跳转设置

    ios 判断用户是否开启权限---并跳转设置 ios 判断用户是否开启权限---并跳转“系统设置” 1.判断 访问相册 或 相机 权限是否开启 2.检测是否开启定位 后面将持续更新 只有在应用请求过位 ...

  2. Hibernate知识点小结(一)--快速入门

    一.Hibernate的简介    1.Hibernate是一个开放源代码的对象关系映射框架    2.对象关系映射:ORM  Object Relation Mapping        对象与数据 ...

  3. NFS网络文件系统详解

    第1章 NFS基本概述 1.1 什么是nfs NFS是Network File System的缩写及网络文件系统. 主要功能是通过局域网络让不同的主机系统之间可以共享文件或目录. NFS系统和Wind ...

  4. 【解决】MacOS下 Python3.7 使用 pyinstaller 打包后执行报错 Failed to execute script pyi_rth__tkinter

    Fix tcl/tk libs inclusion in tkinter with Python3.7 under MacOS 使用 Pyinstaller 打包时候报错 3027 ERROR: Tc ...

  5. 方别《QQ群霸屏技术》,又见《QQ群建群细则》

    规则,时刻变动;QQ群系列,咱们再来一轮. QQ群霸屏技术,你说建群貌似很菜,大家仿佛都知道,其实只知其一不知其二. QQ群类别 群分类,常规的就以下几种. 普通群. 建群随意,偏个性化,一言不合就拉 ...

  6. 【Nginx】Nginx配置REWRITE隐藏index.php

    只需要在server里面加上 if (!-e $request_filename) { rewrite ^/(.*)$ /index.php/$1 last; break; }

  7. 第五课:PHP echo和print 语句

    PHP  echo 和 print 语句 PHP 是通过 print 和 echo 语句来动态输出 HTML 内容,虽然 print 和 echo 语句两者的功能几乎是完全一样,但是还是有一点差别的. ...

  8. 洛谷U32670 小凯的数字(比赛)

    题目网址 https://www.luogu.org/problemnew/show/U32670 题目背景 NOIP2018 原创模拟题T1 NOIP DAY1 T1 or DAY 2 T1 难度 ...

  9. STL——map和set

    一.map 1.map被定义为一对(pair即key和value)数值. key值在map内最多只有一份. 假如需要对一篇文章中的单词计数,那么就可以建立一个map,string类型的key,int型 ...

  10. java简单界面实现

    import javax.swing.JFrame; import javax.swing.JPanel; public class DemoFrame extends JFrame{ public ...