将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)的算法:

#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm> using namespace std;
const int maxn=+;
int n,m;
int level[];
int vis[];
int a[maxn];
int main()
{
scanf("%d",&n);
scanf("%d",&m);
memset(vis,,sizeof(vis));
int tmp;
for(int i=;i<=m;i++){
scanf("%d",&tmp);
level[tmp]=i;
vis[tmp]=;
}
int cnt=;
int L;
scanf("%d",&L);
for(int i=;i<L;i++){
scanf("%d",&tmp);
if(vis[tmp]){
a[cnt++]=level[tmp];
}
}
//LIS最长上升子序列O(N^2)算法
int dp[maxn]; //dp[i]表示以a[i]结尾的最长上升子序列的长度
memset(dp,,sizeof(dp));
int ans=;
for(int i=;i<cnt;i++){
dp[i]=;
for(int j=;j<=i-;j++){
if(a[j]<=a[i] && dp[j]+>dp[i]){
dp[i]=dp[j]+;
}
}
if(dp[i]>ans)
ans=dp[i];
}
printf("%d\n",ans);
return ;
}

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

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

#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm> using namespace std;
const int maxn=+;
int n,m;
int level[];
int vis[];
int a[maxn]; /* 找出满足条件d[j-1]< val <= d[j]的j,最后结果返回l即为j的值 为什么第三个样例WA????!!!! 6
0
12 2 2 4 1 5 5 6 3 1 1 5 6 */
int Binary_Search(int l,int r,int val){
int mid;
while(l<=r){
mid=(l+r)>>;
if(val>a[mid])
l=mid+;
else
r=mid-;
}
return l;
} int main()
{
scanf("%d",&n);
scanf("%d",&m);
memset(vis,,sizeof(vis));
int tmp;
for(int i=;i<=m;i++){
scanf("%d",&tmp);
level[tmp]=i;
vis[tmp]=;
}
int cnt=;
int L;
scanf("%d",&L);
for(int i=;i<L;i++){
scanf("%d",&tmp);
if(vis[tmp]){
a[cnt++]=level[tmp];
}
}
//如果Eva喜欢的颜色在stripe里面没有的话,那么输出是0!然而第三个样例还是WA。。。
if(cnt==){
printf("0\n");
}
else{
//LIS最长上升子序列O(NlogN)算法
int dp[maxn]; //dp[i]表示长度为i的最长上升子序列中最小的末尾元素
dp[]=a[];
int len=;
for(int i=;i<cnt;i++){
if(dp[len]<=a[i]){
len++;
dp[len]=a[i];
}
else{
int idx=lower_bound(dp+,dp+len+,a[i])-dp;
dp[idx]=a[i];
}
}
printf("%d\n",len);
}
return ;
}

关于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. November 14th, 2017 Week 46th Tuesday

    Eternity is said not to be an extension of time but an absence of time. 永恒不是时间的无限延伸,而是没有时间. What is ...

  2. css常见知识点

    1.内核区分 希望某一个浏览器能一统江湖 -ms-transform:rotate(7deg); //-ms代表ie内核识别码 -moz-transform:rotate(7deg); //-moz代 ...

  3. make报错

    笔记本Ubuntu16.04环境下,进入项目的src目录下执行make操作,发现报如下错误 /bin/sh: 1: /usr/bin/libtool: not found makefile:89: r ...

  4. 4、url控制系统

    第1节:简单配置 参考代码: from django.conf.urls import url from . import views urlpatterns = [ url(r'^articles/ ...

  5. Python接口自动化--URL参数的编码和解码 6

    # _*_ coding:utf-8 _*_ #python2 import urllib #有时,需要从上一个请求的url获取参数,传到下一个请求中,中文会显示为编码的形式,这时候就需要进行解码 u ...

  6. Guava 的EventBus示例代码(简单笔记,后期补充)

    package guavademo.event.bus; import com.google.common.eventbus.EventBus; import com.google.common.ev ...

  7. 欢迎来到Curl的世界

    一.Curl 简介 curl命令是一个利用URL规则在命令行下工作的文件传输工具.它支持文件的上传和下载,所以是 综合传输工具,但按传统,习惯称curl为下载工具.作为一款强力工具,curl支持包括H ...

  8. python is、==区别;with;gil;python中tuple和list的区别;Python 中的迭代器、生成器、装饰器

    1. is 比较的是两个实例对象是不是完全相同,它们是不是同一个对象,占用的内存地址是否相同 == 比较的是两个对象的内容是否相等 2. with语句时用于对try except finally 的优 ...

  9. torchvision

    torchvision是一个包,它服务于pytorch深度学习框架,用来生成图片,视频数据集,模型类和预训练的模型torchvision由以下四个部分组成:1. torchvision.dataset ...

  10. easyui的datagrid

    datagrid数据的绑定方式: 1)data 后跟数据行的json串 2)url 后跟{"total":,"rows":,"foot":} ...