Wooden Sticks

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

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
 
Source
 
题意:
 有一个木棒处理机,每次处理一个木棒,处理的一个木棒时需要1分钟step时间,处理完一个木棒后下一次只能处理l和w都不小于此木棒的木棒,问最少的step时间。
代码:
 /*
按照l由小到大排序,用一个数组存储机器不需要step的w值,如果下一个w大于等于此数组中的某一个用它更新与他差值
最小的那个,如果大于每一个数组值就再开一个数组来记录他。
*/
#include<iostream>
#include<string>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<queue>
#include<stack>
using namespace std;
int t,n;
int a[];
struct stick
{
int l,w;
}sti[];
bool cmp(stick x,stick y)
{
if(x.l==y.l)
return x.w<y.w;
else return x.l<y.l;
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d%d",&sti[i].l,&sti[i].w);
sort(sti,sti+n,cmp);
int k=;
a[k]=sti[].w;
for(int i=;i<n;i++)
{
int tem=,temj;
for(int j=;j<=k;j++)
{
if((sti[i].w>=a[j])&&(sti[i].w-a[j]<tem))
{
tem=sti[i].w-a[j];
temj=j;
}
}
if(tem!=)
a[temj]=sti[i].w;
else
a[++k]=sti[i].w;
}
cout<<k+<<endl;
}
return ;
}

HDU1051 贪心的更多相关文章

  1. hdu1051 Wooden Sticks(贪心+排序,逻辑)

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

  2. hdu 1257/1800 - 贪心,dp

    1257题目链接 一个序列划分子序列,每个子序列都是非增序列,问最少分成几个子序列 1800题目链接 一堆数分组,每组内数据严格递减,问最少分几组 -------------------------- ...

  3. BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1383  Solved: 582[Submit][St ...

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

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

  5. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]

    1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 786  Solved: 391[Submit][S ...

  7. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  8. 【BZOJ-4245】OR-XOR 按位贪心

    4245: [ONTAK2015]OR-XOR Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 486  Solved: 266[Submit][Sta ...

  9. code vs 1098 均分纸牌(贪心)

    1098 均分纸牌 2002年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解   题目描述 Description 有 N 堆纸牌 ...

随机推荐

  1. HDU 3586 Information Disturbing 树形DP+二分

    Information Disturbing Problem Description   In the battlefield , an effective way to defeat enemies ...

  2. Sp EF输出 临时表

    -- ============================================= -- Author: <Author,,Name> -- Create date: < ...

  3. 枚举PEB获取进程模块列表

    枚举进程模块的方法有很多种,常见的有枚举PEB和内存搜索法,今天,先来看看实现起来最简单的枚举PEB实现获取进程模块列表. 首先,惯例是各种繁琐的结构体定义.需要包含 ntifs.h 和 WinDef ...

  4. Linux内核system_call中断处理过程

    “平安的祝福 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ” men ...

  5. XSS 跨站脚本攻击之构造剖析(一)

    1.XSS-Filter:跨站脚本过滤器,用于分析用户提交的输入,并消除潜在的跨站脚本攻击 (1)XSS Filter实际上是一段精心编写的过滤函数作用是过滤XSS跨站脚本代码: (2)绕过XSS F ...

  6. Codeforces Round #354 (Div. 2)-C

    C. Vasya and String 题目链接:http://codeforces.com/contest/676/problem/C High school student Vasya got a ...

  7. Swift3.0语言教程获取字符串编码与哈希地址

    Swift3.0语言教程获取字符串编码与哈希地址 Swift3.0语言教程获取字符串编码与哈希地址,以下将讲解字符串中其它内容的获取方法. 1.获取字符串编码 在NSString中可以使用2个属性获取 ...

  8. js不间断平滑地自动向上滚动

    <html> <head> <title>scroll up auto smooth</title> <style> *{ margin: ...

  9. js小例子(多字溢出,省略号表示)

    实现了div中字数较多,显示不下的时候,用省略号来表示,并且可以展开和收起: <html> <head> <meta http-equiv="Content-T ...

  10. JetS3t使用说明

    http://blog.csdn.net/hitmediaman/article/details/6636402