将Eva喜欢的颜色按顺序编个优先级,

2 3 1 5 6-> 1 2 3 4 5

然后读取stripe,将Eva不喜欢的先剔除掉,剩下的颜色替换为相应的优先级

2 2 4(去掉) 1 5 5 6 3 1 1 5 6 就变为:

1 1 3 4 4 5 2 3 3 4 5

接下来就是求最长上升子序列LIS的问题了,搞定~

O(n^2)的算法:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string.h>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7. const int maxn=+;
  8. int n,m;
  9. int level[];
  10. int vis[];
  11. int a[maxn];
  12. int main()
  13. {
  14. scanf("%d",&n);
  15. scanf("%d",&m);
  16. memset(vis,,sizeof(vis));
  17. int tmp;
  18. for(int i=;i<=m;i++){
  19. scanf("%d",&tmp);
  20. level[tmp]=i;
  21. vis[tmp]=;
  22. }
  23. int cnt=;
  24. int L;
  25. scanf("%d",&L);
  26. for(int i=;i<L;i++){
  27. scanf("%d",&tmp);
  28. if(vis[tmp]){
  29. a[cnt++]=level[tmp];
  30. }
  31. }
  32. //LIS最长上升子序列O(N^2)算法
  33. int dp[maxn]; //dp[i]表示以a[i]结尾的最长上升子序列的长度
  34. memset(dp,,sizeof(dp));
  35. int ans=;
  36. for(int i=;i<cnt;i++){
  37. dp[i]=;
  38. for(int j=;j<=i-;j++){
  39. if(a[j]<=a[i] && dp[j]+>dp[i]){
  40. dp[i]=dp[j]+;
  41. }
  42. }
  43. if(dp[i]>ans)
  44. ans=dp[i];
  45. }
  46. printf("%d\n",ans);
  47. return ;
  48. }

还想写一遍O(nlogn)的算法,但就是不知道为啥第三个样例一直WA,我觉得应该不是算法写错的问题。。。

先把代码贴出来吧,如果有人看到知道错在哪了,还请赐教~~谢谢

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string.h>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7. const int maxn=+;
  8. int n,m;
  9. int level[];
  10. int vis[];
  11. int a[maxn];
  12.  
  13. /*
  14.  
  15. 找出满足条件d[j-1]< val <= d[j]的j,最后结果返回l即为j的值
  16.  
  17. 为什么第三个样例WA????!!!!
  18.  
  19. 6
  20. 0
  21. 12 2 2 4 1 5 5 6 3 1 1 5 6
  22.  
  23. */
  24. int Binary_Search(int l,int r,int val){
  25. int mid;
  26. while(l<=r){
  27. mid=(l+r)>>;
  28. if(val>a[mid])
  29. l=mid+;
  30. else
  31. r=mid-;
  32. }
  33. return l;
  34. }
  35.  
  36. int main()
  37. {
  38. scanf("%d",&n);
  39. scanf("%d",&m);
  40. memset(vis,,sizeof(vis));
  41. int tmp;
  42. for(int i=;i<=m;i++){
  43. scanf("%d",&tmp);
  44. level[tmp]=i;
  45. vis[tmp]=;
  46. }
  47. int cnt=;
  48. int L;
  49. scanf("%d",&L);
  50. for(int i=;i<L;i++){
  51. scanf("%d",&tmp);
  52. if(vis[tmp]){
  53. a[cnt++]=level[tmp];
  54. }
  55. }
  56. //如果Eva喜欢的颜色在stripe里面没有的话,那么输出是0!然而第三个样例还是WA。。。
  57. if(cnt==){
  58. printf("0\n");
  59. }
  60. else{
  61. //LIS最长上升子序列O(NlogN)算法
  62. int dp[maxn]; //dp[i]表示长度为i的最长上升子序列中最小的末尾元素
  63. dp[]=a[];
  64. int len=;
  65. for(int i=;i<cnt;i++){
  66. if(dp[len]<=a[i]){
  67. len++;
  68. dp[len]=a[i];
  69. }
  70. else{
  71. int idx=lower_bound(dp+,dp+len+,a[i])-dp;
  72. dp[idx]=a[i];
  73. }
  74. }
  75. printf("%d\n",len);
  76. }
  77. return ;
  78. }

关于lower_bound函数,是在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置

