Building Permutation

CodeForces - 285C

Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.

You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.

Input

The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).

Output

Print a single number — the minimum number of moves.

Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.

Examples

Input
2
3 0
Output
2
Input
3
-1 -1 2
Output
6

Note

In the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).

In the second sample you need 6 moves to build permutation (1, 3, 2).

sol:较显然的是最小的变成1,次小的变成2,以此类推,应该是最优的

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n;
ll a[N];
int main()
{
int i;
ll ans=;
R(n);
for(i=;i<=n;i++) R(a[i]);
sort(a+,a+n+);
for(i=;i<=n;i++) ans+=abs(i-a[i]);
Wl(ans);
return ;
}
/*
Input
2
3 0
Output
2 Input
3
-1 -1 2
Output
6
*/

codeforces285C的更多相关文章

随机推荐

  1. Java多线程学习(四)---控制线程

    控制线程 摘要: Java的线程支持提供了一些便捷的工具方法,通过这些便捷的工具方法可以很好地控制线程的执行 1. join线程控制,让一个线程等待另一个线程完成的方法 2. 后台线程,又称为守护线程 ...

  2. DOM(一)

    DOM可以将任何HMLT或XML文档描绘成一个由多层节点构成的结构,节点氛围几种不同的类型,每种类型分别表示文档中不同的信息及标记,每个节点都拥有各自的特点.数据和方法. Node类型 DOM1级定义 ...

  3. Maven项目远程部署到Tomcat

    目录 Maven项目远程部署到Tomcat 一.Tomcat插件支持的目标 二.系统要求及插件引入 2.1 系统要求 2.2 引入插件 三.远程部署war到tomcat 3.1 添加tomcat管理角 ...

  4. H5海报制作实践

    引言 年后一直处于秣马厉兵的状态,上周接到了一个紧急需求,为38妇女节做一个活动页,主要功能是生成海报,第一次做这种需求,我也是个半桶水前端,这里将碰到的问题.踩的坑,如何解决的分享给大家,讲的不到位 ...

  5. 页面添加iconfont字体-[超详细]-支持彩色

    第一步: 去矢量图官网注册一下,获取小图标(字体) 的来源 (也可以是其他类似的网站)这里以 阿里妈妈矢量图 官网为例,因为图标丰富,方便使用. 注册请点:https://www.iconfont.c ...

  6. MySQL的常用命令:添加外键,修改字段名称,增加字段 设置主键自增长等

    Mysql命令添加外键 前提是有这么几个表  以mall_product 和 mall_category为例 ALTER TABLE mall_product ADD CONSTRAINT fore_ ...

  7. C. Painting the Fence

    链接 [https://codeforces.com/contest/1132/problem/C] 题意 就是有个n长的栅栏,然后每个油漆工可以染的区域不同 给你q让你选出q-2个人使得被染色的栅栏 ...

  8. C语言之运算符、表达式和语句

    #include<stdio.h> #define ADJUST 7.31 int main(void) { const double SCALE = 0.333; double shoe ...

  9. Python_守护进程、锁、信号量、事件、队列

    1.创建进程 守护进程(*****) _.daemon = True #  _进程成为守护进程 守护进程也是一个子进程. 主进程的<代码>执行结束之后守护进程自动结束. import ti ...

  10. Vue基础(ES6)

      起步 1.扎实的HTML/CSS/Javascript基本功,这是前置条件. 2.不要用任何的构建项目工具,只用最简单的<script>,把教程里的例子模仿一遍,理解用法.不推荐上来就 ...