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 堆纸牌 ...
随机推荐
- loj 1357(树形dp)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1357 #define _CRT_SECURE_NO_WARNINGS #include ...
- Fragment实现兼容手机和平板
Android手机的设置界面,点击一下Sound,可以跳转到声音设置界面,如下面两张图所示: 然后再来看一下Android Pad的设置界面,主设置页面和声音设置页面都是在一个 ...
- filter应用案例三:解决全站编码问题
1 说明 乱码问题: 获取请求参数中的乱码问题: POST请求:request.setCharacterEncoding("utf-8"): GET请求:new String(re ...
- 用js完成毫秒格式数据的日期格式化任务
后台传过来的数据 creationTime 在后台是Date类型的 毫秒转换成 05-24 月 日格式的 //获得月日得到日期oTime function getMoth(str){ var ...
- JavaScript笔试必备语句
1. document.write( " "); 输出语句 2.JS中的注释为// 3.传统的HTML文档顺序是:document- >html- >(head,bod ...
- 06_Java多线程、线程间通信
1. 线程的概念 1.1多进程与多线程 进程:一个正在执行的程序.每个进程执行都有一个执行顺序,该顺序是一个执行路径,或叫一个控制单元. 一个进程至少有一个线程. 线程:就是进程中的一个独立 ...
- 原生 js 写分页
欢迎留言或者加本人QQ172360937咨询 这段代码是用原生 js 写的一个分页的效果 <!doctype html> <html lang="en"> ...
- 如何在UMG中上显示3D物体
http://www.unrealchina.net/forum.php?mod=viewthread&tid=101548 个人认为可以使用关卡系统,搞个空关卡来做渲染比较好
- 最新版EJS的include函数已支持参数传递
最新版的express中partial函数已经被移除,使用include虽然可以实现同样的效果,但是代码看起来很不爽比如 1 <%-partial("user/home",{ ...
- hdu 1312 Red and Black
Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...