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

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

Asia 2001, Taejon (South Korea)

题意:

有一个木棒处理机,每次处理一个木棒,处理的一个木棒时需要1分钟step时间,处理完一个木棒后下一次只能处理l和w都不小于此木棒的木棒,问最少的step时间。

代码:

 1 /*
 2 按照l由小到大排序,用一个数组存储机器不需要step的w值,如果下一个w大于等于此数组中的某一个用它更新与他差值
 3 最小的那个,如果大于每一个数组值就再开一个数组来记录他。
 4 */
 5 #include<iostream>
 6 #include<string>
 7 #include<cstdio>
 8 #include<cmath>
 9 #include<cstring>
10 #include<algorithm>
11 #include<vector>
12 #include<iomanip>
13 #include<queue>
14 #include<stack>
15 using namespace std;
16 int t,n;
17 int a[5010];
18 struct stick
19 {
20     int l,w;
21 }sti[5010];
22 bool cmp(stick x,stick y)
23 {
24     if(x.l==y.l)
25     return x.w<y.w;
26     else return x.l<y.l;
27 }
28 int main()
29 {
30     scanf("%d",&t);
31     while(t--)
32     {
33         scanf("%d",&n);
34         for(int i=0;i<n;i++)
35         scanf("%d%d",&sti[i].l,&sti[i].w);
36         sort(sti,sti+n,cmp);
37         int k=0;
38         a[k]=sti[0].w;
39         for(int i=1;i<n;i++)
40         {
41             int tem=10000000,temj;
42             for(int j=0;j<=k;j++)
43             {
44                 if((sti[i].w>=a[j])&&(sti[i].w-a[j]<tem))
45                 {
46                     tem=sti[i].w-a[j];
47                     temj=j;
48                 }
49             }
50             if(tem!=10000000)
51             a[temj]=sti[i].w;
52             else
53             a[++k]=sti[i].w;
54         }
55         cout<<k+1<<endl;
56     }
57     return 0;
58 }
时间: 2024-08-06 16:02:45

HDU1051 贪心的相关文章

HDU1051 Wooden Sticks 【贪心】

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11627    Accepted Submission(s): 4807 Problem Description There is a pile of n wooden sticks. The length and weight of each stick a

HDU1051 Wooden Sticks【贪心】

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1051 题目大意: 有N根木棍,每根木棍的长度和质量是已知的,机器要处理这N根木棍,处理时间和木棍的长度和 权重有关.第一根木棍的处理时间为1min,之后处理的木棍如果长度大于等于前一根木棍的长度 并且权重也大于等于前一根木棍的长度,就不需要处理时间:否则就需要1min的处理时间.问: 最小的处理时间为多少. 思路: 贪心思想.先将木棍按长度从小到大排列,如果长度一致,则按权重从小到大排列.然后根据

【uva 1615】Highway(算法效率--贪心 区间选点问题)

题意:给定平面上N个点和一个值D,要求在x轴上选出尽量少的点,使得对于给定的每个店,都有一个选出的点离它的欧几里德距离不超过D. 解法:先把问题转换成模型,把对平面的点满足条件的点在x轴的直线上可得到一个个区间,这样就是选最小的点覆盖所有的区间的问题了.我之前的一篇博文有较详细的解释:关于贪心算法的经典问题(算法效率 or 动态规划).代码实现我先空着.挖坑~

【贪心+Treap】BZOJ1691-[Usaco2007 Dec]挑剔的美食家

[题目大意] 有n头奶牛m种牧草,每种牧草有它的价格和鲜嫩度.每头奶牛要求它的牧草的鲜嫩度要不低于一个值,价格也不低于一个值.每种牧草只会被一头牛选择.问最少要多少钱? [思路] 显然的贪心,把奶牛和牧草都按照鲜嫩度由大到小排序,对于每奶牛把鲜嫩度大于它的都扔进treap,然后找出后继. 不过注意后继的概念是大于它且最小的,然而我们这里是可以等于的,所以应该是找cow[i].fresh-1的后继,注意一下…… 1 #include<iostream> 2 #include<cstdio&

POJ1017 Packets(贪心算法训练)

Time Limit: 1000MS          Memory Limit: 10000K          Total Submissions: 51306          Accepted: 17391 Description A factory produces products packed in square packets of the same height h and of the sizes 1*1, 2*2, 3*3, 4*4, 5*5, 6*6. These pro

ZOJ 3946 Highway Project 贪心+最短路

题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3946 题解: 用dijkstra跑单元最短路径,如果对于顶点v,存在一系列边(ui,v)使得dis[v]最小(dis[v]表示0到v的距离).这些边能且只能选一条,那么我们自然应该选cost最小的那个边了. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #inc

CoderForce 140C-New Year Snowmen(贪心)

题目大意:有n个已知半径的雪球.堆一个雪人需要三个尺寸不同的雪球,问用这些雪球最多能堆多少个雪人? 题目分析:先统计一下每种尺寸的球的个数,从三种最多的种类中各取出一个堆成雪人,这样贪心能保证的到的数目最多. 代码如下: # include<iostream> # include<map> # include<vector> # include<cstdio> # include<queue> # include<algorithm>

计蒜客 跳跃游戏(贪心)

给定一个非负整数数组,假定你的初始位置为数组第一个下标.数组中的每个元素代表你在那个位置能够跳跃的最大长度. 请确认你是否能够跳跃到数组的最后一个下标. 例如: A = [2,3,1,1,4], return ture A = [3,2,1,0,4], return false. 格式: 第一行输入一个正整数n,接下来的一行,输入数组A[n].如果能跳到最后一个下标,输出"true",否则输出"false" 样例1 ????输入:???? ????????5 ???

BZOJ 2525 [Poi2011]Dynamite 二分+树形贪心

题意: n个点,一棵树,有些点是关键点,可以将m个点染色. 求所有关键点到最近的被染色点的距离的最大值最小. 解析: 反正从这道题我是学了一种做题思路? 大爷讲课的时候说的:一般选择某些点的代价相同的话都是贪心,代价不同的话一般都是DP. 想想也挺对的,不过就是没有感悟到过? 反正这题考试的时候我是直接D了贪心的- -! 忘了为啥D了. 显然最大值最小我们需要二分一下这个值. 然后接下来我们从下往上扫整棵树. 节点的状态有几个? 第一种是 子树内没有不被覆盖的关键点,并且子树中有一个节点的贡献可