(注意:此时的last在原数组是越界的)

PAT-1045. Favorite Color Stripe (30)-LIS的更多相关文章

  1. PAT 甲级 1045 Favorite Color Stripe (30 分)(思维dp,最长有序子序列)

    1045 Favorite Color Stripe (30 分)   Eva is trying to make her own color stripe out of a given one. S ...

  2. PAT 1045 Favorite Color Stripe[dp][难]

    1045 Favorite Color Stripe (30)(30 分) Eva is trying to make her own color stripe out of a given one. ...

  3. 1045. Favorite Color Stripe (30) -LCS允许元素重复

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

  4. 1045. Favorite Color Stripe (30) -LCS同意元素反复

    题目例如以下: Eva is trying to make her own color stripe out of a given one. She would like to keep only h ...

  5. 1045 Favorite Color Stripe (30)(30 分)

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

  6. 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 favor ...

  7. 1045 Favorite Color Stripe (30分)(简单dp)

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

  8. 【PAT甲级】1045 Favorite Color Stripe (30 分)(DP)

    题意: 输入一个正整数N(<=200),代表颜色总数,接下来输入一个正整数M(<=200),代表喜爱的颜色数量,接着输入M个正整数表示喜爱颜色的编号(同一颜色不会出现两次),接下来输入一个 ...

  9. PAT (Advanced Level) 1045. Favorite Color Stripe (30)

    最长公共子序列变形. #include<iostream> #include<cstring> #include<cmath> #include<algori ...

随机推荐

  1. 浏览器加载和渲染html的顺序-css渲染效率的探究(转载)

    1.浏览器加载和渲染html的顺序1.IE下载的顺序是从上到下,渲染的顺序也是从上到下,下载和渲染是同时进行的.2.在渲染到页面的某一部分时,其上面的所有部分都已经下载完成(并不是说所有相关联的元素都 ...

  2. 51nod 1636 教育改革

    题目链接 令f[i][j][k]为第i天选择的课程为j,设置作业为a[j]+k时的最大作业量. 那么f[i][j][k]可以由哪些状态转移而来?先把课程按复杂度排序,那么可以转移来的课程是f[i-1] ...

  3. 一道经典面试题-----setTimeout(function(){},0)

    一道经典面试题-----setTimeout(function(){},0) 转载: http://www.w3cfuns.com/notes/17398/e8a1ce8f863e8b5abb5300 ...

  4. 线程相关代码分析->常见面试题(一、Thead类)

    As always,我们直接看jdk的代码切入: 首先是最简单的Runnable接口: public interface Runnable { public abstract void run(); ...

  5. Codeforces Round #553 (Div. 2)B. Dima and a Bad XOR 思维构造+异或警告

    题意: 给出一个矩阵n(<=500)*m(<=500)每一行任选一个数 异或在一起 求一个 异或在一起不为0 的每行的取值列号 思路: 异或的性质  交换律 x1^x2^x3==x3^x2 ...

  6. MP实战系列(十五)之执行分析插件

    SQL 执行分析拦截器[ 目前只支持 MYSQL-5.6.3 以上版本 ],作用是分析 处理 DELETE UPDATE 语句, 防止小白或者恶意 delete update 全表操作! 这里我引用M ...

  7. jQuery封装自定义事件--valuechange(动态的监听input,textarea)之前值,之后值的变化

    jQuery封装自定义事件--valuechange(动态的监听input,textarea)之前值,之后值的变化 js监听输入框值的即时变化 网上有很多关于 onpropertychange.oni ...

  8. Python2.7-bz2

    bz2模块,提供 bz2 压缩的接口,一般使用 BZ2File 类来完成操作,操作的文件是后缀为“.bz2”的文件 1.模块方法 bz2.compress(data[, compresslevel]) ...

  9. dom4j加载xml文件

    ## dom4j加载xml文件 ``` // 1. 加载xml文件 InputStream is = MyTest.class.getResourceAsStream("user.xml&q ...

  10. Jmeter—实现识别验证码登录

    在做自动化测试或压力测试时,验证码总是一个问题.在以往的压力测试经历中,测试一般在独立的测试环境中进行,可以放心禁用验证码或使用万能验证码,这个是最实用的.但是,这两天我尝试了一个使用第三方的图形图像 ...