Wooden Sticks

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19672    Accepted Submission(s): 7991

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分钟加工,若接下来放入的木材的长度和重量都大于等于前一个放入的木材,则这个木材加工花费的时间为零,否则得花费一分钟时间来加工;以此类推,直到木材加工完为止。
思想:结构体二级排序,对其中一个属性贪心。
 
 
代码:
 #include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
typedef struct wood {
int l;
int w;
}Wood;
int compare(const void * p1, const void * p2) {
Wood * a1 = (Wood *)p1;
Wood * a2 = (Wood *)p2;
if (a1->l != a2->l)
return a1->l - a2->l;
else
return a1->w - a2->w;
}
int main(void)
{
int T;
scanf("%d", &T);
while (T--) {
int n;
scanf("%d", &n);
Wood sticks[];
bool vis[] = { };
for (int i = ; i < n; i++) {
scanf("%d %d", &sticks[i].l, &sticks[i].w);
}
qsort(sticks, n, sizeof(sticks[]), compare);
int time = ;
for (int i = ; i < n; i++) {
if (vis[i])
continue;
int curw = sticks[i].w;
for (int j = i + ; j < n; j++) {
if (!vis[j] && sticks[j].w >= curw) {
curw = sticks[j].w;
vis[j] = ;
}
}
time++;
}
printf("%d\n", time);
}
    return 0;
}

HDOJ-1051 Wooden sticks(贪心)的更多相关文章

  1. HDOJ.1051 Wooden Sticks (贪心)

    Wooden Sticks 点我挑战题目 题意分析 给出T组数据,每组数据有n对数,分别代表每个木棍的长度l和重量w.第一个木棍加工需要1min的准备准备时间,对于刚刚经加工过的木棍,如果接下来的木棍 ...

  2. HDOJ 1051. Wooden Sticks 贪心 结构体排序

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

  3. HDU 1051 Wooden Sticks (贪心)

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

  4. HDU 1051 Wooden Sticks 贪心||DP

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

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

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

  6. HDOJ 1051. Wooden Sticks

    题目 There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The ...

  7. HDU 1051 Wooden Sticks 贪心题解

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

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

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

  9. 1270: Wooden Sticks [贪心]

    点击打开链接 1270: Wooden Sticks [贪心] 时间限制: 1 Sec 内存限制: 128 MB 提交: 31 解决: 11 统计 题目描述 Lialosiu要制作木棍,给n根作为原料 ...

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

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

随机推荐

  1. 强烈推荐:240多个jQuery插件【转】

    强烈推荐:240多个jQuery插件 概述 jQuery 是继 prototype 之后又一个优秀的 Javascript 框架.其宗旨是—写更少的代码,做更多的事情.它是轻量级的 js 库(压缩后只 ...

  2. T_SQL查询语句(一): 单表查询

    ############################################ 查询语句--SELECT ########################################## ...

  3. TCP/IP详解学习笔记- 概述

    TCP/IP详解学习笔记(1)-- 概述1.TCP/IP的分层结构      网络协议通常分不同层次进行开发,每一层分别负责不同的同信功能.TCP/IP通常被认为是一个四层协议系统.      如图所 ...

  4. 史上最全条件编译解析 #ifdef #ifndef #undef #else #endif

    C语言和C++语言程序中广泛存在着#ifdef或#ifndef等条件编译语句,本篇就系统介绍下他们的用法. 这几个宏是为了进行条件编译.一般情况下,源程序中所有的行都参加编译.但是有时希望对其中一部分 ...

  5. reading words in your computer and changing to female voice, linux festival text2wave saving wav files

    on a brand new linux PC, e.g. ubuntu 14.04 amd64 To hear voice sudo apt-get install festival -y then ...

  6. Erlang Resources 资讯小站

    Erlang Resources 资讯小站  好久没有写博客,是懒了吗?不是;前面两个月在紧张地推进一个项目,中间积累了一些RabbitMQ和Erlang的东西;本打算在项目结束之后赶紧总结一下,结果 ...

  7. asp.net MVC 路由机制 Route

    1:ASP.NET的路由机制主要有两种用途: -->1:匹配请求的Url,将这些请求映射到控制器 -->2:选择一个匹配的路由,构造出一个Url 2:ASP.NET路由机制与URL重写的区 ...

  8. aix 禁止root远程登录

    Aix禁止root远程登录 aix用户扩展信息大都在/etc/security/user这个文本文件里.你可以修改: login=false 用户不能登录系统 rlogin=false 用户不能从远程 ...

  9. 不想再做"鸟蛋"

    至今为止学习C++两个学期了,有过迷茫,有过自信,有过崩溃,有过希望,有过伤心,有过高兴.这一路走来,C++虐我千百遍,我待C++如"初恋".                    ...

  10. ubuntu 安装jdk7总结

    ubuntu 安装jdk7,现在来总结一下: 第一步:下载jdk-7u25-linux-x64.tar.gz 直接在ORACLE的官网中下载就可以: http://download.oracle.co ...