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 先附上AC代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
pair<int ,int > a[5000];

int cmp(pair<int ,int >a,pair<int ,int > b ) {
if(a.first==b.first) return a.second<b.second;
else return a.first<b.first;
}
int main(){
int T,n;
cin>>T;
int dp[5005];
while(T--){
memset(dp,0,sizeof(dp));
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d%d",&a[i].first,&a[i].second);
sort(a,a+n,cmp); //对第一属性进行排序,然后第二属性求最长递减数列,长度即为所求(与之前做过的最少拦截系统类似)
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
if(a[i].second<a[j].second) dp[i]=max(dp[i],dp[j]+1);
}
if(dp[i]==0) dp[i]=1;
}
int maxn=0;
for(int i=0;i<n;i++)
if(dp[i]>maxn) maxn=dp[i];
printf("%d\n",maxn);
}
return 0;
}

这道题想了很久都没有思路,最后发现与最少拦截系统类似,不过就是相当于一个改编而已,而且是属于那种换汤不换药的改编。

在做ACM题时,我觉得应该多在头脑中积累一些典型的例题,毕竟即便是出题人也不可能凭空就出来一道题(这种可能非常非常小),出题人肯定也是根据现有的题进行加工改变,而如果我们积累了一些典型的例题,未必不能很快的解出答案,实现AC.

poj1065Wooden Sticks(dp——最长递减数列)的更多相关文章

  1. POJ - 1065 Wooden Sticks(贪心+dp+最长递减子序列+Dilworth定理)

    题意:给定n个木棍的l和w,第一个木棍需要1min安装时间,若木棍(l’,w’)满足l' >= l, w' >= w,则不需要花费额外的安装时间,否则需要花费1min安装时间,求安装n个木 ...

  2. POJ1065Wooden Sticks[DP LIS]

    Wooden Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21902   Accepted: 9353 De ...

  3. HOJ Recoup Traveling Expenses(最长递减子序列变形)

    A person wants to travel around some places. The welfare in his company can cover some of the airfar ...

  4. 代码的坏味道(4)——过长参数列(Long Parameter List)

    坏味道--过长参数列(Long Parameter List) 特征 一个函数有超过3.4个入参. 问题原因 过长参数列可能是将多个算法并到一个函数中时发生的.函数中的入参可以用来控制最终选用哪个算法 ...

  5. hdu 1025 dp 最长上升子序列

    //Accepted 4372 KB 140 ms //dp 最长上升子序列 nlogn #include <cstdio> #include <cstring> #inclu ...

  6. poj1159 dp最长公共子串

    //Accepted 204 KB 891 ms //dp最长公共子串 //dp[i][j]=max(dp[i-1][j],dp[i][j-1]) //dp[i][j]=max(dp[i][j],dp ...

  7. 最长递减子序列(nlogn)(个人模版)

    最长递减子序列(nlogn): int find(int n,int key) { ; int right=n; while(left<=right) { ; if(res[mid]>ke ...

  8. [Swift]LeetCode665. 非递减数列 | Non-decreasing Array

    Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...

  9. P1091 合唱队形 DP 最长升序列维护

    题目描述 NN位同学站成一排,音乐老师要请其中的(N-KN−K)位同学出列,使得剩下的KK位同学排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左到右依次编号为1,2,…,K1,2,…,K,他 ...

随机推荐

  1. 《剑指offer》面试题16 反转链表 Java版

    (输入链表的头节点,反转链表) 书中方法:对于一个链表,我们只能从头往后遍历,如果要反转,我们需要更改当前节点的next域指向前一个节点,此时链表断开,为了能继续修改下一个节点的next域,我们还要维 ...

  2. python 重点理论知识点

    Python多线程 GIL blablabla concurrent blablabla 简单地说就是作为可能是仅有的支持多线程的解释型语言(perl的多线程是残疾,PHP没有多线程),Python的 ...

  3. 问题 H: 小k的简单问题

    问题 H: 小k的简单问题 时间限制: 1 Sec  内存限制: 128 MB提交: 107  解决: 57[提交] [状态] [命题人:jsu_admin] 题目描述 地图上有n个村庄,小k每个月需 ...

  4. P2517 [HAOI2010]订货(dp)

    P2517 [HAOI2010]订货 设$f[i][j]$表示第$i$个月,库存为$j$的最小代价 枚举上个月的库存$k$,那么$f[i][j]=f[i-1][k]+(j+U[i]-k)*D[i]+j ...

  5. sql server 应用bcp进行数据导出导入

    bcp 实用工具可以在 Microsoft SQL Server 实例和用户指定格式的数据文件间大容量复制数据. 使用 bcp 实用工具可以将大量新行导入 SQL Server 表,或将表数据导出到数 ...

  6. 前端开发HTML&css入门——常用的标签以及一个小练习

    meta标签 <!doctype html> <html> <head> <meta charset="utf-8" /> < ...

  7. SwiftUI 里的 swift 闭包总结

    创建 UI 时的闭包使用 在 SwiftUI 里闭包出现的频率特别高,这里我重新梳理了下闭包的定义. 关于闭包 闭包表达式语法的一般形式如下: {(parameters) -> return t ...

  8. Rsync+sersync 数据同步指南

    (1):sersync 可以记录下被监听目录中发生变化的(包括增加.删除.修改)具体某一个文件或 某一个目录的名字: (2):rsync 在同步的时候,只同步发生变化的这个文件或者这个目录(每次发生变 ...

  9. Linux架构之Nginx Web基础1

    第41章 Nginx Web基础入门 41.1 Nginx部署 41.1.1 Nginx的安装方式   源码编译 官方仓库 epel仓库 优点 规范 安装简单 安装简单   便于管理 配置易读   缺 ...

  10. css 鼠标经过图片缓慢切换图片、鼠标离开缓慢还原

    https://blog.csdn.net/qq_26780317/article/details/80486766 一.控制背景图片在一个圆形div内切换 .header .logo { width ...