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 ...
随机推荐
- 浏览器加载和渲染html的顺序-css渲染效率的探究(转载)
1.浏览器加载和渲染html的顺序1.IE下载的顺序是从上到下,渲染的顺序也是从上到下,下载和渲染是同时进行的.2.在渲染到页面的某一部分时,其上面的所有部分都已经下载完成(并不是说所有相关联的元素都 ...
- 51nod 1636 教育改革
题目链接 令f[i][j][k]为第i天选择的课程为j,设置作业为a[j]+k时的最大作业量. 那么f[i][j][k]可以由哪些状态转移而来?先把课程按复杂度排序,那么可以转移来的课程是f[i-1] ...
- 一道经典面试题-----setTimeout(function(){},0)
一道经典面试题-----setTimeout(function(){},0) 转载: http://www.w3cfuns.com/notes/17398/e8a1ce8f863e8b5abb5300 ...
- 线程相关代码分析->常见面试题(一、Thead类)
As always,我们直接看jdk的代码切入: 首先是最简单的Runnable接口: public interface Runnable { public abstract void run(); ...
- Codeforces Round #553 (Div. 2)B. Dima and a Bad XOR 思维构造+异或警告
题意: 给出一个矩阵n(<=500)*m(<=500)每一行任选一个数 异或在一起 求一个 异或在一起不为0 的每行的取值列号 思路: 异或的性质 交换律 x1^x2^x3==x3^x2 ...
- MP实战系列(十五)之执行分析插件
SQL 执行分析拦截器[ 目前只支持 MYSQL-5.6.3 以上版本 ],作用是分析 处理 DELETE UPDATE 语句, 防止小白或者恶意 delete update 全表操作! 这里我引用M ...
- jQuery封装自定义事件--valuechange(动态的监听input,textarea)之前值,之后值的变化
jQuery封装自定义事件--valuechange(动态的监听input,textarea)之前值,之后值的变化 js监听输入框值的即时变化 网上有很多关于 onpropertychange.oni ...
- Python2.7-bz2
bz2模块,提供 bz2 压缩的接口,一般使用 BZ2File 类来完成操作,操作的文件是后缀为“.bz2”的文件 1.模块方法 bz2.compress(data[, compresslevel]) ...
- dom4j加载xml文件
## dom4j加载xml文件 ``` // 1. 加载xml文件 InputStream is = MyTest.class.getResourceAsStream("user.xml&q ...
- Jmeter—实现识别验证码登录
在做自动化测试或压力测试时,验证码总是一个问题.在以往的压力测试经历中,测试一般在独立的测试环境中进行,可以放心禁用验证码或使用万能验证码,这个是最实用的.但是,这两天我尝试了一个使用第三方的图形图像 ...