B. Elimination of a Ring

B. Elimination of a Ring

Define a cyclic sequence of size $n$ as an array $s$ of length $n$, in which $s_n$ is adjacent to $s_1$.

Muxii has a ring represented by a cyclic sequence $a$ of size $n$.

However, the ring itself hates equal adjacent elements. So if two adjacent elements in the sequence are equal at any time, one of them will be erased immediately. The sequence doesn't contain equal adjacent elements initially.

Muxii can perform the following operation until the sequence becomes empty:

  • Choose an element in $a$ and erase it.

For example, if ring is $[1, 2, 4, 2, 3, 2]$, and Muxii erases element $4$, then ring would erase one of the elements equal to $2$, and the ring will become $[1, 2, 3, 2]$.

Muxii wants to find the maximum number of operations he could perform.

Note that in a ring of size $1$, its only element isn't considered adjacent to itself (so it's not immediately erased).

Input

Each test contains multiple test cases. The first line contains a single integer $t$ ($1\leq t\leq 100$) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer $n$ ($1\leq n\leq 100$) — the size of the cyclic sequence.

The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq a_i\leq n$) — the sequence itself.

It's guaranteed that $a_i\ne a_{i+1}$ for $1\leq i<n$.

It's guaranteed that $a_n\ne a_1$ when $n>1$.

Output

For each test case, output a single integer — the maximum number of operations Muxii can perform.

Example

input

3
4
1 2 3 2
4
1 2 1 2
1
1

output

4
3
1

Note

In the first test case, you can erase the second element first, then erase the remaining elements one by one in any order. In total, you can perform the operation $4$ times. Note that if you erase the first element first, then the sequence will be turned into $[2,3,2]$ and then immediately become $[2,3]$.

In the second test case, you can erase the first element first, then the sequence becomes $[2,1]$. Then you can erase all remaining elements one by one in any order.

 

解题思路

  比赛的时候看了快两个小时都没写出来,一直在写模拟,优先删除相邻两个元素不相同的元素,结果一直WA,后来看了个样例 1 2 3 1 3 ,模拟的做法得到的答案是$4$,实际上答案是$5$,就知道这种贪心的做法是错的了。现在基本遇到思维题贪心题都做不出来,心态已经炸了。

  结论就是如果序列长度为$1$,那么答案就是$1$;如果序列中只有两种不同的数字,那么答案就是$\frac{n}{2}+1$;如果数字的种类大于等于$3$,那么答案就是$n$。

  如果序列中只有两种不同的数字,那么序列的形式必然是 1 2 1 2 1 2 ... 这种两个数字交替循环的形式,并且长度必然是偶数,这是因为如果是长度是奇数的话那么序列的首尾都是数字$1$,就与定义矛盾了。对于这种形式,由于任意一个位置$i$的相邻两个元素相同(即$a_{i+1} = a_{i-1}$),因此无论我们删掉哪个数字都会因为重新拼接而再删除一个数字。直到最后只有两个不同的数字,此时要进行两次删除操作。因此删除的次数就是$\frac{n}{2}+1$。

  如果数字的种类大于等于$3$,这时关键是要想到如果存在某个数字在序列中只出现一次,那么我们每次都删除这个数字的相邻两个数中的任意一个,直到最后只剩下这个数字。这个比较好证明,因为这个数字在序列中只出现一次,因此如果是删除这个数字的相邻两个数,那么在重新拼接时必然不会发生数字重复的情况。因此最大的答案就为$n$。问题是不一定会存在只出现一次的数字,幸运的是,如果序列中数字的种类大于等于$3$,那么我们一定可以通过删除操作使得某个数字在序列中只出现一次,并且在这些删除操作中不会出现因为拼接而删除数字的情况发生,答案还是$n$。

  这个问题我想了很久,最后还是参考评论区里面的一个证明:

My intuition:

If there are at least $3$ different numbers, there is a segment $'\ldots xyz \ldots'$ where $x$, $y$ and $z$ are all different.

That must be true, otherwise, our ring would look like $xyxyxyxyxyxyxy \ldots$

After getting .$'\ldots xyz \ldots'$, if y appears only once in the entire ring, we can keep removing elements to the right of $y$ until we remove all elements.

If $y$ appears at least $2$ times, we remove $y$ and continue our algorithm, as there are at least $3$ unique numbers left.

  就是说因为数字的种类大于等于$3$,因此序列一定是 ... 1 2 3 ... 这种形式($1$,$2$,$3$就是三种不同类型的数字,当然可能会更多不同类型的数字,但都可以用这种形式来表达),否则就是 1 2 1 2 1 2 ... ,就不可能存在至少$3$种不同类型的数字。如果$2$只出现一次那么就按上面的方法每次删相邻的元素就好了。否则$2$出现超过一次,那么此时我们删除$2$(那么$1$和$3$不同不会再删掉其中一个),由于此时序列中数字的种类仍然大于等于$3$(一开始就假定$2$是只出现一次的数字,但现在$2$出现超过一次,也意味着序列中不存在只出现一次的序列),因此序列还是 ... 1 2 3 ...  这一类型,重复这个做法,直到存在只出现一次的数字为止。

  AC代码如下:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 void solve() {
 5     int n;
 6     scanf("%d", &n);
 7     unordered_set<int> st;
 8     for (int i = 0; i < n; i++) {
 9         int x;
10         scanf("%d", &x);
11         st.insert(x);
12     }
13     if (st.size() == 1 || st.size() > 2) printf("%d\n", n);
14     else printf("%d\n", n / 2 + 1);
15 }
16 
17 int main() {
18     int t;
19     scanf("%d", &t);
20     while (t--) {
21         solve();
22     }
23     
24     return 0;
25 }

 

参考资料

  Pinely Round 1 (Div. 1 + Div. 2) Editorial:https://codeforces.com/blog/entry/109256

posted @ 2022-11-21 16:33  onlyblues  阅读(68)  评论(0编辑  收藏  举报
Web Analytics