Codeforces Round #533 (Div. 2) 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.
题意:给你一个含有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(暴力)的更多相关文章
- 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 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)题解
link orz olinr AK Codeforces Round #533 (Div. 2) 中文水平和英文水平都太渣..翻译不准确见谅 T1.给定n<=1000个整数,你需要钦定一个值t, ...
- 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 ...
- 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks
题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...
- 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 ...
- Codeforces Round #533 (Div. 2) Solution
A. Salem and Sticks 签. #include <bits/stdc++.h> using namespace std; #define N 1010 int n, a[N ...
- 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 ...
- Codeforces Round #533 (Div. 2) C.思维dp D. 多源BFS
题目链接:https://codeforces.com/contest/1105 C. Ayoub and Lost Array 题目大意:一个长度为n的数组,数组的元素都在[L,R]之间,并且数组全 ...
随机推荐
- 数据库之mysql篇(6)—— mysql常用函数函数/自定义函数
常用函数 运算函数 我相信你都能看懂,所以以上的不再做过多解释 然后还有个,前面漏掉的between and: 意指10是否在0到20之间,如果是返回1,否则返回0 日期函数 这个要稍微注意一下参数, ...
- HDU ACM 1690 Bus System (SPFA)
Bus System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Vue学习之路8-v-on指令学习简单事件绑定之属性
前言 上一篇文章以v-on指令绑定click事件为例介绍了v-on指令的使用方法,本文介绍一下v-on绑定事件的一些属性的使用方法. v-on绑定指令属性 .stop属性 阻止单击事件继续向上传播(简 ...
- June 15. 2018 Week 24th Friday
If at first you don't succeed, then dust yourself off and try again. 失败了没关系,重振旗鼓,从头再来. From Aaliyah, ...
- ES5-ES6-ES7_字符串扩展—模板字符串
includes(), startsWith(), endsWith() 传统上,JavaScript只有indexOf方法,可以用来确定一个字符串是否包含在另一个字符串中.ES6又提供了三种新方法 ...
- DP 魔族密码 LIS
题目描述 风之子刚走进他的考场,就…… 花花:当当当当~~偶是魅力女皇——花花!!^^(华丽出场,礼炮,鲜花) 风之子:我呕……(杀死人的眼神)快说题目!否则……-_-### 花花:……咦好冷我们现在 ...
- 设计模式のStatePattern(状态模式)----行为模式
一.产生背景 在面向对象软件设计时,常常碰到某一个对象由于状态的不同而有不同的行为.如果用if else或是switch case等方法处理,对象操作及对象的状态就耦合在一起,碰到复杂的情况就会造成代 ...
- linux+node.js+redis+mongodb+nginx环境的搭建
1.推荐购买阿里云服务器,使用Centos7.0的服务器版本,在创建完全系统并进入之后,第一步是更新服务器的相关组件 yum -y install gcc gcc-c++ openssl-dev ...
- Rsync服务实战
目录 1 安装rsync软件 2 配置 /etc/rsyncd.conf 3 创建用户(运行rsync服务的用户身份) 4 创建虚拟用户密码文件(客户端连接时候使用) 5启动 rsync 服务,并加入 ...
- lelnet爱一直在
[root@elk3 ~]# telnet 192.168.122.135 22 Trying 192.168.122.135... 这意味着telnet 没有收到任何回应来建立连接.有两个可能的原因 ...