pat1045. Favorite Color Stripe (30)
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 favorite colors in her favorite order by cutting off those unwanted pieces and sewing the remaining parts together to form her favorite color stripe.
It is said that a normal human eye can distinguish about less than 200 different colors, so Eva's favorite colors are limited. However the original stripe could be very long, and Eva would like to have the remaining favorite stripe with the maximum length. So she needs your help to find her the best result.
Note that the solution might not be unique, but you only have to tell her the maximum length. For example, given a stripe of colors {2 2 4 1 5 5 6 3 1 1 5 6}. If Eva's favorite colors are given in her favorite order as {2 3 1 5 6}, then she has 4 possible best solutions {2 2 1 1 1 5 6}, {2 2 1 5 5 5 6}, {2 2 1 5 5 6 6}, and {2 2 3 1 1 5 6}.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (<=200) which is the total number of colors involved (and hence the colors are numbered from 1 to N). Then the next line starts with a positive integer M (<=200) followed by M Eva's favorite color numbers given in her favorite order. Finally the third line starts with a positive integer L (<=10000) which is the length of the given stripe, followed by L colors on the stripe. All the numbers in a line are separated by a space.
Output Specification:
For each test case, simply print in a line the maximum length of Eva's favorite stripe.
Sample Input:
6
5 2 3 1 5 6
12 2 2 4 1 5 5 6 3 1 1 5 6
Sample Output:
7
注意以后不要用动态申请内存,直接申请。
核心思想:对于的当前的颜色A条纹,找到颜色优先权比A大并且当前累积条纹个数最大的颜色B条纹(B可以等于A),然后颜色A条纹的个数=颜色B条纹的个数+1(即颜色A条纹的前面最后剪放颜色B条纹)。最后取颜色A条纹的最大值,即为符合条件的最大条纹个数。
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<set>
using namespace std;
/*struct node{
int v,fr;//fr前面有多少个比他大的数
};*/
map<int,int> getorder;
int colornum[];//前一种颜色条纹当前的数量
int order[];//题目给出的颜色优先权
//只要考虑优先权不小于当前颜色的颜色B条纹数量的最大值,那么直接从B跳到当前颜色A条纹,当前颜色A的数量=B的数量+1
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int n,m,l,i,j;
scanf("%d",&n);
//int *colornum=new int[n+5];//前一种颜色当前的数量
//memset(colornum,0,sizeof(colornum));
/*for(i=0;i<=n;i++){
cout<<"i: "<<i<<" "<<colornum[i]<<endl;
}*/
scanf("%d",&m);
//int *order=new int[m+1];//题目给出的颜色优先权
for(i=;i<=m;i++){
scanf("%d",&order[i]);
getorder[order[i]]=i;//由颜色得到优先顺序,然后当前颜色条纹个数=max{之前所有颜色条纹个数,当前颜色条纹个数}+1;
}
scanf("%d",&l);
int maxlen=-,color;
//node *stripe=new node[l+1];//放题目给出的颜色
for(i=;i<=l;i++){
scanf("%d",&color);//当前的颜色
if(!getorder.count(color)){//注意不在队列中的颜色不进行分析
continue;
}
//只要考虑优先权不小于当前颜色的颜色A数量的最大值,那么直接从A跳到当前颜色,当前颜色的数量=A的数量+1
int curnum=getorder[color],maxnum=;
for(j=;j<=curnum;j++){
if(colornum[order[j]]>colornum[order[maxnum]]){
maxnum=j;
}
}
colornum[order[curnum]]=colornum[order[maxnum]]+;//加上当前的颜色
if(colornum[order[curnum]]>maxlen){
maxlen=colornum[order[curnum]];
/* cout<<"maxnum: "<<maxnum<<endl;
cout<<"color: "<<order[curnum]<<endl;
cout<<"maxlen: "<<maxlen<<endl;*/
}
}
printf("%d",maxlen);
return ;
}
pat1045. Favorite Color Stripe (30)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Dojo Style Guide
Contents: General Quick Reference Naming Conventions Specific Naming Conventions Files Variables Lay ...
- 「BZOJ 2440」完全平方数「数论分块」
题意 \(T\)组数据,每次询问第\(k\)个无平方因子的数(\(1\)不算平方因子),\(T\leq 50,k\leq 10^9\) 题解 \(k\)的范围很大,枚举肯定不行,也没什么奇妙性质,于是 ...
- Vue.js 的几点总结Watchers/router key/render
Vue.js 的几点总结,下面就是实战案例,一起来看一下. 第一招:化繁为简的Watchers 场景还原: 1 2 3 4 5 6 7 8 created(){ this.fetchPostLis ...
- 树状数组【bzoj1782】: [Usaco2010 Feb]slowdown 慢慢游
[bzoj1782]: [Usaco2010 Feb]slowdown 慢慢游 Description 每天Farmer John的N头奶牛(1 <= N <= 100000,编号1-N) ...
- 13.Convert BST to Greater Tree(将树转为更大树)
Level: Easy 题目描述: Given a Binary Search Tree (BST), convert it to a Greater Tree such that every k ...
- 基础线程机制--Executor线程池框架
基础线程机制 Executor线程池框架 1.引入Executor的原因 (1)new Thread()的缺点 每次new Thread()耗费性能 调用new Thread()创建的线程 ...
- fatal error C1859: “Release\IWBServer.pch”意外的预编译头错误,只需重新运行编译器就可能修复此问题
解决方案 1. 创建预编译头(/Yc) -- > stdafx.cpp 使用预编译头(/Yu) 2. complie 3. 使用预编译头(/Yu) -- ...
- [转载]什么情况下应该设置 cudnn.benchmark = True?
总的来说,大部分情况下,设置这个 flag 可以让内置的 cuDNN 的 auto-tuner 自动寻找最适合当前配置的高效算法,来达到优化运行效率的问题. 一般来讲,应该遵循以下准则: 如果网络的输 ...
- POJ1056 IMMEDIATE DECODABILITY & POJ3630 Phone List
题目来源:http://poj.org/problem?id=1056 http://poj.org/problem?id=3630 两题非常类似,所以在这里一并做了. 1056题目大意: 如果一 ...
- Tinkphp 教程 一
1项目生成配置php环境变量在控制台进入项目目录,执行php console build --config build.php命令在application目录创建项目目录,把创建好的目录复制到自定义a ...