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 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: np.random.permutation 是 numpy 中的一个函数,它可以将一个数组中的元素随机打乱,返回一个打乱后的新数组。 使用方法如下: ```python import numpy as np # 对一个列表进行打乱 arr = [1, 2, 3, 4, 5] np.random.permutation(arr) # 对一个 numpy 数组进行打乱 arr = np.array([1, 2, 3, 4, 5]) np.random.permutation(arr) ``` 你也可以指定打乱的范围: ```python import numpy as np # 只打乱数组中的前 3 个元素 arr = np.array([1, 2, 3, 4, 5]) np.random.permutation(arr)[:3] ``` ### 回答2: np.random.permutation是NumPy库中的一个函数,用于对数组进行随机排列。它可以将数组中的元素打乱顺序,生成一个新的打乱后的数组。这个函数的使用非常灵活,可以接受多种不同类型的输入。 np.random.permutation函数可以接受一个整数作为参数,表示生成打乱顺序的数组的长度。比如,如果传入参数10,则会生成一个包含0到9的整数,且每个数字都会随机出现一次的数组。 此外,np.random.permutation函数还可以接受一个数组作为参数,表示对该数组进行随机排列。这个函数会返回一个新的打乱顺序后的数组,不会改变原始数组。这种用法可以用于在一些机器学习任务中,将数据集随机打乱,以便更好地进行训练。 需要注意的是,np.random.permutation函数对于数组的维度有特定的处理方式。如果传入的是一个n维数组,它会以第一个维度为准,对数组进行打乱顺序,而不会改变其他维度的相对位置。 总的来说,np.random.permutation函数是NumPy库中用于对数组进行随机排列的函数,可以生成一个新的打乱顺序的数组,或者对一个给定的数组进行随机排列。它的使用非常灵活,可以满足不同类型的需求。 ### 回答3: `np.random.permutation`是NumPy库中的一个函数,用于对给定的数组或整数序列进行随机排列。 当`np.random.permutation`的参数是一个整数n时,它会返回一个长度为n的随机排列的整数序列。例如,若`np.random.permutation(5)`返回[3, 0, 4, 1, 2]。 当`np.random.permutation`的参数是一个数组a时,它会返回一个随机排列的a的副本。该函数不会改变原始数组的顺序。例如,若`a = np.array([1, 2, 3, 4, 5])`,则`np.random.permutation(a)`可以返回[4, 1, 2, 5, 3]。 可以使用`np.random.permutation`函数来打乱数组的顺序,以便进行随机化处理或者洗牌操作。这对于在机器学习和数据分析中进行训练集与测试集划分、数据扩增等操作非常有用。 需要注意的是,`np.random.permutation`函数是基于随机数生成器的,通过设置`np.random.seed`函数可以获得重复的随机排列结果。此外,在高维数组中,`np.random.permutation`默认只会对第一个维度进行随机排序,若想对其他维度进行随机排序,可以使用`np.take`函数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值