把题目给的式子展开,发现是一组二次函数.
直接对称轴求最小值即可.
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#define MEM(x,y) memset ( x , y , sizeof ( x ) )
#define rep(i,a,b) for (int i = (a) ; i <= (b) ; ++ i)
#define per(i,a,b) for (int i = (a) ; i >= (b) ; -- i)
#define pii pair < int , int >
#define one first
#define two second
#define rint read<int>
#define int long long
#define pb push_back
#define db double
using std::queue ;
using std::set ;
using std::pair ;
using std::max ;
using std::min ;
using std::priority_queue ;
using std::vector ;
using std::swap ;
using std::sort ;
using std::unique ;
using std::greater ;
template < class T >
inline T read () {
T x = 0 , f = 1 ; char ch = getchar () ;
while ( ch < '0' || ch > '9' ) {
if ( ch == '-' ) f = - 1 ;
ch = getchar () ;
}
while ( ch >= '0' && ch <= '9' ) {
x = ( x << 3 ) + ( x << 1 ) + ( ch - 48 ) ;
ch = getchar () ;
}
return f * x ;
}
const int N = 1e5 + 100 ;
int n , v[N] , ans ;
double sum ;
signed main (int argc , char * argv[]) {
n = rint () ;
rep ( i , 1 , n ) v[i] = rint () , sum += v[i] ;
db d = sum / (db) n ;
int tmp = d < 0.0 ? (db)d - 0.5 : (db)d + 0.5 ;
printf ("%lld\n" , tmp ) ;
rep ( i , 1 , n ) ans += ( ( v[i] - tmp ) * ( v[i] - tmp ) ) ;
printf ("%lld\n" , ans ) ;
system ("pause") ; return 0 ;
}
原文地址:https://www.cnblogs.com/Equinox-Flower/p/11650101.html
时间: 2024-10-12 07:48:50