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 ...
随机推荐
- 微信商户/H5支付申请 被拒原因:网站存在不实内容或不安全信息
看到这张图片,申请H5支付的朋友估计是崩溃的,被拒的原因是网站存在不实内容或不安全信息. 解决方法有两种: 针对不安全内容,则是微信支付审核团队认为你的网站存在安全漏洞的,需要修复. 针对不实内容,则 ...
- cv::Mat类之type成员
CV_8UC3 -->16 CV_8UC3 表示存储元素的数据类型为 8bit unsigned char 类型,通道数C = 3 为什么OpenCV中type为CV_8UC3的图像 输出其ty ...
- 通过 iis或者本地IP 调试代码
首先说下这个操作的意义,做微信开发每次需要将代码部署后才能调试.现在设置了Nginx服务器,生产环境可以指向正式服务器地址,调试时可以将Nginx指向自己的PC,但是vs调试启动的默认地址是[loca ...
- python正则表达式--match search方法
1.re.match函数 re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回None. (1)函数语法: re.match(pattern, st ...
- 第十五章:Oracle12c 数据库 警告日志
一:查看警告日志文件的位置 Oracle 12c环境下查询,alert日志并不在bdump目录下,看到网上和书上都写着可以通过初始化参数background_dump_dest来查看alter日志路径 ...
- 面向对象(OOP)基本概念
面向对象(OOP)基本概念 面向对象编程 —— Object Oriented Programming 简写 OOP 目标 了解 面向对象 基本概念 01. 面向对象基本概念 我们之前学习的编程方式就 ...
- GIT----IDEA配置git
配置git 创建本地厂库 可以选中项目所在的目录下 此时发现所有的页面的文件都变红,是因为变红的文件还没有add 添加提交的项目(add) 选中提交的文件右击,git ,add 如果想把整个项目都ad ...
- ELK搭建<一>:搭建ES集群
1.首先进入官网下载ES,如果下载最新之前的版本 点击past releases就行了. 2.解压后进入config修改配置文件elasticsearch.yml #集群名称 cluster.name ...
- log4j配置,输出sql到控制台
网上的 # Global logging configuration log4j.rootLogger=ERROR, stdout # log4j.logger后面跟着的是项目dao包路径,里面全部都 ...
- MiniGUI 如何显示繁体字
有两种编码格式都可以显示繁体字,一种是GBK,一种是BIG5: 由于MiniGUI官方提供的字库的字体大小太小,所以只得自己去做字库,我用一个字库生成软件,分别做了一个GBK编码的字库和一个BIG5编 ...