HDU1051 贪心
Wooden Sticks
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18745 Accepted Submission(s): 7692
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 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.
/*
按照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 贪心的更多相关文章
- hdu1051 Wooden Sticks(贪心+排序,逻辑)
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu 1257/1800 - 贪心,dp
1257题目链接 一个序列划分子序列,每个子序列都是非增序列,问最少分成几个子序列 1800题目链接 一堆数分组,每组内数据严格递减,问最少分几组 -------------------------- ...
- BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]
1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1383 Solved: 582[Submit][St ...
- HDOJ 1051. Wooden Sticks 贪心 结构体排序
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]
1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 786 Solved: 391[Submit][S ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【BZOJ-4245】OR-XOR 按位贪心
4245: [ONTAK2015]OR-XOR Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 486 Solved: 266[Submit][Sta ...
- code vs 1098 均分纸牌(贪心)
1098 均分纸牌 2002年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 有 N 堆纸牌 ...
随机推荐
- 导出word使用模版
在我们做我们的小组项目的时候,刚开始的时候我们用到的是Mvc+EF,用上了我们的ITOO框架.在最开始的计划,我们要用到瑞郎报表.可是呢,由于工期原因以及技术暂时没有实现,我们不得不想一个比较折中的方 ...
- 消息提示插件toastr.js与Messenger组件
Toastr是一款基于jQuery的通知插件,可以灵活的自定义样式和拓展其功能! toastr是一个基于Jquery简单.漂亮的消息提示插件,使用简单.方便,可以根据设置的超时时间自动消失. cdn最 ...
- 异步框架asyn4j的原理
启动时调用init方法 public void init(){ if (!run){ run = true; //工作队列 workQueue = newPriorityBlockingQueue(m ...
- getTickCount()函数 VS GetTickCount()函数
这俩函数看上去长得太像了,笔者曾经马大哈地把两者当成一个函数了(确实长得很像),知道有一天发现返回的值离预期值差很远差仔细查了下. 1. getTickCount() 实际上,该函数为opencv中的 ...
- PHP 配置文件详解(php.ini 详解 )
[PHP] ; PHP还是一个不断发展的工具,其功能还在不断地删减 ; 而php.ini的设置更改可以反映出相当的变化, ; 在使用新的PHP版本前,研究一下php.ini会有好处的 ;;;;;;;; ...
- PHP 传值和传引用、传地址的区别
传值, 是把实参的值赋值给行参 那么对行参的修改,不会影响实参的值 传地址 是传值的一种特殊方式,只是他传递的是地址,不是普通的如int 那么传地址以后,实参和行参都指向同一个对象 ...
- CocoaPods安装和使用教程
Code4App 原创文章.转载请注明出处:http://code4app.com/article/cocoapods-install-usage 目录 CocoaPods是什么? 如何下载和安装Co ...
- Map.Entry用法示例
一般在HashMap中可以通过key值得到value值,以key作为检索项.Map.Entry<K,V>可以作为条目的检索项.HashMap中有entrySet()方法,返回值是Set&l ...
- android6.0 适配的问题——activity销毁的问题
1.最近我去运行我们公司所开发的APP,发现出现很多问题,就是从前一个页面跳到另外一个页面后,前一个页面会被销毁. 正常来说,activity跳转过程是这样: A: 存在intent +setActi ...
- poj2109
刚开始看着道题时,感觉不用高精度好像就没法做,想了半天然后果断去看依然博客(这样确实不好),发现又用到了double(这个可以放“+” “-” 300多位的家伙!!!) #include <io ...