题目链接: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. SSAS下玩转PowerShell

     操作SSAS数据库的方法有非常多,是否有一种能够方法能够通过脚本自己主动去做这些事呢,比方处理分区,创建备份以及监视SSAS的执行状况.   原文地址: http://www.mssqltips ...

  2. awk命令的基本使用

    命令主要用法 -格式1:前置命令 | awk [选项] '[条件]{编辑指令}' -格式2:awk [选项] '[条件]{编辑指令}' filename 常用命令选项 -F:指定分隔符,可省略(默认空 ...

  3. 20170724 Airflow官网资料学习

    -- 1  Apache Airflow 文档 AirFlow 对编程人员来讲就是一个平台,用于进行日程安排和监控.但是还在卵化期,严格来说,不是一个完整的成品.

  4. 封装||property

    封装 封装:主要是指在类的定义阶段将,以__开头的属性名进行变形..例如:__name ==> _People__name 封装的主要特点: 1.在类外部无法直接__name,想要在外部调用可以 ...

  5. [django]modelform实现的多文件上传

    实现效果 代码 models.py from django.db import models import uuid class UUIDTools(object): ""&quo ...

  6. char varchar

    对于字符类型的有:char:固定长度,存储ANSI字符,不足的补英文半角空格.nchar:固定长度,存储Unicode字符,不足的补英文半角空格varchar:可变长度,存储ANSI字符,根据数据长度 ...

  7. Sql注入基础原理介绍

    说明:文章所有内容均截选自实验楼教程[Sql注入基础原理介绍]~ 实验原理 Sql 注入攻击是通过将恶意的 Sql 查询或添加语句插入到应用的输入参数中,再在后台 Sql 服务器上解析执行进行的攻击, ...

  8. [Java] public, private, final and basic rules for naming.

    1. Access: public, private, protected public: Any other class can access a public field or method. ( ...

  9. MVC 下 JsonResult 的使用方法(JsonRequestBehavior.AllowGet)

    MVC 默认 Request 方式为 Post. actionpublic JsonResult GetPersonInfo(){var person = new{Name = "张三&qu ...

  10. Github Pages 搭建网站

    参考网站: https://pages.github.com/ http://gitbeijing.com/pages.html 搬进github:http://gitbeijing.com