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)的更多相关文章

  1. 《挑战程序设计竞赛》2.3 动态规划-优化递推 POJ1742 3046 3181

    POJ1742 http://poj.org/problem?id=1742 题意 有n种面额的硬币,面额个数分别为Ai.Ci,求最多能搭配出几种不超过m的金额? 思路 据说这是传说中的男人8题呢,对 ...

  2. Aizu 2249Road Construction 单源最短路变形《挑战程序设计竞赛》模板题

    King Mercer is the king of ACM kingdom. There are one capital and some cities in his kingdom. Amazin ...

  3. 挑战程序设计竞赛》P345 观看计划

                                                 <挑战程序设计竞赛>P345 观看计划 题意:一周一共有M个单位的时间.一共有N部动画在每周si时 ...

  4. POJ 2386 Lake Counting 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=2386 <挑战程序设计竞赛>习题 题目描述Description Due to recent rains, water has ...

  5. poj 3253 Fence Repair 贪心 最小堆 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=3253 题解 本题是<挑战程序设计>一书的例题 根据树中描述 所有切割的代价 可以形成一颗二叉树 而最后的代价总和是与子节点和深 ...

  6. 《挑战程序设计竞赛》2.3 动态规划-基础 POJ3176 2229 2385 3616 3280

    POJ3176 Cow Bowling 题意 输入一个n层的三角形,第i层有i个数,求从第1层到第n层的所有路线中,权值之和最大的路线. 规定:第i层的某个数只能连线走到第i+1层中与它位置相邻的两个 ...

  7. 《挑战程序设计竞赛》 4.1.1 矩阵 P286

    想写几篇挑战的感悟,也有助于自己理解这本书.但这上面大多贴的是书上的代码,主要是为了用的时候后直接复制就好了,这样就很方便了,就相当于黑盒模板了. 1.线性方程组 /** \brief 高斯消元法 * ...

  8. poj1182食物链_并查集_挑战程序设计竞赛例题

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 65534   Accepted: 19321 Description ...

  9. 迷宫问题_BFS_挑战程序设计竞赛p34

    给定一个N*M的迷宫,求从起点到终点的最小步数. N,M<100: 输入: 10 10#S######.#......#..#.#.##.##.#.#........##.##.####.... ...

随机推荐

  1. Spark缓存机制

    虽然默认情况下 RDD 的内容是临时的,但 Spark 提供了在 RDD 中持久化数据的机制.第一次调用动作并计算出 RDD 内容后,RDD 的内容可以存储在集群的内存或磁盘上.这样下一次需要调用依赖 ...

  2. 矩阵乘法C语言实现

    /* 矩阵乘法C语言实现 Slyar 2009.3.20 */   #include <stdio.h> #include <stdlib.h>   /* 给 int 类型定义 ...

  3. 3.selenium模块

    本节内容: 介绍 安装 基本使用 选择器 等待元素被加载 元素交互操作 其他 项目练习 一.介绍 selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行Ja ...

  4. OpenGL数据类型及相应C数据类型

  5. WebApi接口安全认证——HTTP之摘要认证

    摘要访问认证是一种协议规定的Web服务器用来同网页浏览器进行认证信息协商的方法.它在密码发出前,先对其应用哈希函数,这相对于HTTP基本认证发送明文而言,更安全.从技术上讲,摘要认证是使用随机数来阻止 ...

  6. sublimtext3 自定义编译环境

    sublime text是一个非常神奇到编辑器,对于我这种小白来说,感觉比vim好用,但是如果用sublime自带到编译环境到话,是没法向程序汇总输入数据的,所以要自己新建编译命令 { "c ...

  7. 数据库设计(四)数据库的规范化(Normalization)

    数据库的规范化 Database Normalization is a technique of organizing the data in the database. Normalization ...

  8. TPM--Trusted Platform Module

    trouSerS是IBM的一帮牛人搞的TSS软件栈,提供了与TPM交互的API,从而可以让我们方便地编写应用程序. 地址:https://sourceforge.net/projects/trouse ...

  9. ubox及日志管理

    ubox是openwrt的帮助工具箱,位于代码package/system/ubox下, CMakeLists.txt kmodloader.c log/ lsbloader.c validate/ ...

  10. c# 获取某日期所在周的第一天和最后一天(转)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WyfC ...