poj1065Wooden Sticks(dp——最长递减数列)
Description
(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
Output
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——最长递减数列)的更多相关文章
- POJ - 1065 Wooden Sticks(贪心+dp+最长递减子序列+Dilworth定理)
题意:给定n个木棍的l和w,第一个木棍需要1min安装时间,若木棍(l’,w’)满足l' >= l, w' >= w,则不需要花费额外的安装时间,否则需要花费1min安装时间,求安装n个木 ...
- POJ1065Wooden Sticks[DP LIS]
Wooden Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21902 Accepted: 9353 De ...
- HOJ Recoup Traveling Expenses(最长递减子序列变形)
A person wants to travel around some places. The welfare in his company can cover some of the airfar ...
- 代码的坏味道(4)——过长参数列(Long Parameter List)
坏味道--过长参数列(Long Parameter List) 特征 一个函数有超过3.4个入参. 问题原因 过长参数列可能是将多个算法并到一个函数中时发生的.函数中的入参可以用来控制最终选用哪个算法 ...
- hdu 1025 dp 最长上升子序列
//Accepted 4372 KB 140 ms //dp 最长上升子序列 nlogn #include <cstdio> #include <cstring> #inclu ...
- 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 ...
- 最长递减子序列(nlogn)(个人模版)
最长递减子序列(nlogn): int find(int n,int key) { ; int right=n; while(left<=right) { ; if(res[mid]>ke ...
- [Swift]LeetCode665. 非递减数列 | Non-decreasing Array
Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...
- P1091 合唱队形 DP 最长升序列维护
题目描述 NN位同学站成一排,音乐老师要请其中的(N-KN−K)位同学出列,使得剩下的KK位同学排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左到右依次编号为1,2,…,K1,2,…,K,他 ...
随机推荐
- BZOJ 1179 (Tarjan缩点+DP)
题面 传送门 分析 由于一个点可以经过多次,显然每个环都会被走一遍. 考虑缩点,将每个强连通分量缩成一个点,点权为联通分量上的所有点之和 缩点后的图是一个有向无环图(DAG) 可拓扑排序,按照拓扑序进 ...
- python RE表达式规则剩余规则
前面我学习了 ’.‘ '^' '$' '*' '+' '?' 基本针对单个字符的,学习python 表达式规则剩余规则. 1,{m} 匹配前一个字符m次 2,{n,.m} 匹配前一个字符n到m次 3 ...
- Vue-cli2项目文件目录解析
前言 不是原创,真的不是原创,主要我是根据CSDN的一篇文章和其他平台上的文章整理而来,在最后我会贴上所有原文的地址,下面正式进入正文. Vue-cli项目文件目录结构 这个是Vue-cli2.0版本 ...
- zTree根节点默认打开
1.在生成tree的json数据中,直接给出:open:true的属性; 2.treeObj.expandAll(true); 3.var zTree = $.fn.zTree.getZTreeObj ...
- 刚新建好的动态网站项目,创建jsp页面就报错??
拿到刚刚可以运行的Eclipse,就马上想敲码了,但一创建项目之后再创建jsp页面就报错= =! 报错的内容大概为缺乏对应的jar包. 我们常用Tomcat为中间体,而他本身是带有开发jsp网站的对应 ...
- Python + logging输出到屏幕,将log日志写入到文件
logging提供了一组便利的函数,用来做简单的日志.它们是 debug(). info(). warning(). error() 和 critical(). logging函数根据它们用来跟踪的事 ...
- 22pygame 安装
实战步骤 pygame 快速体验 飞机大战 实战 确认模块 --pygame pygame 就是一个 Python 模块, 专为电子游戏设计 提示 : 学习第三方模块, 通常最好的参考资料就在官方网站 ...
- POJ-1390-Blocks (复杂区间DP)
$ POJ~1390~~Blocks: $ (很难想的区间DP) $ solution: $ 很好的一道题目.看起来似乎很简单,当时一直认为可以用二维区间DP来完成,转移 $ n^3 $ . 后来发现 ...
- mobiscroll实现二级联动菜单
mobiscroll是一款非常使用的移动端选择控件,一般用来日期时间的选择的多,其实从官网上可以看到它有很多方面的使用,这里就不一一介绍了,有兴趣可以去官网上查阅一下 https://demo.mob ...
- selenium 自动化的坑(2)
UI自动化,一天一坑系列(2) 今天要介绍的坑是这样的:在使用google浏览器的过程中,F12查看页面元素,我的操作步骤是先F12,然后点击箭头,接着点击要查找的元素来实现元素查看,不知道你是不是这 ...