题解 AT4807 [ABC156C] Rally
题意
有
思路
由于数据范围很小,可以枚举
时间复杂度为
当然有比这个时间复杂度更优的做法,但对于这道题的数据范围,这个时间复杂度是可以接受的。
Code:
#include <bits/stdc++.h>
using namespace std;
int n;
int a[105];
int cnt = 0;
int ans = 1e9 + 5;
int main() {
cin >> n;
for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
for(int i = 0; i <= 102; i++) { //点p的位置
cnt = 0;
for(int j = 1; j <= n; j++) cnt += (a[j] - i) * (a[j] - i); //计算距离和
ans = min(ans, cnt);
}
cout << ans << endl;
return 0;
}