zoukankan      html  css  js  c++  java
  • CodeForce 137B

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

    Description

    "Hey, it's homework time" — thought Polycarpus and of course he started with his favourite subject, IT. Polycarpus managed to solve all tasks but for the last one in 20 minutes. However, as he failed to solve the last task after some considerable time, the boy asked you to help him.

    The sequence of n integers is called a permutation if it contains all integers from 1 to n exactly once.

    You are given an arbitrary sequence a1, a2, ..., an containing n integers. Each integer is not less than 1 and not greater than 5000. Determine what minimum number of elements Polycarpus needs to change to get a permutation (he should not delete or add numbers). In a single change he can modify any single sequence element (i. e. replace it with another integer).

    Input

    The first line of the input data contains an integer n (1 ≤ n ≤ 5000) which represents how many numbers are in the sequence. The second line contains a sequence of integers ai (1 ≤ ai ≤ 5000, 1 ≤ i ≤ n).

    Output

    Print the only number — the minimum number of changes needed to get the permutation.

    Sample Input

    Input
    3
    3 1 2
    Output
    0
    Input
    2
    2 2
    Output
    1
    Input
    5
    5 3 3 3 1
    Output
    2

    Hint

    The first sample contains the permutation, which is why no replacements are required.

    In the second sample it is enough to replace the first element with the number 1 and that will make the sequence the needed permutation.

    In the third sample we can replace the second element with number 4 and the fourth element with number 2.

    这道题的题意是给出一个数n,再给出n个数,要求从1到n都要出现在这个数组中,可以改变任意一个数以达到这个状态,问最小需要几次。这个题需要注意的地方有,数组给的可能是乱序的 ,数组中可能有大于n的数,应该作为非法的数处理。

    #include<iostream>
     using namespace std;
     #include<algorithm>
     int a[5010];
     int main()
     {
         int num = 0,i,j = 0;
         cin >> num;
         for(i = 0;i < num;i++)
    {
    cin >> a[i]; } sort(a,a+num);//将数组排序便于统计 for(i = 0;i < num && a[i] <= num;i++)//统计小于num且不同的数 { if(a[i] == a[i+1])continue; else j++; } cout << num - j; return 0; }
  • 相关阅读:
    将Ajax 中数组转换成字符串 封装成类
    网页中删除数据弹出提示框
    pdo连接数据库
    pdo 的配置与启用
    php中常用的运算符
    [JZOJ 5912] [NOIP2018模拟10.18] VanUSee 解题报告 (KMP+博弈)
    [JZOJ 5910] [NOIP2018模拟10.18] DuLiu 解题报告 (并查集+思维)
    [JZOJ 5852] [NOIP2018提高组模拟9.6] 相交 解题报告 (倍增+LCA)
    [JZOJ 5437] [NOIP2017提高A组集训10.31] Sequence 解题报告 (KMP)
    [JZOJ 5875] [NOIP2018提高组模拟9.20] 听我说,海蜗牛 解题报告(BFS+二分)
  • 原文地址:https://www.cnblogs.com/lwy-kitty/p/3194219.html
Copyright © 2011-2022 走看看