HDOJ 1051 Wooden Sticks
来源:互联网 发布:java应届自我介绍 编辑:程序博客网 时间:2024/05/01 09:56
Wooden Sticks
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 15883 Accepted Submission(s): 6524
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).
(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
213
大致题意:
工厂加工一批木棍,加工所用的设备调整一次需要一分钟,只要后面的木棍的长度和重量大于等于前面的木棍,设备就不用调整。设备第一次启动的时候也需要一分钟。木棍的顺序可以调整,依次输入木棍的数量,长度和重量,求加工完这些木棍所需的最短时间。
AC一:
思路:
因为题目中没说输入的木材是按长度从小到大(从大到小)排的,或是按重量从小到大(从大到小)排的,所以要先对木材进行排序,笔者是按木材长度从小到大排的(如果长度相同,则质量小的排前面);然后就可以比较了,先定义两个变量来存放前一个木材的规格,再定义一个数组flag,1表示该木材已经加工,0反之,然后与后一个作比较,情况如下(注意,还得定义一个变量ans来记录已加工的木材的总数):
①若符合不花费时间的标准,就加工(即把对应的vis标为1);
②若不符合标准就跳过,等待下一轮的加工;
因为题目中没说输入的木材是按长度从小到大(从大到小)排的,或是按重量从小到大(从大到小)排的,所以要先对木材进行排序,笔者是按木材长度从小到大排的(如果长度相同,则质量小的排前面);然后就可以比较了,先定义两个变量来存放前一个木材的规格,再定义一个数组flag,1表示该木材已经加工,0反之,然后与后一个作比较,情况如下(注意,还得定义一个变量ans来记录已加工的木材的总数):
①若符合不花费时间的标准,就加工(即把对应的vis标为1);
②若不符合标准就跳过,等待下一轮的加工;
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 5000 + 10;struct P{ int l, w;}p[maxn];int t, n;int ans;bool flag[maxn];int cmp(P a, P b){ return a.l < b.l;}int main(){ scanf("%d", &t); while (t--){ scanf("%d", &n); for (int i = 0; i < n; i++){ scanf("%d%d", &p[i].l, &p[i].w); } sort(p, p + n, cmp); ans = 0; memset(flag, 0, sizeof(flag)); for (int i = 0; i < n; i++){ if (flag[i]) continue; int L = p[i].l, W = p[i].w; ans++; for (int j = i; j < n; j++){ if (p[j].l >= L && p[j].w >= W && !flag[j]){ L = p[j].l; W = p[j].w; flag[j] = 1; } } } printf("%d\n", ans); } return 0;}
AC二:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 5000 + 10;struct P{ int l, w;}p[maxn];int t, n;bool flag[maxn];int cmp(P a, P b){ if (a.l < b.l) return 1; else if (a.l == b.l && a.w < b.w) return 1; else return 0;}int main(){ scanf("%d", &t); while (t--){ scanf("%d", &n); for (int i = 0; i < n; i++){ scanf("%d%d", &p[i].l, &p[i].w); } sort(p, p + n, cmp); int ans = 0; for (int i = 0; i < n; i++){ if (p[i].w){ ans++; int temp = p[i].w; p[i].w = 0; for (int j = i + 1; j < n; j++){ if (p[j].w >= temp){ temp = p[j].w; p[j].w = 0; } } } } printf("%d\n", ans); } return 0;}
0 0
- hdoj 1051 Wooden Sticks
- hdoj 1051 Wooden Sticks
- HDOJ 1051 Wooden Sticks
- HDOJ 1051 Wooden Sticks
- hdoj 1051 Wooden Sticks
- HDOJ 1051 Wooden Sticks(贪心)
- hdoj 1051 Wooden Sticks 【贪心】
- HDOJ 1051 Wooden Sticks(LIS)
- hdoj--1051--Wooden Sticks(LIS)
- hdoj 1051 Wooden Sticks ( LIS_num )
- HDOJ-----1051Wooden Sticks贪心
- HDOJ.1051 Wooden Sticks (贪心)
- HDOJ HDU 1051 Wooden Sticks
- HDOJ Wooden Sticks JAVA 1051
- hdoj 1051 Wooden Sticks 【LIS变形】
- hdoj Wooden Sticks 1051 (DP&LIS)
- hdoj 1051 Wooden Sticks(LIS变形)
- HDOJ 1051 Wooden Sticks
- Swing事件分发线程EDT与SwingUtilities.invokeLater
- EJB总结
- shuffle算法及应用
- 基于大数据的软件缺陷分析和预测
- Backbone.js 1.1.2
- HDOJ 1051 Wooden Sticks
- linux 电源管理驱动编写
- Less 简介
- UITableView
- C++中的explicit关键字
- 51Nod 1191 消灭兔子 (贪心+优先队列)
- 第16周SHH数据结构-【项目1-验证算法(8)基数排序 】
- ZOJ 题目3911 Prime Query(线段树求区间素数个数)
- 关于run-time error:The thread has exited with code :1073741701 (0xc000007b)