zoukankan      html  css  js  c++  java
  • Codeforces Round #175 (Div. 2) C. Building Permutation(贪心)

    C. Building Permutation
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 С++. It is preferred to use the cin, cout streams or the %I64d specifier.

    Sample test(s)
    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).

    思路:对于若a[i]满足0<a[i]<=n,则标记tag[a[i]]=1,否则tag[a[i]]=1.对于集合A={a[i]|a[i]<=0 || a[i]>n},进行不降序排列,集合B=[1,n]-{a[i]|0<a[i]<=n}也不降序排列,A中元素转化为B中位置对应相同的元素。

    AC Code:

     1 #include <iostream>
     2 #include <fstream>
     3 #include <string>
     4 #include <set>
     5 #include <map>
     6 #include <vector>
     7 #include <stack>
     8 #include <queue>
     9 #include <cmath>
    10 #include <cstdio>
    11 #include <cstring>
    12 #include <algorithm>
    13 #include <utility>
    14 using namespace std;
    15 #define ll long long
    16 #define cti const int
    17 #define ctll const long long
    18 #define dg(i) cout << '*' << i << endl;
    19 
    20 int p[300002], pi;
    21 bool tag[300002];
    22 
    23 int main()
    24 {
    25     int n, x;
    26     ll cnt;
    27     while(scanf("%d", &n) != EOF)
    28     {
    29         memset(tag, false, sizeof(tag));
    30         pi = 0;
    31         for(int i = 0; i < n; i++)
    32         {
    33             scanf("%d", &x);
    34             if(x < 1 || x > n || tag[x]) p[pi++] = x;
    35             else tag[x] = true;
    36         }
    37         sort(p, p + pi);
    38         cnt = 0;
    39         for(int i = 0, j = 1; i < pi; i++, j++)
    40         {
    41             while(tag[j]) j++;
    42             cnt += (ll)abs(p[i] - j);
    43         }
    44         printf("%I64d\n", cnt);
    45     }
    46     return 0;
    47 }
  • 相关阅读:
    设计模式——原型模式
    设计模式——复合模式
    设计模式——桥接模式
    建筑模式
    设计模式——单键模式
    工厂模式
    抽象工厂
    设计模式——适配器模式
    一个简单的文件上传功能控件(原创)
    算法题:用php生成excel列
  • 原文地址:https://www.cnblogs.com/cszlg/p/2990297.html
Copyright © 2011-2022 走看看