[CF2152A]A. Increase or Smash题解
A. Increase or Smash
time limit per test
1 second
memory limit per test
1024 megabytes
Geumjae has an array a consisting of n zeros. His goal is to transform it into a given target array using a minimum number of operations.
He can perform the following two types of operations any number of times, in any order:
- Increase: Choose any positive integer x. Increase all elements of the array a by x. In other words, he chooses a positive integer x, and for each i (1≤i≤n), he replaces ai with ai+x.
- Smash: Set some elements (possibly none or all) of the array a to 0. In other words, for each i (1≤i≤n), he either replaces ai with 0 or leaves it as before.
Given the final target state of the array a, find the minimum total number of operations (both Increase and Smash) Geumjae needs to perform.
It can be shown that for any given final array, a sequence of operations always exists.
讯飞听见 翻译
Geumjae有一个由 n 个零组成的数组 a 。他的目标是使用最少的操作将其转换为给定的目标数组。他可以以任何顺序执行以下两种类型的操作,次数不限:
增加:选择任意正整数 x 。
将数组 a 的所有元素增加 x 。换句话说,他选择一个正整数 x ,并且对于每个 i ( 1≤i≤n ),他用 ai+x 替换 ai 。
2.SMASH:将数组 a 的一些元素(可能没有或全部)设置为 0 。换句话说,对于每个 i ( 1≤i≤n ),他要么用 0 替换 ai ,要么保持原样。给定数组 a 的最终目标状态,求出最小操作总数(增加和粉碎)
金宰需要表演。
可以证明,对于任何给定的最终数组,操作序列总是存在的。
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤1000). The description of the test cases follows.
The first line contains a single integer n (1≤n≤100) — the number of elements in the array a.
The second line contains n integers a1,a2,…,an (1≤ai≤100) — the elements of the target array a.
讯飞听见 翻译
输入
每个测试包含多个测试用例。第一行包含测试用例的数量 t ( 1≤t≤1000 )。测试用例的描述如下。
第一行包含一个整数 n ( 1≤n≤100 )—数组 a 中的元素数。
第二行包含 n 整数 a1,a2,…,an ( 1≤ai≤100 )—目标数组 a 的元素。
Output
For each test case, output a single integer — the minimum number of operations required.
讯飞听见 翻译
输出
对于每个测试用例,输出一个整数——所需的最小操作数。
Example
Input
Copy
3
3
1 1 3
1
100
9
9 9 3 2 4 4 8 5 3
Output
Copy
3
1
11
Note
Explanation of the first test case:
The target array is [1,1,3]. A possible sequence of 3 operations (which is the minimum) is:
- Initially, the array is [0,0,0]. After an Increase operation with x=2, the array becomes [2,2,2].
- Next, after a Smash operation on the first two elements, the array becomes [0,0,2].
- Finally, after an Increase operation with x=1, the array becomes [1,1,3].
We used 2 Increase operations and 1 Smash operation for a total of 3 operations.
Explanation of the second test case:
The target array is [100]. A single Increase operation with x=100 gives the target array.
讯飞听见 翻译
注意
第一个测试用例的解释:
目标数组为 [1,1,3] 。3个操作的可能顺序(最小)为:
最初,数组为 [0,0,0] 。对 x=2 执行增加操作后,数组变为 [2,2,2] 。
2.接下来,在对前两个元素执行smash操作后,数组变为 [0,0,2] 。
3.最后,在对 x=1 进行增加操作后,数组变为 [1,1,3] 。我们使用了 2 增加操作和 1 粉碎操作,总共进行了 3 次操作。
第二个测试用例说明:
目标数组为 [100] 。对 x=100 执行一次增加操作即可得到目标数组。
思路
容易发现和顺序无关,于是排序,贪心,发现答案即为2*种数-1。
代码见下
#include<bits/stdc++.h>
using namespace std;
long long t,n,a[100005],lk=0;
int main(){
cin>>t;
while(t--){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
lk=0;
sort(a+1,a+n+1);
for(int i=1;i<=n;i++){
if(a[i]!=a[i-1]||i==1){
lk++;
}
}
cout<<lk*2-1<<endl;
}
return 0;
}

浙公网安备 33010602011771号