codeforces 285C Building Permutation (简单贪心)

来源:互联网 发布:ubuntu安装深度商店 编辑:程序博客网 时间:2024/06/05 02:35
Building Permutation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Permutationp is an ordered set of integersp1,  p2,  ...,  pn, consisting ofn distinct positive integers, each of them doesn't exceedn. We'll denote thei-th element of permutation p aspi. We'll call numbern the size or the length of permutationp1,  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 containsn 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 С++. It is preferred to use the cin, cout streams or the%I64d specifier.

Examples
Input
23 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).

问题:就是给你一组数...然后让它变为自然数数列(不用管顺序)所需要的最小改变...

分析:问题其实就是找每个数和自然数列差的绝对值的和最小...而自然数列是递增数列...所以把题目里给的数列排成递增的,然后算下差的绝对值的和就行...

注意:有个坑是用来记改变的sum值用int的话会越界...所以还是得用long long...其他的都用int就行...注意题目中给的范围...

#include<iostream>#include<algorithm>using namespace std;int main(){int n,a[300005];cin>>n;for(int i=0;i<n;i++){cin>>a[i];}sort(a,a+n);int t=1;long long sum=0;for(int j=0;j<n;j++){sum+=abs(j+1-a[j]);}cout<<sum<<endl;return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 怀孕全部过程视频播放 水电工在厨房和妻子的视频 妻子出轨水电工在线播放 老公把婆婆干怀孕 在丈夫面前被侵 中文字幕全集 部长的妻子味道中文字幕 中文字幕被水电工掠夺的妻子 妻子在厨房跟水电工 互动 妻子与水电工在线 丈夫出差妻子与水电工 樱萌子被水电工侵犯中文字幕 在丈夫的面前侵犯入侵中文字幕 好妻子在线中文字幕 前田香织侵犯中文字幕 佐佐木明希上司中文字幕迅雷 医生d淫孕妇 公公一晚上要8次 公公现在就想要白关婷中文字幕 丈夫上司侵犯系列在线 义父犯美媳嫁樱花叶菜在线播放 儿子的妻子在线中文字幕云播 老公去世公公每天晚上抱着我睡 被水电工掠夺的妻子在线播放b 佐佐木明希大嫂中文字幕全集 善良的小妻子字幕 公公在我洗碗时在厨房要了我 大嫂被肉翻了天佐佐木明希1 教师蕾丝短裙中文字幕 瓜棚里和大嫂乱禽 强睡大嫂中文字 免费 佐佐木明希 房东中文字幕 与父亲干柴烈火 中文字幕丈夫不在 9大嫂被禽翻中文字幕 侵犯你的贞洁 中文字幕 年轻的妻子在线观中文字幕 大嫂被翻天了佐佐木b希中文7 美丽的大嫂中文字幕影迅雷下载 邻居的妻子中文字幕下载 神马电影院电影中文 神马电影院理论中文