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
  • 相关阅读:
    CSS:Tutorial three
    CSS:Tutorial two
    CSS:Tutorial one
    JavaScript: 代码简洁之道
    python数据结构:pandas(1)
    python数据结构:numpy
    python函数之format()
    算法(1)
    python基础面试题:(1)
    python学习笔记(13):python并发编程以及系统常用模块
  • 原文地址:https://www.cnblogs.com/iwannabe/p/10559651.html
Copyright © 2011-2022 走看看