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,他 ...
随机推荐
- postgresql 口令: psql: 致命错误: 用户 认证失败
在Windows环境下使用psql 命令出现认证失败. 网上找了很多,都没讲到要点 D:\program\PostgreSql\bin>psql口令: psql: 致命错误: 用户 " ...
- CocosCreator与Laya2.0区别
1图集: Laya:直接拖拽res里面的图片,当生成图集后,会自动优先使用图集的 Cocos:应该先打图集,且图集里的图就是图集里的图,资源里的图就是资源里的.2者不同 addChild Laya:会 ...
- 攻防世界--re-for-50-plz-50
RetDec是真的难安装,太笨了~~~ 1.准备 获取信息 32位文件 2.IDA打开 发现这是MIPS代码.本来准备安装RetDec,哎...还是恶补MIPS指令知识吧:https://www.cn ...
- Tomcat 8.5 apr 模式配置
tomcat APR模式配置 一.环境 操作系统:Ubutnu 14 ubuntu@ubuntu:~$ uname -a Linux ubuntu 4.4.0-31-generic #50~14.04 ...
- spark复习笔记(6):数据倾斜
一.数据倾斜 spark数据倾斜,map阶段对key进行重新划分.大量的数据在经过hash计算之后,进入到相同的分区中,zao
- 本地文件夹上传到Github(一)
1.在要上传的文件夹下单击右键,选择Git Bash here打开Git bash,设置全局用户名和邮箱 语法:git config --global user.name wandou 语法:git ...
- 前端之CSS基础
前端之CSS 1. CSS CSS定义如何显示HTML元素. 当浏览器读到一个样式表,他就会按照这个样式表来对文档进行格式化(渲染). 3.CSS语法 1)CSS实例 每个CSS由两部分组成: 选择器 ...
- 解决GitHub push项目——Push failed: Unable to access 'https://********.git/': Failed to connect to 127.0.0.1 port 1080: Connection refused
解决方法: 第一步:在git中设置http代理 git config --global http.proxy 第二步:在git中取消http代理 git config --global --unset ...
- 四、MyBatis-映射文件
映射文件指导着MyBatis如何进行数据库增删改查,有着非常重要的意义. <?xml version="1.0" encoding="UTF-8" ?&g ...
- 设置div 居中 和得到dom元素高度
javascript中获取dom元素高度和宽度的方法如下: 网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网 ...