题目链接: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. 运行python文件时出错SyntaxError: Non-UTF-8 code starting with '\xb5' in file, but no encoding declared;

    今天ytkah在运行python文件时出现错误,提示如下,很明显这是没有定义python文件编码引起的问题,那么要怎么解决呢?很简单,在文件头部定义一下就可以了. File "hello.p ...

  2. 理解Spark里的闭包

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/rlnLo2pNEfx9c/article/details/80650309 闭包的概念例如以下图: ...

  3. Python-多线程.md

    # 环境 - xubuntu 16.04 - anaconda - pycharm - python3.6 - https://www.cnblogs.com/jokerbj/p/7460260.ht ...

  4. QGL登陆wui 提示not possible

    1: sap 登陆 QGL系统,  提示 2: 需要到该系统下,使用 T-CODE  su01 或者  su3  对LIHANS1 用户角色进行修改. 3:su53  查询 自己操作的 历史纪录和 状 ...

  5. [py]python的time和datetime模块获取星期几

    import time import datetime #今天星期几 today=int(time.strftime("%w")) print today #某个日期星期几 any ...

  6. MySQL数据类型--与MySQL零距离接触2-8查看数据表

    SHOW COLUMNS FROM tb_name 写入列之后,需要写入行,也就是记录:INSERT 插入记录:INSERT [INTO]  tbl_name  [(col_name,...)]  V ...

  7. ROSETTA使用技巧随笔--蛋白蛋白对接

    先写简略版,以后再详细写. 1. 对输入结构进行预处理(refine) $> relax.default.linuxgccrelease -in:file:s input_files/from_ ...

  8. 用int还是用Integer?

    昨天例行code review时大家有讨论到int和Integer的比较和使用. 这里做个整理,发表一下个人的看法.   [int和Integer的区别] int是java提供的8种原始类型之一,ja ...

  9. OEMCC13.2 添加监控目标

    1.需求描述 2.添加数据库目标 2.1 部署AGENT   2.1.1 直接安装方式   2.1.2 离线安装方式   2.1.3 命令行安装方式 2.2 添加集群资源 2.3 添加数据库 3.添加 ...

  10. Faster-rcnn实现目标检测

      Faster-rcnn实现目标检测 前言:本文浅谈目标检测的概念,发展过程以及RCNN系列的发展.为了实现基于Faster-RCNN算法的目标检测,初步了解了RCNN和Fast-RCNN实现目标检 ...