HDU1051 Wooden Sticks
来源:互联网 发布:mac爱奇艺 编辑:程序博客网 时间:2024/05/17 03:01
题目:
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).
(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).
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
213题目大意是:有一些木棍,知道它们的长度和重量,每当要处理更短或更轻的木棍时,都需要花1分钟准备,问至少要多久。
分析:这是一道贪心题,要考虑长度和重量。先排序(优先按长度由短到长,其次按重量由轻到重),排序后则不用过多考虑长度,比较重量即可。
思路:先将数组排序,以a[ 0 ]为标准,与数组元素依次比较重量w。符合要求的元素被赋值成最长最重的棍子(即第n根)(再次排序则可将a[ i ]调到最后,同时n--,这样就排除出数组,不妨碍下一轮的比较)。
代码如下:
#include<cstdio>#include<iostream>#include<algorithm>using namespace std;const int maxn=5000+20;struct size{int l,w;} a[maxn],s[1];//s[0]记录当前比较的标准 bool cmp(const size a,const size b ){//优先排长度,长度相等排重量 if( a.l < b.l ) return 1; else if( a.l == b.l ){ if( a.w < b.w ) return 1; else return 0;} else return 0;}int main(){int t,n;cin>>t;while(t--){int k=0;//k 为机器setup的次数 cin>>n;for(int i=0;i<n;i++){ cin>>a[i].l>>a[i].w;}sort(a,a+n,cmp);while(n>0){k++;int m=n; //这一轮的棍子个数 s[0]=a[0]; //机器setup for(int i=0;i<m;i++){if(s[0].w<=a[i].w){s[0]=a[i];a[i]=a[m-1];n--;}}//循环完之后 n为下一轮棍子个数 sort(a,a+m,cmp); } printf("%d\n",k);}return 0;}
0 0
- HDU1051(Wooden Sticks)
- HDU1051:Wooden Sticks
- hdu1051(Wooden Sticks 贪心)
- hdu1051 Wooden Sticks (贪心)
- hdu1051 Wooden Sticks
- HDU1051 Wooden Sticks
- HDU1051 Wooden Sticks 贪心
- HDU1051--Wooden Sticks
- hdu1051 Wooden Sticks贪心
- HDU1051 Wooden Sticks 【贪心】
- HDU1051 Wooden Sticks 贪心
- ACM-HDU1051-Wooden Sticks
- HDU1051 Wooden Sticks【贪心】
- hdu1051 Wooden Sticks
- hdu1051 Wooden Sticks
- HDU1051 Wooden Sticks
- Hdu1051 Wooden Sticks
- HDU1051:Wooden Sticks
- 声音音量
- Android 利用奇虎DroidPlugin实现插件化
- 深入.NET平台和c#理解------第三章上机题员工打卡
- 获取当前分类的顶级分类的分类名称
- nls 字符编码文件对应的国家语言
- HDU1051 Wooden Sticks
- gl_FragCoord的xyzw
- 坐标移动
- Ubuntu无法登录mysql的解决方法——亲测有效
- leecode 解题总结:87. Scramble String
- 二维码和URL的相互转换
- PHP pdo单例模式连接数据库
- Android 网络通信架包优缺点使用情况的调研
- PHP解决base64_decode解密出现的乱码问题