PAT-1045. Favorite Color Stripe (30)-LIS
将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的更多相关文章
- 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 ...
- 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. ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 【PAT甲级】1045 Favorite Color Stripe (30 分)(DP)
题意: 输入一个正整数N(<=200),代表颜色总数,接下来输入一个正整数M(<=200),代表喜爱的颜色数量,接着输入M个正整数表示喜爱颜色的编号(同一颜色不会出现两次),接下来输入一个 ...
- PAT (Advanced Level) 1045. Favorite Color Stripe (30)
最长公共子序列变形. #include<iostream> #include<cstring> #include<cmath> #include<algori ...
随机推荐
- 使用Index()+Match()函数实现更为灵活的VLookUp()
上一篇 http://www.cnblogs.com/-SANG/p/8407017.html 文章中已经介绍了vlookup的用法. 今天要使用index+match实现更为灵活的vlookup 先 ...
- Hadoop HBase概念学习系列之HLog(二)
首先,明确,HRegion服务器包含两大部分:HLog和HRegion. HLog用来存储数据日志,采用的是先写日志的方式. 当用户需要更新数据的时候,数据会被分配到对应的HRegion服务器上提交修 ...
- 阿里八八Alpha阶段Scrum(9/12)
今日进度 叶文滔: 成功完成多级悬浮按钮,并添加与日程输入界面的连接.debug了一些对接产生的问题 黄梅玲: 合并单日项目至多日.获取json数据 王国超: 完成了初步的多日界面,pull至项目.进 ...
- 联想笔记本BIOS设置中文详解
对于很多新装系统的小伙伴们 可能很多都不是太懂BIOS中都是干什么用的,小编这里给大家详细介绍一下 联想笔记本的主板BIOS设置跟别的笔记本或许有些不同但大体相差不多,和大家分享一下. BIOS介绍 ...
- ceph kubernetes中使用
1.在管理节点上,进入刚创建的放置配置文件的目录,用 ceph-deploy 执行如下步骤 mkdir /opt/cluster-ceph cd /opt/cluster-ceph ceph-depl ...
- Ubuntu16.04安装Zabbix3.2(快速安装教程)
ubuntu16.04下zabbix安装和配置 上面这篇虽然是一样的,但是针对的版本有差异,版本差异,安装方式也就有差异,对要求的环境可能有所不同. 今天帮同事安装zabbix,按照如下几个步骤,就完 ...
- OpenCV——滤波
常见滤波函数: 其中前3中为线性滤波,后2中为非线性滤波 参考资料:https://mp.weixin.qq.com/s/3maNqlTSkl6FQs_xqrs8Fg
- 了解linux的进程:rootfs与linuxrc
导读 内核启动的最后阶段启动了三个进程进程0:进程0其实就是刚才讲过的idle进程,叫空闲进程,也就是死循环.进程1:kernel_init函数就是进程1,这个进程被称为init进程.进程2:kthr ...
- anaconda使用以及创建python3.7+pytorch1.0虚拟环境以及Jupyter notebook初级使用
查看所有已安装的软件包$ conda list# packages in environment at S:\Users\jiangshan\Anaconda3:## Name Version Bui ...
- SVN 错误收集
一.the working copy is locked due to a previous error 解决办法:在 Cornerstone 右键本地工程 --> clean.