《挑战程序设计竞赛》2.3 动态规划-进阶 POJ1065 1631 3666 2392 2184(5)
POJ1065:
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 ( 9 , 4 ) , ( 2 , 5 ) , ( 1 , 2 ) , ( 5 , 3 ) , and ( 4 , 1 ) , then the minimum setup time should be 2 minutes since there is a sequence of pairs ( 4 , 1 ) , ( 5 , 3 ) , ( 9 , 4 ) , ( 1 , 2 ) , ( 2 , 5 ) .
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 2n 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
代码
#include<iostream>
#include<algorithm>
using namespace std;
struct wood
{
int l;
int w;
bool visited;
};
bool cmp(wood wood1, wood wood2)
{
if(wood1.l < wood2.l)
return true;
if(wood1.l == wood2.l && wood1.w < wood2.w)
return true;
return false;
}
int main()
{
int caseNum;
cin >> caseNum;
wood woods[5005];
for(int mm = 0; mm<caseNum; mm++)
{
int woodNum;
cin >> woodNum;
for(int i=0; i<woodNum; i++)
{
cin >> (woods[i].l) >> woods[i].w;
woods[i].visited = false;
}
sort(woods, woods + woodNum, cmp );
int count = 0;
for(int i=0; i<woodNum; i++)
{
if(woods[i].visited == false)
{
count++;
woods[i].visited = true;
int weight = woods[i].w;
for(int j=i+1; j<woodNum; j++)
{
if(woods[j].visited == true)
continue;
if(woods[j].l >= woods[i].l && woods[j].w >= weight)
{
woods[j].visited = true;
weight = woods[j].w;
}
}
}
}
cout << count << endl;
}
return 0;
}
《挑战程序设计竞赛》2.3 动态规划-进阶 POJ1065 1631 3666 2392 2184(5)的更多相关文章
- 《挑战程序设计竞赛》2.3 动态规划-优化递推 POJ1742 3046 3181
POJ1742 http://poj.org/problem?id=1742 题意 有n种面额的硬币,面额个数分别为Ai.Ci,求最多能搭配出几种不超过m的金额? 思路 据说这是传说中的男人8题呢,对 ...
- Aizu 2249Road Construction 单源最短路变形《挑战程序设计竞赛》模板题
King Mercer is the king of ACM kingdom. There are one capital and some cities in his kingdom. Amazin ...
- 挑战程序设计竞赛》P345 观看计划
<挑战程序设计竞赛>P345 观看计划 题意:一周一共有M个单位的时间.一共有N部动画在每周si时 ...
- POJ 2386 Lake Counting 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2386 <挑战程序设计竞赛>习题 题目描述Description Due to recent rains, water has ...
- poj 3253 Fence Repair 贪心 最小堆 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=3253 题解 本题是<挑战程序设计>一书的例题 根据树中描述 所有切割的代价 可以形成一颗二叉树 而最后的代价总和是与子节点和深 ...
- 《挑战程序设计竞赛》2.3 动态规划-基础 POJ3176 2229 2385 3616 3280
POJ3176 Cow Bowling 题意 输入一个n层的三角形,第i层有i个数,求从第1层到第n层的所有路线中,权值之和最大的路线. 规定:第i层的某个数只能连线走到第i+1层中与它位置相邻的两个 ...
- 《挑战程序设计竞赛》 4.1.1 矩阵 P286
想写几篇挑战的感悟,也有助于自己理解这本书.但这上面大多贴的是书上的代码,主要是为了用的时候后直接复制就好了,这样就很方便了,就相当于黑盒模板了. 1.线性方程组 /** \brief 高斯消元法 * ...
- poj1182食物链_并查集_挑战程序设计竞赛例题
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65534 Accepted: 19321 Description ...
- 迷宫问题_BFS_挑战程序设计竞赛p34
给定一个N*M的迷宫,求从起点到终点的最小步数. N,M<100: 输入: 10 10#S######.#......#..#.#.##.##.#.#........##.##.####.... ...
随机推荐
- 【搞机】Apple Pencil 开箱
前言 上次入手了新的iPad Pro .好开心呢. 然后发现官方的笔不错呢~ 后来,苹果官方的12期免息分期又回来啦~ 买买买!!! 上图 体验 官方的笔真的不愧叫Pencil ,完美模拟铅笔的手感. ...
- jsp自己主动编译机制
总的来说,Jasper的自己主动检測实现的机制比較简单,依靠某后台线程不断检測JSP文件与编译后的class文件的最后改动时间是否同样,若同样则觉得没有改动.但倘若不同则须要又一次编译.实际上因为在T ...
- 现在的C语言编辑器里的int范围为什么是-2147483648~2147483647 2014-08-05 10:21 100人阅读 评论(0) 收藏
下面是引用百度文库的一段话: "这得从二进制的原码说起: 如果以最高位为符号位,二进制原码最大为0111111111111111=215-1=32767 最小为111111111111111 ...
- c++ 返回对象的引用要小心
除非能保证返回对象的生命周期足够长. 一定不要返回临时对象的引用.
- IIS8.5设置 MVC HTTP 错误 404.0 - Not Found
0. 确认 设置IIS的“ISAPI和CGI限制”中的“ASP.NET v4.0.0.30319”为允许 1. 解决方案 <system.webServer> <modules ...
- SignalR IOS Android
http://www.dotblogs.com.tw/toysboy21/archive/2014/03/24/144505.aspx https://www.youtube.com/watch?v= ...
- mapper.xml文件
1. 概述 mybatis的真正强大在于它的映射语句.由于它的异常强大,映射器的XML文件就显得相对简单,如果拿它跟具有相同功能的JDBC代码进行对比,省掉将近95%的代码.mybatis是针对SQL ...
- 进击的Android注入术《一》
写在前面 这个系列本来是在公司的一个分享.内容比較多,所以就把这个PPT又一次组织整理成博客,希望对大家学习有所帮助.我会先以一个"短信拦截"作为样例,抛出问题,并提出了一种基于& ...
- JS高程3:Ajax与Comet-进度事件、跨源资源共享
有以下 6 个进度事件 loadstart:在接收到响应数据的第一个字节时触发. progress:在接收响应期间持续不断地触发. error:在请求发生错误时触发. abort:在因 ...
- [Egret]长按截屏分享、分享截屏图片、本地存储
egret 分享有API可以把一个显示对象树渲染成一个位图纹理,我把它赋值给 HTML 的 Image 元素,就实现了图片的显示,在微信中,通过长按图片可以分享出去.当然在其他浏览器可以保存在本地. ...