zoukankan      html  css  js  c++  java
  • Codeforces 1454C Sequence Transformation

    C. Sequence Transformation
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given a sequence aa, initially consisting of nn integers.

    You want to transform this sequence so that all elements in it are equal (i. e. it contains several occurrences of the same element).

    To achieve this, you choose some integer xthat occurs at least once in aa, and then perform the following operation any number of times (possibly zero): choose some segment [l,r][l,r] of the sequence and remove it. But there is one exception: you are not allowed to choose a segment that contains xx. More formally, you choose some contiguous subsequence [al,al+1,,ar][al,al+1,…,ar] such that aixai≠x if lirl≤i≤r, and remove it. After removal, the numbering of elements to the right of the removed segment changes: the element that was the (r+1)(r+1)-th is now ll-th, the element that was (r+2)(r+2)-th is now (l+1)(l+1)-th, and so on (i. e. the remaining sequence just collapses).

    Note that you can not change xx after you chose it.

    For example, suppose n=6n=6, a=[1,3,2,4,1,2]a=[1,3,2,4,1,2]. Then one of the ways to transform it in two operations is to choose x=1x=1, then:

    1. choose l=2l=2, r=4r=4, so the resulting sequence is a=[1,1,2]a=[1,1,2];
    2. choose l=3l=3, r=3r=3, so the resulting sequence is a=[1,1]a=[1,1].

    Note that choosing xx is not an operation. Also, note that you can not remove any occurrence of xx.

    Your task is to find the minimum number of operations required to transform the sequence in a way described above.

    You have to answer tt independent test cases.

    Input

    The first line of the input contains one integer tt (1t21041≤t≤2⋅104) — the number of test cases. Then tt test cases follow.

    The first line of the test case contains one integer nn (1n21051≤n≤2⋅105) — the number of elements in aa. The second line of the test case contains nn integers a1,a2,,ana1,a2,…,an (1ain1≤ai≤n), where aiai is the ii-th element of aa.

    It is guaranteed that the sum of nn does not exceed 21052⋅105 (n2105∑n≤2⋅105).

    Output

    For each test case, print the answer — the minimum number of operations required to transform the given sequence in a way described in the problem statement. It can be proven that it is always possible to perform a finite sequence of operations so the sequence is transformed in the required way.

    Example
    input
    Copy
    5
    3
    1 1 1
    5
    1 2 3 4 5
    5
    1 2 3 2 1
    7
    1 2 3 1 2 3 1
    11
    2 2 1 2 3 2 1 2 3 1 2
    
    output
    Copy
    0
    1
    1
    2
    3
     1 //2021-03-13 10:11:14
     2 #include <iostream>
     3 #include <cstdio>
     4 #include <cstring>
     5 using namespace std;
     6 
     7 const int N = 2e5+10;
     8 int T, n;
     9 int vis[N];
    10 int a[N];
    11 
    12 
    13 int main(){
    14     scanf("%d", &T);
    15     while(T--){
    16         scanf("%d", &n);
    17         memset(vis, 0, sizeof(vis));
    18         int tot = 0;
    19         for(int i = 1; i <= n; i++){
    20             int x;
    21             scanf("%d", &x);
    22             if(a[tot] != x){
    23                 a[++tot] = x;
    24                 vis[x]++;
    25             }
    26         }
    27         int minn = 0x3f3f3f3f;
    28         for(int i = 1; i <= n; i++){
    29             if(vis[i]){
    30                 int res = vis[i] + 1;
    31                 if(a[1] == i) res--;
    32                 if(a[tot] == i) res--;
    33                 minn = min(res, minn);
    34             }
    35         }
    36         printf("%d
    ", minn);
    37     }
    38 
    39     return 0;
    40 }

     用stl里面的去重unique()

    //2021-03-13 10:11:14
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    const int N = 2e5+10;
    int T, n;
    int vis[N];
    int a[N];
    
    
    int main(){
        scanf("%d", &T);
        while(T--){
            scanf("%d", &n);
            memset(vis, 0, sizeof(vis));
            for(int i = 1; i <= n; i++){
                scanf("%d", &a[i]);
            }
            int nn = unique(a+1, a+n+1) - (a+1);
            for(int i = 1; i <= nn; i++) vis[a[i]]++;
             int minn = 0x3f3f3f3f;
            for(int i = 1; i <= n; i++){
                if(vis[i]){
                    int res = vis[i] + 1;
                    if(a[1] == i) res--;
                    if(a[nn] == i) res--;
                    minn = min(res, minn);
                }
            }
            printf("%d
    ", minn);
        }
    
        return 0;
    }
  • 相关阅读:
    用webclient.DownloadFile下载exe文件时大小为0
    C# ,asp.net 获取当前,相对,绝对路径(转)
    c#读取进程列表判断程序是否已经启动(转)
    如何提升页面渲染效率
    前端知识点总结——VUE
    ie7ajax 跨域 no transport 解决办法
    酷炫网址
    框架学习官网
    JavaScript数组方法大全(推荐)
    rem是如何实现自适应布局的
  • 原文地址:https://www.cnblogs.com/sineagle/p/14527904.html
Copyright © 2011-2022 走看看