解题思路

本题属于Longest Increasing Sequence最长不下降子序列,但是要注意,LIS当中不会有无效的元素,而本题是有的,所以先要把无效元素过滤掉,才能转化成为LIS问题。

这里用到了hashTable(用map更慢),初始化全零。有两个用处:1.将元素设置成伊娃规定的的顺序,然后映射到顺序上,不再对原数组进行排序(因为反正是计数,不需要保留原数据) 2.是过滤掉不是伊娃想要的元素。

有一个特别易错的点,我认为递归边界是dp[0] = 1,但是这不严谨,因为在过滤一遍之后,每个元素都能自成一个子序列,所以应当是dp[i]  = 1。

·珍藏版原创测试用例

4
3 1 2 3
10 4 2 4 1 1 2 1 1 1 4

AC代码

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;const int INF = 1000000000;//10的9次方
const int maxn = 10010;
const double eps = 1e-3;int type,colorN,EvaN;//感觉type是无用的
int hashTable[210] = {0};
int b[maxn];
int dp[maxn] = {0}; int main(){scanf("%d",&type);scanf("%d",&EvaN);for(int i=1;i<=EvaN;i++){int color;scanf("%d",&color);hashTable[color] = i; }int num = 0;scanf("%d",&colorN);for(int i=0;i<colorN;i++){int color;scanf("%d",&color);if(hashTable[color]>0)b[num++] = hashTable[color];}int ans = 0;for(int i=0;i<num;i++){dp[i] = 1;//每一轮更新一个元素的dpfor(int j=0;j<i;j++){if(dp[j]+1>dp[i]&&b[j]<=b[i])dp[i] = dp[j] + 1;}//每一轮结束更新ansans = max(ans,dp[i]); }printf("%d\n",ans);return 0;
}

1045 Favorite Color Stripe(LIS解法)相关推荐

  1. 1045 Favorite Color Stripe(LCS解法) 需再理解

    解题思路 使用LCS方法解这一题,首先要把现有的颜色和Eva的颜色看成即将取材的序列s和e. 而且注意2个序列都要从1开始读入,因为递归边界(待后叙). dp[i][j]代表的是e[1]-e[i]和s ...

  2. 1045 Favorite Color Stripe(最长不下降子序列)

    1045 Favorite Color Stripe(最长不下降子序列) 题意:按照题目给出的颜色序列找出原颜色序列中对应的子序列,给出的颜色序列不需要在子序列中全部出现. 解法:根据给出的序列对相应 ...

  3. PAT甲级1045 Favorite Color Stripe (30 分):[C++题解]最佳彩色带、DP、公共子序列变形

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:这是一个公共子序列的问题.但是有点变式,序列a和序列b不是完全等价的,序列a的每个元素可以对应多个相同元素,而且有些元素可以不使用.比 ...

  4. 1045. Favorite Color Stripe (30)

    题目连接:https://www.patest.cn/contests/pat-a-practise/1045原题如下: Eva is trying to make her own color str ...

  5. 1045 Favorite Color Stripe(30分)-PAT甲级

    Eva is trying to make her own color stripe(条纹) out of a given one. She would like to keep only her f ...

  6. PAT甲级-1045 Favorite Color Stripe (30分)

    点击链接PAT甲级-AC全解汇总 题目: Eva is trying to make her own color stripe out of a given one. She would like t ...

  7. 1045 Favorite Color Stripe (30分)

    题目 Eva is trying to make her own color stripe out of a given one. She would like to keep only her fa ...

  8. 1045 Favorite Color Stripe (30 分)【难度: 中 / 知识点: DP】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805437411475456 这一道题,实际上就是一道最长上升子序列的一个 ...

  9. pat1045. Favorite Color Stripe (30)

    1045. Favorite Color Stripe (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

最新文章

  1. 获取指定路径文件方法
  2. 各类木材强度_木材的力学性能
  3. C#调用ORACLE存储过程返回结果集及函数
  4. lua检测表中是否有某个值_Lua基础知识总结(入职面试题)
  5. STM8单片机串口发送引脚和接收引脚分开使用
  6. vs2010上opencv配置
  7. Creator3D:炫动球球(音乐游戏)实现分享
  8. 十大开源游戏引擎深入比较之美
  9. Android 如何正确的获取屏幕高度
  10. PHP将uncode转utf8,一行代码解决问题
  11. 【其实是空空荡荡的牵挂,没有结局的怀念】
  12. 计算机中 空间局限性(Spatial Locality)与时间局限性(Temporal Locality)
  13. asp.net打印错误日志
  14. 使用fetch 遇到的ReadableStream is locked 的问题
  15. 97-微服务项目的编写(上篇)
  16. java ping域名
  17. Font shape `OMX/cmex/m/n‘ in size <10.53937> not available (Font) size <10.95> substituted.
  18. vs code 语法_VS Code上的Java现在具有语法模式和SonarLint支持
  19. 企业寄快递软件就选快递管家,让员工和老板双满意的精细化服务
  20. PCA9306—I2C总线和SMBus双向电平转换器

热门文章

  1. 小程序内容审核违规过滤,在小程序使用security.msgSecCheck
  2. 判断数组里面的下标是否等于一个字符串
  3. [微信小程序]组件化开发,以一个自定义模块框组件当做示例(附完整示例代码和效果图)
  4. 1-Swift中的Struct 和 Class
  5. 一个考查作用域以及闭包的题目
  6. matlab处理txt文件数据
  7. Phpcms V9手机门户设置教程:怎么用PC V9做手机网站
  8. 在Server 2003上部署IIS+PHP+MySQL配置清单
  9. 《CLR via C#》笔记——CLR的执行模型
  10. as3自定义加载图片类