AcWing 3809. 修改数组
4 分钟能读完
题目描述
给定一个长度为
你可以任意改变其中任意元素的值。
但是,改变后的元素的值仍需是正整数。
将一个元素的值从
对于一个正整数
现在,请你指定一个正整数
指定的
请你合理选择正整数
输入格式
第一行包含整数
每组数据第一行包含整数
第二行包含
输出格式
每组数据输出一行结果,首先输出你选择的正整数
如果
数据范围
样例
输入样例:
2
3
10 1 4
5
1 1 2 2 3
输出样例:
3 7
2 0
思路
要让所有元素与
由于
代码
C++
#include <iostream>
using namespace std;
const int N = 1005;
int a[N];
int main()
{
int T;
cin >> T;
while ( T-- )
{
int n;
scanf("%d", &n);
for ( int i = 0; i < n; i ++ ) scanf("%d", &a[i]);
int res = 0, cnt = 1e9;
for ( int t = 1; t <= 100; t ++ )
{
int k = 0;
for ( int i = 0; i < n; i ++ )
k += (abs(a[i] - t) > 1 ? abs(a[i] - t) - 1 : 0);
if ( k < cnt ) res = t, cnt = k;
}
printf("%d %d\n", res, cnt);
}
}
Java
import java.util.*;
public class Main
{
static Scanner in = new Scanner(System.in);
public static void main(String args[])
{
int T = in.nextInt();
while ( T -- > 0 )
{
int n = in.nextInt();
int a[] = new int[n];
for ( int i = 0; i < n; i ++ ) a[i] = in.nextInt();
int res = 0, cnt = (int) 1e9;
for ( int t = 1; t <= 100; t ++ )
{
int k = 0;
for ( int x : a )
k += Math.abs(x - t) > 1 ? Math.abs(x - t) - 1 : 0;
if ( k < cnt )
{
res = t;
cnt = k;
}
}
System.out.printf("%d %d\n", res, cnt);
}
}
}
这篇在全部 101 篇里的位置: