题目链接:http://abc043.contest.atcoder.jp/tasks/arc059_a

Time limit : 2sec / Memory limit : 256MB

Score : 200 points

Problem Statement

Evi has N integers a1,a2,..,aN. His objective is to have N equal integers by transforming some of them.

He may transform each integer at most once. Transforming an integer x into another integer y costs him (xy)2 dollars. Even if ai=aj(ij), he has to pay the cost separately for transforming each of them (See Sample 2).

Find the minimum total cost to achieve his objective.

Constraints

  • 1≦N≦100
  • −100≦ai≦100

Input

The input is given from Standard Input in the following format:

N
a1 a2 ... aN

Output

Print the minimum total cost to achieve Evi's objective.


Sample Input 1

Copy
2
4 8

Sample Output 1

Copy
8

Transforming the both into 6s will cost (4−6)2+(8−6)2=8 dollars, which is the minimum.


Sample Input 2

Copy
3
1 1 3

Sample Output 2

Copy
3

Transforming the all into 2s will cost (1−2)2+(1−2)2+(3−2)2=3 dollars. Note that Evi has to pay (1−2)2 dollar separately for transforming each of the two 1s.


Sample Input 3

Copy
3
4 2 5

Sample Output 3

Copy
5

Leaving the 4 as it is and transforming the 2 and the 5 into 4s will achieve the total cost of (2−4)2+(5−4)2=5 dollars, which is the minimum.


Sample Input 4

Copy
4
-100 -100 -100 -100

Sample Output 4

Copy
0

Without transforming anything, Evi's objective is already achieved. Thus, the necessary cost is 0.

题解:数据不大 枚举

 #include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#include <queue>
#include <stack>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
bool cmp(int x,int y)
{
return x>y;
}
const int N=;
const int mod=1e9+;
int a[];
int main()
{
std::ios::sync_with_stdio(false);
int n;
while(cin>>n){
int s=;
for(int i=;i<n;i++)
cin>>a[i];
sort(a,a+n);
if(a[]==a[n-]) cout<<s<<endl;
else{
s=;
int t=,j;
for(int i=a[];i<a[n-];i++){
t=;
for(j=;j<n;j++){
t+=(i-a[j])*(i-a[j]);
}
if(t<s) s=t;
}
cout<<s<<endl;
}
}
return ;
}

いっしょ / Be Together (暴力枚举)的更多相关文章

  1. CodeForces 742B Arpa’s obvious problem and Mehrdad’s terrible solution (暴力枚举)

    题意:求定 n 个数,求有多少对数满足,ai^bi = x. 析:暴力枚举就行,n的复杂度. 代码如下: #pragma comment(linker, "/STACK:1024000000 ...

  2. 2014牡丹江网络赛ZOJPretty Poem(暴力枚举)

    /* 将给定的一个字符串分解成ABABA 或者 ABABCAB的形式! 思路:暴力枚举A, B, C串! */ 1 #include<iostream> #include<cstri ...

  3. HNU 12886 Cracking the Safe(暴力枚举)

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274 解题报告:输入4个数 ...

  4. 51nod 1116 K进制下的大数 (暴力枚举)

    题目链接 题意:中文题. 题解:暴力枚举. #include <iostream> #include <cstring> using namespace std; ; ; ch ...

  5. Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举

    题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...

  6. bzoj 1028 暴力枚举判断

    昨天梦到这道题了,所以一定要A掉(其实梦到了3道,有两道记不清了) 暴力枚举等的是哪张牌,将是哪张牌,然后贪心的判断就行了. 对于一个状态判断是否为胡牌,1-n扫一遍,然后对于每个牌,先mod 3, ...

  7. POJ-3187 Backward Digit Sums (暴力枚举)

    http://poj.org/problem?id=3187 给定一个个数n和sum,让你求原始序列,如果有多个输出字典序最小的. 暴力枚举题,枚举生成的每一个全排列,符合即退出. dfs版: #in ...

  8. hihoCoder #1179 : 永恒游戏 (暴力枚举)

    题意: 给出一个有n个点的无向图,每个点上有石头数个,现在的游戏规则是,设置某个点A的度数为d,如果A点的石子数大于等于d,则可以从A点给每个邻接点发一个石子.如果游戏可以玩10万次以上,输出INF, ...

  9. CCF 201312-4 有趣的数 (数位DP, 状压DP, 组合数学+暴力枚举, 推公式, 矩阵快速幂)

    问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次. 2. 所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前. 3. 最高 ...

  10. hdu 1172 猜数字(暴力枚举)

    题目 这是一道可以暴力枚举的水题. //以下两个都可以ac,其实差不多一样,呵呵 //1: //4 wei shu #include<stdio.h> struct tt { ],b[], ...

随机推荐

  1. 用laravel dingo/api创建产品api

    沿着上一篇来讲,我们来创建一个简单的item产品api,也是用到laravel dingo/api来实现,对dingo/api不熟的朋友可以翻看前面的文章.好,我们随着ytkah一起来创建产品api ...

  2. 帝国cms调用栏目自定义字段(栏目简介)如何操作

    开源的cms就像一个操作系统,可以满足大部分人的需求,如果你想增加一些特殊的功能,那就二次开发呗,就像APP一样.帝国cms默认的栏目是没有调用栏目自定义字段的,我们可以增加一些概述类的文字,让读者对 ...

  3. 容器化 RDS:你须要了解数据是怎样被写&quot;坏&quot;的

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/M2l0ZgSsVc7r69eFdTj/article/details/79877076 容器化 RD ...

  4. android TableLayOut画表格

    <TableRow android:layout_width="match_parent" android:layout_height="wrap_content& ...

  5. 构造器初始化(static)

    package demo; /* * 在类 的内部,变量定义的先后顺序决定了初始化的顺序.即使变量定义散布于方法定义之间, * 它们仍旧会在任何方法(包括构造器)被调用之前得到初始化. */ publ ...

  6. IOP开发数据库--20180105整理

    http://10.110.22.12/cloud-web/#/login/tenant 数据库 代理节点  10.110.22.12      数据库  10.110.22.12    dev/ro ...

  7. (转)JVM内存分配 -Xms128m -Xmx512m -XX:PermSize=128m -XX:MaxPermSize=512m

    在linux环境下配置项目运行环境时,部署的人员都会分配一下内存,以保证程序正常的运行.其实在开发的时候(window系统),就已经涉及到内存分配了,只是这些参数有默认值,因此一直没有去重视它. 以M ...

  8. 读取Request body方法

    一:传统方法 StringBuilder stringBuilder = new StringBuilder(); BufferedReader bufferedReader = null; try ...

  9. 【LeetCode每天一题】Permutations(排列组合)

    Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...

  10. node使用 log4js

    log4js //配置日志的输出级别,共ALL<TRACE<DEBUG<INFO<WARN<ERROR<FATAL<MARK<OFF八个级别,defau ...