从Min到Max范围内暴力一下即可。
/* ID: wushuai2 PROG: skidesign LANG: C++ */ //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <fstream> #include <cstring> #include <cmath> #include <stack> #include <string> #include <map> #include <set> #include <list> #include <queue> #include <vector> #include <algorithm> #define Max(a,b) (((a) > (b)) ? (a) : (b)) #define Min(a,b) (((a) < (b)) ? (a) : (b)) #define Abs(x) (((x) > 0) ? (x) : (-(x))) #define MOD 1000000007 #define pi acos(-1.0) using namespace std; typedef long long ll ; typedef unsigned long long ull ; typedef unsigned int uint ; typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;} template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e-7 ; const int M = 200000 ; const ll P = 10000000097ll ; const int INF = 0x3f3f3f3f ; const int MAX_N = 20 ; int a[1011]; int main() { ofstream fout ("skidesign.out"); ifstream fin ("skidesign.in"); int i, j, k, t, n, m, s, c, w, q; fin >> n; int Min = INF, Max = -INF; int ans = INF; for(i = 0; i < n; ++i){ fin >> a[i]; checkmax(Max, a[i]); checkmin(Min, a[i]); } for(int low = Min; low < Max; ++low){ for(int high = low + 1; high <= Max; ++high){ if(Abs(high - low) <= 17){ int cur = 0; for(i = 0; i < n; ++i){ if(a[i] < low){ cur += (low - a[i]) * (low - a[i]); } else if(a[i] > high){ cur += (a[i] - high) * (a[i] - high); } } checkmin(ans, cur); } } } fout << ans << endl; fin.close(); fout.close(); return 0; }