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. [USACO5.3]校园网Network of Schools

    题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作“接受学校”).注意即使 B 在 A 学校的分发列表中, A 也不一定在 B 学校的列表中. 你要写 ...

  2. 01-Jenkins-Master节点安装

    Jenkins安装前需要JDK8,下载最新版本的Jenkins LTS 2.150.防止下载缓慢可以通过清华源进行下载. [root@node1 ~]# wget https://mirrors.tu ...

  3. I2C地址问题

    #define     MAX_17040_BATTERY_I2C_ADDR        (0x36) 设备地址 #define     MAX_17040_BATTERY_WRITE_ADDR   ...

  4. 安卓权限申请处理框架Android-UsesPermission

    安卓权限申请封装处理框架.测试支持4.0+.项目源于正式处理Android权限问题时,没找到简单.能满足被拒绝权限自动会到系统设置处理的框架,按自己的编程习惯造一个熟悉的轮子还是蛮好的.第一次使用An ...

  5. Item 17: 理解特殊成员函数的生成规则

    本文翻译自modern effective C++,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 C++的官方说法中,特殊成员函数是C++愿意去主动生成的.C++9 ...

  6. Redis系列文章总结:ASP.Net Core 中如何借助CSRedis实现一个安全高效的分布式锁

    引言:最近回头看了看开发的.Net Core 2.1项目的复盘总结,其中在多处用到Redis实现的分布式锁,虽然在OnResultExecuting方法中做了防止死锁的处理,但在某些场景下还是会发生死 ...

  7. LeetCode 657. Robot Return to Origin

    There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its mov ...

  8. python3 pip 安装Scrapy在win10 安装报错error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools

    问题描述 当前环境win10,python_3.6.1,64位. 在windows下,在dos中运行pip install Scrapy报错: building 'twisted.test.raise ...

  9. 网络编程-C/S架构

    什么是C/S架构 C指的是client(客户端软件),S指的是Server(服务端软件),本章的重点就是教大家写一个C/S架构的软件,实现服务端软件与客户端软件基于网络通信. 计算机基础知识 作为应用 ...

  10. 书城项目第五阶段---book表的curd

    JavaEE三层架构分析 MVC