• 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 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
    

    把喜欢的都标记为1,然后裁剪时,只把喜欢的加入序列,对于第i个,找到前面最近的且不是排在他后边的来更新当前的最大长度。所以一开始要标记一下check[a][b]如果是1,表示a在b前,如果是-1,表示a在b后,如果是0就是相等的。
    代码:
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <vector>
    using namespace std;
    int n,m,l,like_order[201],test[10001];
    int check[201][201],iflike[201],dp[10001],ma;
    int main() {
        scanf("%d",&n);
        scanf("%d",&m);
        for(int i = 0;i < m;i ++) {
            scanf("%d",&like_order[i]);
            iflike[like_order[i]] = 1;
            for(int j = 0;j < i;j ++) {///标记前后
                check[like_order[j]][like_order[i]] = 1;
                check[like_order[i]][like_order[j]] = -1;
            }
        }
        scanf("%d",&l);
        for(int i = 0;i < l;i ++) {
            scanf("%d",&test[i]);
            if(!iflike[test[i]]) {///如果不是喜欢的直接过了,并且不保存,如果用vector,就直接不加入序列
                i --;
                l --;
                continue;
            }
            int k = i - 1;
            while(k >= 0 && check[test[k]][test[i]] == -1) {
                k --;
            }///只要不是排在后边的  都可以放在前边
            dp[i] ++;///自己本身长度为1
            if(k >= 0)dp[i] += dp[k];///如果 存在就加上他的长度
            ma = max(ma,dp[i]);///更新最大
        }
        printf("%d",ma);
    }

    这道题如果标记喜欢的颜色的位置,就是求最长非递减子序列。

    代码:

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <vector>
    using namespace std;
    int n,m,l,pos[201];
    int dp[10001],d,c;
    int main() {
        scanf("%d",&n);
        scanf("%d",&m);
        for(int i = 1;i <= m;i ++) {
            scanf("%d",&d);
            pos[d] = i;
        }
        scanf("%d",&l);
        for(int i = 0;i < l;i ++) {
            scanf("%d",&d);
            if(!pos[d])continue;
            if(!c || pos[dp[c - 1]] <= pos[d])dp[c ++] = d;
            else {
                int l = 0,r = c - 1,mid;
                while(l < r) {
                    mid = (l + r) / 2;
                    if(pos[dp[mid]] <= pos[d])l = mid + 1;
                    else r = mid;
                }
                dp[l] = d;
            }
        }
        printf("%d",c);
    }
  • 相关阅读:
    Atitit 图像处理类库 halcon11  安装与环境搭建attilax总结
    Atitit  undac网络设备管理法案 (路由器 交换机等)    法案编号USRr101510
    Atitit 图像处理 halcon类库的使用  范例边缘检测 attilax总结
    Atitit Seed-Filling种子填充算法attilax总结
    Atitti 图像处理 特征提取的科技树 attilax总结
    Atitit 软件体系的进化,是否需要一个处理中心
    Atitit 项目的主体设计与结构文档 v5
    Atitti 过程导向 vs 结果导向 attilax的策略
    Atitti 过程导向 vs 结果导向 attlax的策
    Atitit 版本管理----分支管理
  • 原文地址:https://www.cnblogs.com/8023spz/p/9159099.html
Copyright © 2020-2023  润新知