{"id":1419,"date":"2013-03-22T20:40:58","date_gmt":"2013-03-22T12:40:58","guid":{"rendered":"https:\/\/blog.rexdf.org\/?p=1419"},"modified":"2013-03-22T20:40:58","modified_gmt":"2013-03-22T12:40:58","slug":"codeforces-175-c","status":"publish","type":"post","link":"https:\/\/blog.rexdf.org\/ja\/2013\/03\/codeforces-175-c\/","title":{"rendered":"Codeforces 175 C"},"content":{"rendered":"<div>\n<div>C. Building Permutation<\/div>\n<div>\n<div>time limit per test<\/div>\n<p>2 seconds<\/p><\/div>\n<div>\n<div>memory limit per test<\/div>\n<p>256 megabytes<\/p><\/div>\n<div>\n<div>input<\/div>\n<p>standard input<\/p><\/div>\n<div>\n<div>output<\/div>\n<p>standard output<\/p><\/div>\n<\/div>\n<div>\n<p>Permutation\u00a0<i>p<\/i>\u00a0is an ordered set of integers\u00a0<i>p<\/i><sub>1<\/sub>,\u2009\u2009<i>p<\/i><sub>2<\/sub>,\u2009\u2009&#8230;,\u2009\u2009<i>p<\/i><sub><i>n<\/i><\/sub>, consisting of\u00a0<i>n<\/i>\u00a0distinct positive integers, each of them doesn&#8217;t exceed\u00a0<i>n<\/i>. We&#8217;ll denote the\u00a0<i>i<\/i>-th element of permutation\u00a0<i>p<\/i>\u00a0as\u00a0<i>p<\/i><sub><i>i<\/i><\/sub>. We&#8217;ll call number\u00a0<i>n<\/i>\u00a0the size or the length of permutation\u00a0<i>p<\/i><sub>1<\/sub>,\u2009\u2009<i>p<\/i><sub>2<\/sub>,\u2009\u2009&#8230;,\u2009\u2009<i>p<\/i><sub><i>n<\/i><\/sub>.<\/p>\n<p>You have a sequence of integers\u00a0<i>a<\/i><sub>1<\/sub>,\u2009<i>a<\/i><sub>2<\/sub>,\u2009&#8230;,\u2009<i>a<\/i><sub><i>n<\/i><\/sub>. 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.<\/p>\n<\/div>\n<div>\n<div>Input<\/div>\n<p>The first line contains integer\u00a0<i>n<\/i>\u00a0(1\u2009\u2264\u2009<i>n<\/i>\u2009\u2264\u20093\u00b710<sup>5<\/sup>) \u2014 the size of the sought permutation. The second line contains\u00a0<i>n<\/i>\u00a0integers<i>a<\/i><sub>1<\/sub>,\u2009<i>a<\/i><sub>2<\/sub>,\u2009&#8230;,\u2009<i>a<\/i><sub><i>n<\/i><\/sub>\u00a0(\u2009-\u200910<sup>9<\/sup>\u2009\u2264\u2009<i>a<\/i><sub><i>i<\/i><\/sub>\u2009\u2264\u200910<sup>9<\/sup>).<\/p>\n<\/div>\n<div>\n<div>Output<\/div>\n<p>Print a single number \u2014 the minimum number of moves.<\/p>\n<p>Please, do not use the\u00a0%lld\u00a0specifier to read or write 64-bit integers in \u0421++. It is preferred to use the cin, cout streams or the\u00a0%I64dspecifier.<\/p>\n<\/div>\n<div>\n<div>Sample test(s)<\/div>\n<div>\n<div>\n<div>input<\/div>\n<pre>2 3 0<\/pre>\n<\/div>\n<div>\n<div>output<\/div>\n<pre>2<\/pre>\n<\/div>\n<div>\n<div>input<\/div>\n<pre>3 -1 -1 2<\/pre>\n<\/div>\n<div>\n<div>output<\/div>\n<pre>6<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div>\n<div>Note<\/div>\n<p>In the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is\u00a0(2,\u20091).<\/p>\n<p>In the second sample you need 6 moves to build permutation\u00a0(1,\u20093,\u20092).<\/p>\n<\/div>\n<pre class=\"brush: cpp; gutter: true\">\r\n#include &lt;iostream&gt;\r\n#include &lt;cstdio&gt;\r\n#include &lt;cstring&gt;\r\n#include &lt;algorithm&gt;\r\n\r\nusing namespace std;\r\n\r\nlong long a[300005];\r\n\r\n\r\nint main()\r\n{\r\n    int n;\r\n    long long ans,tmp;\r\n    cin&gt;&gt;n;\r\n    for(int i=0;i&lt;n;i++)cin&gt;&gt;a[i];\r\n    sort(a,a+n);\r\n    ans=0;\r\n    for(int i=0;i&lt;n;i++)\r\n    {\r\n        tmp=(long long)(i+1)-a[i];\r\n        if(tmp&lt;0)ans-=tmp;\r\n        else ans+=tmp;\r\n    }\r\n    cout&lt;&lt;ans&lt;&lt;endl;\r\n    return 0;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>C. Building Permutation time limit per t &hellip; <a href=\"https:\/\/blog.rexdf.org\/ja\/2013\/03\/codeforces-175-c\/\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[125],"class_list":["post-1419","post","type-post","status-publish","format-standard","hentry","category-algorithm","tag-codeforces"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blog.rexdf.org\/ja\/wp-json\/wp\/v2\/posts\/1419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.rexdf.org\/ja\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.rexdf.org\/ja\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.rexdf.org\/ja\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.rexdf.org\/ja\/wp-json\/wp\/v2\/comments?post=1419"}],"version-history":[{"count":0,"href":"https:\/\/blog.rexdf.org\/ja\/wp-json\/wp\/v2\/posts\/1419\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.rexdf.org\/ja\/wp-json\/wp\/v2\/media?parent=1419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.rexdf.org\/ja\/wp-json\/wp\/v2\/categories?post=1419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.rexdf.org\/ja\/wp-json\/wp\/v2\/tags?post=1419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}