codeforces_A. Salem and Sticks_数组/暴力
1 second
256 megabytes
standard input
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.
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.
Print the value of tt and the minimum possible cost. If there are multiple optimal choices for tt, print any of them.
3
10 1 4
3 7
5
1 1 2 2 3
2 0
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.
#include <bits/stdc++.h>
#include <cstdio> using namespace std; int n;
int a[];
int sum=; int cal(int k){
int res=;
for(int i=;i<n;i++){
if(abs(a[i]-k)>){
res+=(abs(a[i]-k)-);
}
}
return res;
} int main()
{
int minn=;
int maxx=;
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d",&a[i]);
sum+=a[i];
minn=a[i]<minn?a[i]:minn;
maxx=a[i]>maxx?a[i]:maxx;
}
int r1=;
int cost=;
int now=;
for(int i=minn;i<=maxx;i++){
now=cal(i);
if(now<cost){
r1=i;
cost=now;
}
}
printf("%d %d\n",r1,cost);
/*
int r1=sum/n;
int r2=r1+1;
int _r1=cal(r1,a);
int _r2=cal(r2,a); if(_r1<_r2){
printf("%d %d\n",r1,_r1);
}else{
printf("%d %d\n",r2,_r2);
}
*/ return ;
}
codeforces_A. Salem and Sticks_数组/暴力的更多相关文章
- BZOJ 4556: [Tjoi2016&Heoi2016]字符串(后缀数组 + 二分答案 + 主席树 + ST表 or 后缀数组 + 暴力)
题意 一个长为 \(n\) 的字符串 \(s\),和 \(m\) 个询问.每次询问有 \(4\) 个参数分别为 \(a,b,c,d\). 要你告诉它 \(s[a...b]\) 中的所有子串 和 \(s ...
- 【BZOJ-2251】外星联络 后缀数组 + 暴力
2251: [2010Beijing Wc]外星联络 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 670 Solved: 392[Submit][ ...
- BZOJ 2754: [SCOI2012]喵星球上的点名 [后缀数组+暴力]
2754: [SCOI2012]喵星球上的点名 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 1906 Solved: 839[Submit][St ...
- 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 ...
- [bzoj2251][2010Beijing Wc]外星联络——后缀数组+暴力求解
Brief Description 找到 01 串中所有重复出现次数大于 1 的子串.并按字典序输出他们的出现次数. Algorithm Design 求出后缀数组之后,枚举每一个后缀,对于每个后缀从 ...
- [luoguP2336] [SCOI2012]喵星球上的点名(后缀数组 + 暴力)
传送门 原本的想法是把所有的串不管是名字还是询问都连起来,记录一下询问串在sa数组中的位置 对于每个询问可以在sa数组中二分出左右边界,第一问用莫队,第二问差分乱搞. 结果发现我差分的思路想错了,先写 ...
- Codeforces Round #263 (Div. 1) C. Appleman and a Sheet of Paper 树状数组暴力更新
C. Appleman and a Sheet of Paper Appleman has a very big sheet of paper. This sheet has a form of ...
- 玲珑学院OJ 1023 - Magic boy Bi Luo with his excited math problem 树状数组暴力
分析:a^b+2(a&b)=a+b so->a^(-b)+2(a&(-b))=a-b 然后树状数组分类讨论即可 链接:http://www.ifrog.cc/acm/probl ...
- Codeforces Round #368 (Div. 2) E. Garlands 二维树状数组 暴力
E. Garlands 题目连接: http://www.codeforces.com/contest/707/problem/E Description Like all children, Ale ...
随机推荐
- JavaWeb之html
html :Hyper Text Markup Language 超文本标记语言 超文本:比文本功能更加强大 标记语言:通过一组标签对内容进行描述的一门语言 html书写规则: 文件的后缀名:.htm ...
- Python爬虫从入门到进阶(4)之xpath的使用
官网地址:https://lxml.de/xpathxslt.html 导入: from lxml import etree lxml.tree 支持 ElementTree 和 Element 上的 ...
- k8s部署etcd数据库集群
⒈下载 https://github.com/etcd-io/etcd/releases ⒉解压 tar -zxvf etcd-v3.3.12-linux-amd64.tar.gz ⒊移动可执行文件及 ...
- linux系统下完全卸载Jenkins
1.关闭tomcat:./shutdown.sh 2.删除/webapps/jenkins下所有文件:rm -rf jenkins 3.删除配置文件:rm -rf /root/.jenkins/
- 如何用java实现一个p2p种子搜索(2)-路由表实现
路由表实现 回顾一下上一篇讲的内容,上一篇提到从dht网络中获取infohash,那么加入dht网络后的最重要的第一步就是怎么去建立路由表. 路由表里面保存的是dht中其他node的信息,所以node ...
- ActiveMQ简单使用
// 第一步:创建ConnectionFactory对象,需要指定服务端ip及端口号. //brokerURL服务器的ip及端口号 ConnectionFactory connectionFactor ...
- SecureCRT标签显示IP地址
当使用SecureCRT连接到linux服务器后,SecureCRT的标签会随着操作目录的改变而改变,当连接多个的时候很不好区分,所以需要设置标签栏固定显示IP地址信息. options->Se ...
- MySQL存储过程中的事务执行失败之后获取错误信息
1.表结构: 2. 存储过程中: 代码如下: BEGINDECLARE CONTINUE HANDLER FOR SQLEXCEPTIONBEGINROLLBACK;GET DIAGNOSTICS C ...
- foot
码云链接:https://gitee.com/zyyyyyyyyyyy/codes/rcfdzmin4a82v975pl1ko47 效果图: 原网站截图: 源代码: <!DOCTYPE html ...
- 使用Set集合对List集合进行去重
/** * 使用Set集合对List集合进行去重 **/ public class SetTest { /** * List集合的泛型为Integer类型 * * @author hongwei.li ...