题解:Codeforces Round 967 (Div. 2) A [暴力/贪心]

A. Make All Equal

time limit per test: 1 second

memory limit per test: 256 megabytes

input: standard input

output: standard output

You are given a cyclic array a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an.

You can perform the following operation on a a a at most n − 1 n - 1 n1 times:

  • Let m m m be the current size of a a a, you can choose any two adjacent elements where the previous one is no greater than the latter one (In particular, a m a_m am and a 1 a_1 a1 are adjacent and a m a_m am is the previous one), and delete exactly one of them. In other words, choose an integer i i i ( 1 ≤ i ≤ m 1 \leq i \leq m 1im) where a i ≤ a ( i   m o d   m ) + 1 a_i \leq a_{(i \bmod m) + 1} aia(imodm)+1 holds, and delete exactly one of a i a_i ai or a ( i   m o d   m ) + 1 a_{(i \bmod m) + 1} a(imodm)+1 from a a a.

Your goal is to find the minimum number of operations needed to make all elements in a a a equal.

给你一个循环数组 a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an

你最多可以对 a a a 执行 n − 1 n - 1 n1 次以下操作:

  • 假设 m m m a a a 的当前大小,你可以选择任意两个相邻的元素,其中前一个元素不大于后一个元素(特别是 a m a_m am a 1 a_1 a1 是相邻的,而 a m a_m am 是前一个元素),并恰好删除其中一个元素。换句话说,选择一个 a i ≤ a ( i   m o d   m ) + 1 a_i \leq a_{(i \bmod m) + 1} aia(imodm)+1 成立的整数 i i i 1 ≤ i ≤ m 1 \leq i \leq m 1im ),并从 a a a 中恰好删除 a i a_i ai a ( i   m o d   m ) + 1 a_{(i \bmod m) + 1} a(imodm)+1 中的一个。

你的目标是找出使 a a a 中所有元素相等所需的最少运算次数。

Input

Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 500 1 \le t \le 500 1t500). The description of the test cases follows.

The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 100 1 \le n \le 100 1n100) — the length of the array a a a.

The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 1 ≤ a i ≤ n 1 \le a_i \le n 1ain) — the elements of array a a a.

输入

每个测试包含多个测试用例。第一行包含测试用例的数量 t t t ( 1 ≤ t ≤ 500 1 \le t \le 500 1t500 )。测试用例说明如下。

每个测试用例的第一行包含一个整数 n n n ( 1 ≤ n ≤ 100 1 \le n \le 100 1n100 ) - 数组的长度 a a a

每个测试用例的第二行包含 n n n 个整数 a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 1 ≤ a i ≤ n 1 \le a_i \le n 1ain ) - 数组 a a a 的元素。

Output

For each test case, output a single line containing an integer: the minimum number of operations needed to make all elements in a a a equal.

输出

对于每个测试用例,输出一行包含一个整数:使 a a a 中所有元素相等所需的最少操作数。

Example

Input
7
1
1
3
1 2 3
3
1 2 2
5
5 4 3 2 1
6
1 1 2 2 3 3
8
8 7 6 3 8 7 6 3
6
1 1 4 5 1 4
Output
0
2
1
4
4
6
3

Note

In the first test case, there is only one element in a a a, so we can’t do any operation.

In the second test case, we can perform the following operations to make all elements in a a a equal:

  • choose i = 2 i = 2 i=2, delete a 3 a_3 a3, then a a a would become [ 1 , 2 ] [1, 2] [1,2].
  • choose i = 1 i = 1 i=1, delete a 1 a_1 a1, then a a a would become [ 2 ] [2] [2].

It can be proven that we can’t make all elements in a a a equal using fewer than 2 2 2 operations, so the answer is 2 2 2.

在第一个测试用例中, a a a 中只有一个元素,因此我们无法进行任何操作。

在第二个测试用例中,我们可以执行以下操作,使 a a a 中的所有元素相等:

  • 选择 i = 2 i = 2 i=2 ,删除 a 3 a_3 a3 ,那么 a a a 将变为 [ 1 , 2 ] [1, 2] [1,2]
  • 选择 i = 1 i = 1 i=1 ,删除 a 1 a_1 a1 ,则 a a a 将变为 [ 2 ] [2] [2]

可以证明,使用少于 2 2 2 的运算无法使 a a a 中的所有元素相等,因此答案是 2 2 2

题意

有一个循环数组 a a a
你每次可以操作:

对数组中相邻两个不同的数字删去任意一个(首尾两个也算相邻)
你需要不断操作,直到数组中只有一种元素时停止
问:你至少需要操作多少次

题解

只需要用一个map存储某种数字出现的个数
然后最后的数组就是全部那种数字了
所以只需要输出
数组总数 − 出现最多数字的个数 数组总数 - 出现最多数字的个数 数组总数出现最多数字的个数
即可

代码

#include <bits/stdc++.h>
#define int long long
#define INF 0x3f3f3f3f
#define all(x) x.begin(),x.end()

using i64 = long long;
const int N = 2e5 + 10;
int t = 1;
int a[N];

void solve() {
	std::map<int,int> mp;
	int n;
	std::cin >> n;
	int max = 0;
	for(int i = 0 ; i < n ; i ++) {
		int num;
		std::cin >> num;
		mp[num]++;
		max = std::max(max,mp[num]);
	}
	std::cout << n - max << "\n";
}

signed main(){
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);

	std::cin >> t;
	while(t--){
		solve();
	}
	return 0;
}

有什么建议或者意见的欢迎在评论区留言!

转载自博客https://www.cnblogs.com/jiejiejiang2004/p/18371667
博主已同意,我就是博主

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值