HDU 1051: Wooden Sticks(贪心)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了HDU 1051: Wooden Sticks(贪心),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3101字,纯文字阅读大概需要5分钟。
内容图文
![HDU 1051: Wooden Sticks(贪心)](/upload/InfoBanner/zyjiaocheng/1274/feb5e97b7e654cbb9597c72db8db89f1.jpg)
Wooden Sticks
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11244 Accepted Submission(s): 4627
(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
2 1 3
题意是第一个3是3组数据, 然后一个5是5根棍子, 然后逐一输入其的长度和重量。。题目是问怎么做这些棍子用时最少,在做棍子时,若后一根棍子,长度和重量都大于前
一根,则不用加时间。。
想法。。
拿第一组数据来说。。
首先按长度以小到大排序:(1,4),(2,1),(3, 5), (4, 9),(5,2);
我们发现(2, 1)的重量太小, 而(5, 2)的长度太长。这样排结果为3;
其实这时,我们只要把不满足的先标记起来。。
则(1,4),(3,5),(4,9) ———— (2,1), (5,2)
但我们这么排, 也就是题目给出的顺序,结果就为2;也就是最优。
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #include<queue> #include<sstream> #include<cmath> using namespace std; #define M 100500 struct node { int lenth; int weight; int k; } Q[M]; bool cmp ( node a, node b ) { if(a.lenth==b.lenth) return a.weight<b.weight; else return a.lenth<b.lenth; } int main() { int t; int n; scanf("%d", &t); while( t-- ) { scanf("%d", &n); for(int i=0; i<n; i++) { scanf("%d%d", &Q[i].lenth, &Q[i].weight); Q[i].k= 1; } sort( Q, Q+n, cmp ); int ans = 0; for(int i=0; i<n-1; i++) { if(!Q[i].k) continue; int t = Q[i].weight; for(int j = i+1; j<n; j++) if(Q[j].weight>=t &&Q[j].k==1 ) { ans++; Q[j].k=0; t = Q[j].weight; } } printf("%d\n", n-ans); } return 0; }
原文:http://blog.csdn.net/u013487051/article/details/37831631
内容总结
以上是互联网集市为您收集整理的HDU 1051: Wooden Sticks(贪心)全部内容,希望文章能够帮你解决HDU 1051: Wooden Sticks(贪心)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。