• wqy的ACM赛H淮南子


    把题目给的式子展开,发现是一组二次函数.
    直接对称轴求最小值即可.

    #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
    " , tmp ) ;
        rep ( i , 1 , n ) ans += ( ( v[i] - tmp ) * ( v[i] - tmp ) ) ;
        printf ("%lld
    " , ans ) ;
        system ("pause") ; return 0 ;
    }
    
  • 相关阅读:
    扫雷游戏
    打地鼠Demo
    Game2048
    蛇形矩阵
    约瑟夫环
    二分法查找
    动态规划之防卫导弹
    动态规划之0-1背包问题
    回溯算法之火力网
    回溯算法之8皇后问题
  • 原文地址:https://www.cnblogs.com/Equinox-Flower/p/11650101.html
Copyright © 2020-2023  润新知