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. 出来ios顶部导航掉下来问题

    <script type="text/javascript"> setposition(); function setposition(){ var ua = navi ...

  2. opengl典型例程立方体投影与地图绘制

    立方体投影 http://www.cnblogs.com/opengl/p/3790450.html 地图绘制 http://www.cnblogs.com/opengl/p/3790354.html

  3. SpringHttpInvoker解析1-使用示例

    HTTP invoker是一个新的远程调用模型,作为Spring框架的一部分,来执行基于HTTP的远程调用(让防火墙可以接受),并使用Java的序列化机制. 服务端 定义服务接口UserService ...

  4. stack

    stack介绍:先进后出 实现C++STL,栈有两个参数:template<class T,class Container = deque<T>> class stack: 参 ...

  5. Android自动截屏小脚本

    @echo off echo * 截图文件将保存在 E:\pic下,以当前日期+时间命名. echo ================================================= ...

  6. jquery toastr introduction

    1.资源 http://www.jq22.com/jquery-info476 http://www.jq22.com/yanshi476 Nuget Install-Package toastr 官 ...

  7. start bbs轻论坛安装时数据库提示错误

    A PHP Error was encounteredSeverity: NoticeMessage: Only variable references should be returned by r ...

  8. Silverlight

    http://kb.cnblogs.com/zt/silverlight/ http://www.cnblogs.com/gnielee/archive/2010/01/15/silverlight- ...

  9. js的function

    1.funciton的js如果直接写是不会执行的 例如 function TableInit() { //var treeNode = $('#otherTree').omTree('getSelec ...

  10. 【BZOJ】3670: [Noi2014]动物园

    http://www.lydsy.com/JudgeOnline/problem.php?id=3670 题意:太水了= = #include <bits/stdc++.h> using ...