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 ...
随机推荐
- 树莓派踩坑备忘录 -- 使用 Linux
目录 一,工欲善其事,必先利其器 二,开机必备 三,更新 apt-get 源与软件搜索 四,安装 .NET Core 五,文件传输 六,搜索与安装软件 七,常见缺少的 xxx.so 八,小技巧与工具 ...
- 《Effective C++》 目录:
转自:http://blog.csdn.net/KangRoger/article/details/44706403 目录 条款1:视C++为一个语言联邦 条款2:尽量以const.enum.inli ...
- C# 利用VS自带的WSDL工具生成WebService服务类(转载)
WebService有两种使用方式,一种是直接通过添加服务引用,另一种则是通过WSDL生成. 添加服务引用大家基本都用过,这里就不讲解了. 那么,既然有直接引用的方式,为什么还要通过WSDL生成呢? ...
- windows下安装python3 新手上路
本文只针对刚刚拿到“驾照”的实习生 老司机回去开车.. 下载python 地址:https://www.python.org/ 选择Downloads下的windows 选择自己合适的版本 下面的是 ...
- ip 报文头
- ATP学姐的模拟赛
ATPの水题大赛 声明:不是我觉得这题水,这就是本场模拟赛的名称. T1:求所有的$n$位数中有几个数满足:每一位要么是$A$要么是$B$,并且这个$n$位数的每一位加起来是$A$或$B$的倍数. $ ...
- BZOJ4245:[ONTAK2015]OR-XOR(贪心)
Description 给定一个长度为n的序列a[1],a[2],...,a[n],请将它划分为m段连续的区间,设第i段的费用c[i]为该段内所有数字的异或和,则总费用为c[1] or c[2] or ...
- Hadoop安装教程【转】
原贴:http://www.powerxing.com/install-hadoop/ 当开始着手实践 Hadoop 时,安装 Hadoop 往往会成为新手的一道门槛.尽管安装其实很简单,书上有写到, ...
- Postman-自动化传参
一,自动化传参 在实现接口自动测试的时候,会经常遇到接口参数依赖的问题,例如调取登录接口的时候,需要先获取登录的key值,而每次请求返回的key值又是不一样的,那么这种情况下,要实现接口的自动化,就要 ...
- /etc/hosts,GoldenGate
[oracle@g]$ netstat -alp|grep 7809(Not all processes could be identified, non-owned process info wil ...