题目链接: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. Python3学习之路~5.8 shelve模块

    shelve模块是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式 import shelve import datetime name = [& ...

  2. [py][mx]django的cookie和session操作-7天免登录

    浏览器同源策略(same-origin policy) csrf攻击防御核心点总结 django的cookie和session操作-7天免登录 flask操作cookie&django的see ...

  3. Java通过jxl读取Excel

    package com.hd.all.test.testjava; import java.io.File; import java.io.IOException; import java.util. ...

  4. elasticsearch 处理index 一直INITIALIZING状态

    elasticsearch一个节点异常重启后有一个index恢复的过程中状态一直INITIALIZING 处理方法 PUT index_name/_settings { "index&quo ...

  5. MyBatis SpringMVC映射配置注意

    applicationContext.xml中要配置 <!-- MyBatis 的 sqlSessionFactory --> <bean id="sqlSessionFa ...

  6. 带你零基础学习HTML5

    1个HTML5基础入门教程,4个HTML5小项目教程,带你零基础入门学习HTML5. [HTML5基础入门] 教程将会介绍HTML5中的新特性,包括结构标签.新型表单标签.文件操作.Canvas.本地 ...

  7. hibernate.validator.constraints.NotEmpty校验请求参数报错java.lang.NoClassDefFoundError: javax/el/PropertyNotFoundException

    spring maven项目,使用hibernate validator 注解形式校验客户端的请求参数. hibernate-validator版本:5.0.2.Final validation-ap ...

  8. 启用了不安全的HTTP方法【转】

    安全风险:       可能会在Web 服务器上上载.修改或删除Web 页面.脚本和文件. 可能原因:       Web 服务器或应用程序服务器是以不安全的方式配置的. 修订建议:       如果 ...

  9. css 清除一些默认的设置

    一.input标签的placeholder的设置 input::-webkit-input-placeholder{ /*WebKit browsers*/ //重置样式 } input::-moz- ...

  10. es6generator

    yield语句 由于Generator函数返回的遍历器对象,只有调用next方法才会遍历下一个内部状态,所以其实提供了一种可以暂停执行的函数.yield语句就是暂停标志. yield语句只能用在 Ge ...