码迷,mamicode.com
首页 > 其他好文 > 详细

USACO Ski Course Design 暴力

时间:2015-01-27 21:45:46      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:

从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;
}

 

USACO Ski Course Design 暴力

标签:

原文地址:http://www.cnblogs.com/wushuaiyi/p/4253914.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!