A. Salem and Sticks
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Salem gave you nn sticks with integer positive lengths a1,a2,…,ana1,a2,…,an.

For every stick, you can change its length to any other positive integer length (that is, either shrink or stretch it). The cost of changing the stick's length from aa to bb is |a−b||a−b|, where |x||x| means the absolute value of xx.

A stick length aiai is called almost good for some integer tt if |ai−t|≤1|ai−t|≤1.

Salem asks you to change the lengths of some sticks (possibly all or none), such that all sticks' lengths are almost good for some positive integer tt and the total cost of changing is minimum possible. The value of tt is not fixed in advance and you can choose it as any positive integer.

As an answer, print the value of tt and the minimum cost. If there are multiple optimal choices for tt, print any of them.

Input

The first line contains a single integer nn (1≤n≤10001≤n≤1000) — the number of sticks.

The second line contains nn integers aiai (1≤ai≤1001≤ai≤100) — the lengths of the sticks.

Output

Print the value of tt and the minimum possible cost. If there are multiple optimal choices for tt, print any of them.

Examples
input

Copy
3
10 1 4
output

Copy
3 7
input

Copy
5
1 1 2 2 3
output

Copy
2 0
Note

In the first example, we can change 11 into 22 and 1010 into 44 with cost |1−2|+|10−4|=1+6=7|1−2|+|10−4|=1+6=7 and the resulting lengths [2,4,4][2,4,4]are almost good for t=3t=3.

In the second example, the sticks lengths are already almost good for t=2t=2, so we don't have to do anything.

题意:给你一个含有N个数的数组,让你找一个数x,使x的数组求值消耗的能量尽量小。

数组对于x的求值是把数组的每一个数削成| x-a[i] | <=1 ,并且a[i]数值变化1就要消耗1能量。

思路:

数据范围很小,直接暴力从1到max ( a[i] ) +2 试一试,找出那个最小的就行了,

细节看代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== "<<x<<" =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
int a[maxn];
int ans=inf;
int cnt=inf;
int tt;
int tcnt;
int main()
{
gbtb;
cin>>n;
repd(i,,n)
{
cin>>a[i];
}
repd(i,,)
{
tt=i;
tcnt=;
repd(j,,n)
{
if(abs(tt-a[j])<=)
{ }else
{
tcnt+=abs(tt-a[j])-;
}
}
if(tcnt<cnt)
{
cnt=tcnt;
ans=tt;
}
}
cout<<ans<<" "<<cnt;
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}

Codeforces Round #533 (Div. 2) A. Salem and Sticks(暴力)的更多相关文章

  1. Codeforces Round #533(Div. 2) A.Salem and Sticks

    链接:https://codeforces.com/contest/1105/problem/A 题意: 给n个数,找到一个数t使i(1-n)∑|ai-t| 最小. ai-t 差距1 以内都满足 思路 ...

  2. Codeforces Round #533 (Div. 2) A. Salem and Sticks(枚举)

    #include <bits/stdc++.h> using namespace std; int main() { int n;cin>>n; int a[n];for(in ...

  3. Codeforces Round #533 (Div. 2)题解

    link orz olinr AK Codeforces Round #533 (Div. 2) 中文水平和英文水平都太渣..翻译不准确见谅 T1.给定n<=1000个整数,你需要钦定一个值t, ...

  4. Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心

    Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  5. 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks

    题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...

  6. Codeforces Round #297 (Div. 2)D. Arthur and Walls 暴力搜索

    Codeforces Round #297 (Div. 2)D. Arthur and Walls Time Limit: 2 Sec  Memory Limit: 512 MBSubmit: xxx ...

  7. Codeforces Round #533 (Div. 2) Solution

    A. Salem and Sticks 签. #include <bits/stdc++.h> using namespace std; #define N 1010 int n, a[N ...

  8. Codeforces Round #533 (Div. 2) 部分题解A~D

    A. Salem and Sticks 题目描述 Salem gave you n n n sticks with integer positive lengths a1,a2,…,an a_1, a ...

  9. Codeforces Round #533 (Div. 2) C.思维dp D. 多源BFS

    题目链接:https://codeforces.com/contest/1105 C. Ayoub and Lost Array 题目大意:一个长度为n的数组,数组的元素都在[L,R]之间,并且数组全 ...

随机推荐

  1. ASP.NET Core 1.0、ASP.NET MVC Core 1.0和Entity Framework Core 1.0

    ASP.NET 5.0 将改名为 ASP.NET Core 1.0 ASP.NET MVC 6  将改名为 ASP.NET MVC Core 1.0 Entity Framework 7.0    将 ...

  2. 洗礼灵魂,修炼python(78)--全栈项目实战篇(6)—— 多级目录菜单之地址管理系统

    相信各位都在在网上买过东西吧?那么今天我的主题就是写个在线购物系统,是不可能的,哈哈(后期确实有这个项目),那么购物都填写过快递地址吧?然后网上查个地址都有地址管理吧? 要求: 1.打印出省.市.县等 ...

  3. DVWA v1.9 新手指南

    DVWA简介 DVWA(Damn Vulnerable Web Application)是一个用来进行安全脆弱性鉴定的PHP/MySQL Web应用,旨在为安全专业人员测试自己的专业技能和工具提供合法 ...

  4. LeetCode算法题-Single Number(Java实现)

    这是悦乐书的第175次更新,第177篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第34题(顺位题号是136).给定一个非空的整数数组,除了一个元素外,每个元素都会出现两 ...

  5. 聚类——GAKFCM

    聚类——GAKFCM 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 参考文献:黄白梅. 基于GA优化的核模糊C均值聚类算法的研究[D]. 武汉科技大学 ...

  6. Java多线程(三)如何创建线程

    点我跳过黑哥的卑鄙广告行为,进入正文. Java多线程系列更新中~ 正式篇: Java多线程(一) 什么是线程 Java多线程(二)关于多线程的CPU密集型和IO密集型这件事 Java多线程(三)如何 ...

  7. Node爬虫之初体验

    记得之前就听说过爬虫,个人初步理解就是从网页中抓取一些有用的数据,存储到本地,今天就当是小牛试刀,拿来溜溜...... 实现需求: 抓取课程数据,输入url后并在浏览器端以一定的数据格式显示出来(如下 ...

  8. ES5-ES6-ES7_严格模式

    运行模式 正常(混杂)模式与严格模式,除了正常运行模式(混杂模式),ES5添加了第二种运行模式:"严格模式"(strict mode) 顾名思义,这种模式使得Javascript在 ...

  9. bs4 解析 以及用法

    bs4解析 bs4: 环境安装: lxml bs4 bs4编码流程: 1.实例化一个bs4对象,且将页面源码数据加载到该对象中 2.bs相关的方法或者属性实现标签定位 3.取文本或者取属性 bs的属性 ...

  10. UVA1533-Moving Pegs(BFS+状态压缩)

    Problem UVA1533-Moving Pegs Accept:106  Submit:375 Time Limit: 3000 mSec  Problem Description  Input ...