ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Codeforces 1454C Sequence Transformation

2021-03-13 11:02:54  阅读:249  来源: 互联网

标签:sequence int contains Codeforces choose th test 1454C 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 xx that 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 ai≠xai≠x if l≤i≤rl≤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 (1≤t≤2⋅1041≤t≤2⋅104) — the number of test cases. Then tt test cases follow.

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

It is guaranteed that the sum of nn does not exceed 2⋅1052⋅105 (∑n≤2⋅105∑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\n", minn);
37     }
38 
39     return 0;
40 }

 

标签:sequence,int,contains,Codeforces,choose,th,test,1454C,Transformation
来源: https://www.cnblogs.com/sineagle/p/14527904.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有