5985: 矩形嵌套

题意:求最长递增子序列(包含两个元素)

思路:先找出关系式子;

li=lj+1(当ai<aj时)

两层循环 第一层i从1-n

第二层j 从0-i ;求出i前面的每个j 的max长度再加上自己即1 so要初始化dp【0-n】=1

#include<bits/stdc++.h>
using namespace std;
struct node{
int x,y;
}qu[1005];
bool cmp(node a,node b)
{
if(a.x==b.x)return a.y<b.y;
return a.x<b.x;
}
int dp[1005];
int main()
{
int t,n,i,j;
cin>>t;
while(t--){
cin>>n;
for(i=0;i<n;i++){
int u,v;cin>>u>>v;
qu[i].x=min(u,v);
qu[i].y=max(u,v);
dp[i]=1;
}
sort(qu,qu+n,cmp);
for(i=1;i<n;i++){
for(j=0;j<i;j++){
// cout<<i<<" i-j "<<j<<" ";
if(qu[j].x<qu[i].x&&qu[j].y<qu[i].y){
// cout<<dp[i]<<" "<<dp[j]+1;
dp[i]=max(dp[i],dp[j]+1);
}
// cout<<endl;
}
}
int maxx=dp[0];
for(i=1;i<n;i++)maxx=max(maxx,dp[i]);
cout<<maxx<<endl;
}
return 0;
}

3229: Rectangles

解法同上

#include<bits/stdc++.h>
using namespace std;
struct node{
int x1,y1,x2,y2;
}qu[1005];
bool cmp(node a,node b)
{
return a.x1<b.x1|| a.x1==b.x1&&a.y1<b.y1|| a.x1==b.x1&&a.y1==b.y1&&a.x2<b.x2|| a.x1==b.x1&&a.y1==b.y1&&a.x2==b.x2&&a.y2<b.y2;
}
int dp[1005];
int main()
{
int t,n,i,j;
while(scanf("%d",&n),n!=0){
for(i=0;i<n;i++){
scanf("%d%d%d%d",&qu[i].x1,&qu[i].y1,&qu[i].x2,&qu[i].y2);
dp[i]=1;
}
sort(qu,qu+n,cmp);
for(i=1;i<n;i++){
for(j=0;j<i;j++){
if(qu[j].x2<qu[i].x1&&qu[j].y2<qu[i].y1){
dp[i]=max(dp[i],dp[j]+1);
}
}
}
int maxx=dp[0];
for(i=1;i<n;i++)maxx=max(maxx,dp[i]);
printf("%d\n",maxx);
}
return 0;
}

这题也可以用记忆化搜索

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1005;
struct p{
int x1,x2,y1,y2;
}rec[N];
int vis[N][N],res[N],n;
int dfs(int u)
{
if(res[u]) return res[u];
for(int i=0;i<n;i++)
if(vis[u][i]) res[u]=max(res[u],dfs(i)+1);
res[u]=max(res[u],1);
return res[u];
}
void init()
{
memset(vis,0,sizeof(vis));
memset(res,0,sizeof(res));
}
int main()
{
while(~scanf("%d",&n),n)
{
init();
for(int i=0;i<n;i++) scanf("%d%d%d%d",&rec[i].x1,&rec[i].y1,&rec[i].x2,&rec[i].y2);
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(rec[i].x2<rec[j].x1&&rec[i].y2<rec[j].y1) vis[i][j]=1;
int ans=0;
for(int i=0;i<n;i++)
ans=max(ans,dfs(i));
printf("%d\n",ans);
}
}

