将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. Oracle修改表空间为自动扩展

    https://gqsunrise.iteye.com/blog/2015692 1.数据文件自动扩展的好处1)不会出现因为没有剩余空间可以利用到数据无法写入2)尽量减少人为的维护3)可以用于重要级别 ...

  2. Dig命令解析结果

    dig -t RT NAME @NS -t RT 指定要查询的资源记录类型 NAME 需要解析的域(域名) @NS 指定那个域名服务器负责解析 [root@xss ~]# dig www.ihoney ...

  3. 一兄弟把/etc/init.d/functions误删除了,这是多么悲催的节奏啊;

    RPM resource /lib/lsb/init-functions /lib/lsb/init-functions vs. /etc/init.d/functions in init scrip ...

  4. websocket python实现原理

    """ pip install ws4py """ import json from ws4py.client.threadedclient ...

  5. Android Fragment(二)

    废话:在上一篇的博客中我们给出了Fragment的简单介绍,这一片博客给大家介绍一下Fragment到底该怎样用.主要都用在哪方面等等. 需求:现有一个界面,要求,竖屏时界面的背景颜色为红色,横屏时界 ...

  6. nodejs开发调试时,使用supervisor

    如果你有 PHP 开发经验,会习惯在修改 PHP 脚本后直接刷新浏览器以观察结果,而你在开发 Node.js 实现的 HTTP 应用时会发现,无论你修改了代码的哪一部份,都必须终止Node.js 再重 ...

  7. 开关电源PCB设计中的布线技巧

    开关电源PCB设计中的布线技巧关键字:布线 开关电源 走线 一.引言 开关电源是一种电压转换电路,主要的工作内容是升压和降压,广泛应用于现代电子产品.因为开关三极管总是工作在 “开” 和“关” 的状态 ...

  8. Windows控制程序网站带宽及Qos(TOS或DSCP)

    [基于策略的 Qos]位置:gpedit.msc->本地计算机策略->用户配置->Windows 设置->基于策略的 Qos

  9. springboot mybatis 后台框架平台 集成代码生成器 shiro 权限

    1.代码生成器: [正反双向](单表.主表.明细表.树形表,快速开发利器)freemaker模版技术 ,0个代码不用写,生成完整的一个模块,带页面.建表sql脚本.处理类.service等完整模块2. ...

  10. vb用createprocess启动其他应用程序

    Option Explicit Private Type PROCESS_INFORMATION hProcess As Long hThread As Long dwProcessId As Lon ...