zoukankan      html  css  js  c++  java
  • Painting the Fence Gym

    There is a beautiful fence near Monocarp's house. The fence consists of nn planks numbered from left to right. The ii -th plank has color aiai .

    Monocarp's father have decided to give his son mm orders. Each order is a color cjcj . After each order Monocarp finds leftmost and rightmost planks currently having color cjcj and repaints all planks between them into color cjcj .

    For example, if, at first, fence looked like (from left to right) [1,2,3,1,4,1,5,6][1,2,3,1,4,1,5,6] , then after fulfilling an order with color 11 fence will look like [1,1,1,1,1,1,5,6][1,1,1,1,1,1,5,6] .

    Assume that Monocarp fulfills all orders in the order they come, one by one.

    Note that if current order is about color xx and there is no more than one plank in the fence having color xx , then Monocarp doesn't repaint anything, so he can skip this order and skip to the next one.

    Find out the color of each plank after Monocarp has done all the given orders.

    Input

    The first line contains one integer nn (1n3105)(1≤n≤3⋅105) — the number of planks in the fence.

    The second line contains nn space-separated integers a1,a2,,ana1,a2,…,an (1ai3105)(1≤ai≤3⋅105) , where aiai is the initial color of the ii -th plank.

    The third line contains one integer mm (1m3105)(1≤m≤3⋅105) — the number of orders.

    The fourth line contains mm space-separated integers c1,c2,,cmc1,c2,…,cm (1cj3105)(1≤cj≤3⋅105) , where cjcj is the color of the jj -th order.

    Output

    Print nn space-separated integers — the colors of planks in the fence after processing all mm orders.

    Examples

    Input
    4
    1 2 1 2
    2
    2 1
    Output
    1 2 2 2 
    Input
    8
    7 1 7 1 23 9 23 1
    4
    23 4 7 1
    Output
    7 7 7 1 1 1 1 1 

    Note

    In the first example initial appearance of the fence is [1,2,1,2][1,2,1,2] . After the first order (color 22 ) fence will look like [1,2,2,2][1,2,2,2] . After the second order (color 11 ) appearance of the fence will not change.

    In the second example initial appearance of the fence is [7,1,7,1,23,9,23,1][7,1,7,1,23,9,23,1] . After the first order (color 2323 ) the fence will look like [7,1,7,1,23,23,23,1][7,1,7,1,23,23,23,1] . After the second order (color 44 ) appearance of the fence will not change. After the third order (color 77 ) the fence will look like [7,7,7,1,23,23,23,1][7,7,7,1,23,23,23,1] . After the fourth order (color 11 ) the fence will look like [7,7,7,1,1,1,1,1][7,7,7,1,1,1,1,1] .

    题意:给你n个位置,每个位置都有颜色,然后m个操作,每个操作给你一种颜色,然你从该颜色的出现的最左端涂色到最右端,最后输出涂色情况

    思路:记录下每个颜色出现的位置,然后按照操作进行涂色,涂色的时候顺便清楚这一区间内所有颜色的涂色情况。用set实现是不错的,毕竟自带erase,但是要注意不要每次涂色都直接去数组上更改,可以先将最后答案每个颜色的最左端和最右端求出来再进行涂色(做的时候思路没错,实现起来太沙雕了,tle到崩溃,借鉴(抄袭)网上题解代码)

    #include<bits/stdc++.h>
    using namespace std;
    
    const int maxn = 3e5+5;
    bool vis[maxn];
    set<int>s[maxn];
    int n,m;
    int ans[maxn];
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&ans[i]);
            s[ans[i]].insert(i);
        }
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
        {
            int tmp;
            scanf("%d",&tmp);
            if(s[tmp].size() < 2 || vis[tmp])
            {
                vis[tmp] = 1;
                continue;
            }
            int l = *(s[tmp].begin());
            int r = *(s[tmp].rbegin());
            for(int j=l+1;j<r;j++)
            {
                s[ans[j]].erase(j);
                if(vis[ans[j]] && s[ans[j]].size() >= 1) ///如果涂了色,那么就不必一个个向后erase,直接跳至该色右端点,再erase掉该端点就可,有效防止tle
                {
                    j = *(s[ans[j]].begin());
                    s[ans[j]].erase(j);
                }
            }
            vis[tmp] = 1;
        }
        for(int i=1;i<maxn;i++)
        {
            if(vis[i] && s[i].size()>1)
            {
                int l = *(s[i].begin());
                int r = *(s[i].rbegin());
                for(int j=l;j<=r;j++)ans[j] = i;
            }
        }
        int tmp = 0;
        for(int i=1;i<=n;i++)
        {
            printf(i != n?"%d ":"%d
    ",ans[i]);
        }
    }
    View Code
  • 相关阅读:
    彻底领悟javascript中的exec与match方法
    javascript doT 使用
    pluginstorage 插件
    html5离线储存,application cache,manifest使用体验
    window.location.hash属性介绍 ajax后退按钮失效问题
    控制textarea光标移到末尾
    webkitanylink 谷歌浏览器CSS之A:HOVER
    正则表达式详细参考文档
    复杂应用的 CSS 性能分析和优化建议
    seaJs api 帮助文档
  • 原文地址:https://www.cnblogs.com/iwannabe/p/10559651.html
Copyright © 2011-2022 走看看