最长上升子序列(LIS) dp典型例题(tzoj 矩形嵌套,Rectangles )的更多相关文章

  1. 动态规划(DP),最长递增子序列(LIS)

    题目链接:http://poj.org/problem?id=2533 解题报告: 状态转移方程: dp[i]表示以a[i]为结尾的LIS长度 状态转移方程: dp[0]=1; dp[i]=max(d ...

  2. 1. 线性DP 300. 最长上升子序列 (LIS)

    最经典单串: 300. 最长上升子序列 (LIS) https://leetcode-cn.com/problems/longest-increasing-subsequence/submission ...

  3. 最长回文子序列LCS,最长递增子序列LIS及相互联系

    最长公共子序列LCS Lintcode 77. 最长公共子序列 LCS问题是求两个字符串的最长公共子序列 \[ dp[i][j] = \left\{\begin{matrix} & max(d ...

  4. 最长上升子序列LIS(51nod1134)

    1134 最长递增子序列 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素是递 ...

  5. 最长上升子序列(LIS)与最长公共子序列(LCS)

    1.LIS : 给定一个序列,求它的最长上升子序列(n<=2000) 第一种 O(n^2): dp[i] 为以i为开头的最长上升子序列长度 code1: #include<cstdio&g ...

  6. 【部分转载】:【lower_bound、upperbound讲解、二分查找、最长上升子序列(LIS)、最长下降子序列模版】

    二分 lower_bound lower_bound()在一个区间内进行二分查找,返回第一个大于等于目标值的位置(地址) upper_bound upper_bound()与lower_bound() ...

  7. 2.16 最长递增子序列 LIS

    [本文链接] http://www.cnblogs.com/hellogiser/p/dp-of-LIS.html [分析] 思路一:设序列为A,对序列进行排序后得到B,那么A的最长递增子序列LIS就 ...

  8. 题解 最长上升子序列 LIS

    最长上升子序列 LIS Description 给出一个 1 ∼ n (n ≤ 10^5) 的排列 P 求其最长上升子序列长度 Input 第一行一个正整数n,表示序列中整数个数: 第二行是空格隔开的 ...

  9. POJ - 1631 Bridging signals(最长上升子序列---LIS)

    题意:左右各n个端口,已知n组线路,要求切除最少的线路,使剩下的线路各不相交,按照左端口递增的顺序输入. 分析: 1.设左端口为l,右端口为r,因为左端口递增输入,l[i] < l[j](i & ...

  10. 一个数组求其最长递增子序列(LIS)

    一个数组求其最长递增子序列(LIS) 例如数组{3, 1, 4, 2, 3, 9, 4, 6}的LIS是{1, 2, 3, 4, 6},长度为5,假设数组长度为N,求数组的LIS的长度, 需要一个额外 ...

随机推荐

  1. Java基础-类型转换、变量、变量命名规范

    类型转换 强制转换 (类型)变量名 高-->低 自动转换 低-->高 注意点 不能对布尔值进行转换 不能把对象类型转换为不相干的类型 在把高容量转换到低容量的时候,强制转换 转换的时候可能 ...

  2. 2.4 OpenEuler中C语言中的函数调用测试

    2.4 OpenEuler中C语言中的函数调用测试 任务详情 在X86_64架构下实践2.4中的内容 通过GDB查看寄存器的内容,把教材中的图填入具体的值 把2.4的C代码在OpenEuler中重新实 ...

  3. (0303)怎么在sequence中调用agent中的函数以及如何快速实验你的想法?

    https://mp.weixin.qq.com/s/9hDz9-nur5szBib18_yPnA

  4. Vue3中使用JSX简明语法

    掘金JSX:https://juejin.cn/post/7114063575122984973

  5. FFmpeg转换直播流格式

    mp4转rtsp ffmpeg -re -i 1671680590843.mp4 -vcodec copy -acodec copy -f rtsp rtsp://localhost:8554/liv ...

  6. 使用python启动appium(虚拟器)

    1.先安装各种库 https://www.cnblogs.com/zhanglingling00/p/14169462.html pip install Appium-Flutter-Finder p ...

  7. Kotlin属性委托

    业务定义 对于属性,我们可以读取(get)和赋值(set),在Java中会定义get和set方法来操作属性,Kotlin的属性建议直接操作,一些业务的要求会对属性有额外的功能需求,在Java中会在ge ...

  8. sql常用记录

    sqlserver 在已有值的列上自动增加 获取列最大的值 declare @Field int select @Field = ISNULL(Max(Field),0) from SupCsBill ...

  9. Flutter配置签名打包全流程填坑笔记

    1.配置包名和版本 找到android-app-src-build.gradle文件在defaultConfig{...}中配置好版本号以及包名 2.生成key 将keytool路径添加进环境变量,c ...

  10. Java 接口内容小结

    Java接口学习:https://www.cnblogs.com/mlllily/p/14923837.html 小结内容: 在Java9+版本中,接口内容可以有常量.抽象方法.默认方法.静态方法.私 ...