Codeforces-Salem and Sticks(枚举+思维)
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
3
10 1 4
Output
3 7
Input
5
1 1 2 2 3
Output
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.
解法:枚举1-100的数,因为a[i]在1-100的范围内
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int a[1005];
int main()
{
int n;
cin>>n;
for(int t=0;t<n;t++)
{
scanf("%d",&a[t]);
}
int maxn=9999999;
int k;
for(int t=1;t<=100;t++)
{
int sum=0;
for(int j=0;j<n;j++)
{
sum+=min(fabs(a[j]-t+1),min(fabs(a[j]-t),fabs(a[j]-t-1)));
}
if(sum<maxn)
{
maxn=sum;
k=t;
}
}
cout<<k<<" "<<maxn<<endl;
return 0;
}
Codeforces-Salem and Sticks(枚举+思维)的更多相关文章
- Codeforces Gym101097I:Sticks (思维)
http://codeforces.com/gym/101097/attachments 题意:现在有k种颜色的木棍,每种颜色有ni根木棍,每根木棍有一个长度,问是否有三根木棍可以组成三角形,并且这三 ...
- Codeforces Round #533 (Div. 2) A. Salem and Sticks(暴力)
A. Salem and Sticks time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 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 ...
- 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 以内都满足 思路 ...
- codeforces 895A Pizza Separation 枚举
codeforces 895A Pizza Separation 题目大意: 分成两大部分,使得这两部分的差值最小(注意是圆形,首尾相连) 思路: 分割出来的部分是连续的,开二倍枚举. 注意不要看成0 ...
- Educational Codeforces Round 61 C 枚举 + 差分前缀和
https://codeforces.com/contest/1132/problem/C 枚举 + 差分前缀和 题意 有一段[1,n]的线段,有q个区间,选择其中q-2个区间,使得覆盖线段上的点最多 ...
- Codeforces 671A Recycling Bottles(贪心+思维)
题目链接:http://codeforces.com/problemset/problem/671/A 题目大意:给你两个人的位置和一个箱子的位置,然后给出n个瓶子的位置,要求让至少一个人去捡瓶子放到 ...
- POJ 2653 - Pick-up sticks - [枚举+判断线段相交]
题目链接:http://poj.org/problem?id=2653 Time Limit: 3000MS Memory Limit: 65536K Description Stan has n s ...
- Vasya and Beautiful Arrays CodeForces - 354C (数论,枚举)
Vasya and Beautiful Arrays CodeForces - 354C Vasya's got a birthday coming up and his mom decided to ...
随机推荐
- DAY3-python函数
目录 一.了解函数 二. 函数定义 三.函数使用原则:先定义,后调用 四.定义函数的三种形式 五.函数的调用 六.函数的参数 七. 函数对象 八.函数嵌套 九.名称空间与作用域 十. 闭包函数 十一. ...
- 自定义Android Studio方法注释模板
前言 你们从Eclipse转到Android Studio的时候,是不是会怀念Eclipse的方法注释模版? 敲/**加回车,模板就出来了,而Android Studio却不能自定义(或者我没有找到) ...
- kernel下制作动态logo
kernel下制作动态logo 在uboot中实现logo的好处是反映速度快. 在kernel中实现logo的好处是,不管是android还是什么其他平台,logo显示无需考虑上层平台. 参照三星平台 ...
- Ros学习——值得学习的package
RViz是一款强大的可视化工具,它允许你查看机器人中的传感器和内部状态. TF程序包(package)提供在机器人所使用到的各种坐标系之间的变换功能,并保持跟踪这些变换的变化. actionlib - ...
- WebSocket详解(一):初步认识WebSocket技术
1.什么是Socket?什么是WebSocket? 对于第1次听说WebSocket技术的人来说,两者有什么区别?websocket是仅仅将socket的概念移植到浏览器中的实现吗? 我们知道,在网络 ...
- Boost中实现线程安全
博客转载自: http://www.cnblogs.com/lvdongjie/p/4447142.html 1 boost原子变量和线程 #include <boost/thread.hpp& ...
- 你可能不知道的pdf的功能
可以创建交互式的pdf,比如在pdf页面添加一个按钮, 添加一个文本框. 上篇文章说了pdf有可移植性,这是个非常重要的特性,我就想能否把3d模型放入到pdf中,这样即使对方电脑没有3d软件也可以查看 ...
- NSSelectorFromString 使用示例
NSSelectorFromString 动态加载实例方法. SEL sel = NSSelectorFromString(@"yourMethod:")//有参数 if([obj ...
- c语言中会遇到的面试题
预处理器(Preprocessor) 1 . 用预处理指令#define 声明一个常数,用以表明1年中有多少秒(忽略闰年问题) #define SECONDS_PER_YEAR (60 ...
- Spring第一篇
我计划用3到五篇文章来描述Spring,这是第一篇 1 . 什么是Spring Spring 是一个开源框架,Spring 是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnso ...