题目链接
- 题意:给你n(1<=n<=10001<=n<=10001<=n<=1000)个数 aia_iai?(1<=ai<=100)1<=a_i<=100)1<=ai?<=100),让你找一个t,使得所有的∣ai?t∣|a_i-t|∣ai??t∣和最小。输出t和这个最小值
- 样例:
//input 输入数据
3
10 1 4
//output 输出数据
3 7
//易知,当t=3是,最小费用是7
- 解题思路:直接暴力枚举t的值即可
- 代码如下:
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
const int inf = 1100;
const int INF = 0x3f3f3f3f;
int s[inf];
int n;
int fun(int m){
int ans=0;for(int i=0;i<n;i++){
if(s[i]==m)continue;ans+=abs(m-s[i])-1;}return ans;
}
int main(){
cin>>n;for(int i=0;i<n;i++)cin>>s[i];int ans1=0,ans2=INF;for(int i=1;i<=100;i++){
int x=fun(i);if(ans2>x){
ans1=i;ans2=x;}}cout<<ans1<<" "<<ans2<<endl;return 0;
}