• Integer Numbers


    ZOJ Problem Set - 3365
    Integer Numbers

    Time Limit: 1 Second      Memory Limit: 32768 KB      Special Judge

    The boy likes numbers. He has a sheet of paper. He have written a sequence of consecutive integer numbers on the sheet. The boy likes them.

    But then the girl came. The girl is cruel. She changed some of the numbers.

    The boy is disappointed. He cries. He does not like all these random numbers. He likes consecutive numbers. He really likes them. But his numbers are not consecutive any more. The boy is disappointed. He cries.

    Help the boy. He can change some numbers. He would not like to change many of them. He would like to change as few as possible. He cannot change their order. He would like the numbers to be consecutive again. Help the boy.

    Input

    The first line of the input file contains n --- the number of numbers in the sequence (1 ≤ n ≤ 50000). The next line contains the sequence itself --- integer numbers not exceeding 109 by their absolute values.

    There are multiple cases. Process to the end of file.

    Output

    Output the minimal number of numbers that the boy must change. After that output the sequence after the change.

    Sample Input

    6
    5 4 5 2 1 8
    

    Sample Output

    3
    3 4 5 6 7 8
    

    Author: Andrew Stankevich
    Source: Andrew Stankevich's Contest #11
    思路:如果元素a[i],a[j]满足a[i] - i = a[j] - j,则a[i]和a[j]要么一同修改要么都不需修改。记录a[i]-i的最大出现次数cnt,cnt就是不需改变的数的最大个数,n-cnt就是需要改变的数的最少个数。
     
    AC Code:
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <vector>
     6 #include <queue>
     7 #include <map>
     8 #include <algorithm>
     9 #include <string>
    10 
    11 using namespace std;
    12 
    13 const int SZ = 50002;
    14 int n, a[SZ];
    15 map<int, pair<int, int> > cnt;
    16 
    17 int main()
    18 {
    19     while(scanf("%d", &n) != EOF)
    20     {
    21         cnt.clear();
    22         for(int i = 0; i < n; i++)
    23         {
    24             scanf("%d", a + i);
    25             int x = a[i] - i;
    26             if(cnt.find(x) == cnt.end())
    27             {
    28                 cnt[x] = make_pair(i, 1);
    29             }
    30             else
    31             {
    32                 cnt[x].second++;
    33             }
    34         }
    35         int max = -1, p;
    36         for(map<int, pair<int, int> >::iterator it = cnt.begin(); it != cnt.end(); it++)
    37         {
    38             if((it->second).second > max)
    39             {
    40                 max = (it->second).second;
    41                 p = (it->second).first;
    42             }
    43         }
    44         printf("%d
    ", n - max);
    45         for(int i = a[p] - p; i < a[p]; i++)
    46             printf("%d ", i);
    47         printf("%d", a[p]);
    48         for(int i = a[p] + 1; i < a[p] + n - p; i++)
    49             printf(" %d", i);
    50         printf("
    ");
    51     }
    52     return 0;
    53 }
     
  • 相关阅读:
    POJ 1966 Cable TV Network
    POJ 3204 Ikki's Story I
    Codeforces Round #388 (Div. 2)
    BZOJ 后缀自动机四·重复旋律7
    扩展欧几里得 exGCD
    网络流模板 NetworkFlow
    BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊
    BZOJ 3224: Tyvj 1728 普通平衡树
    BZOJ 1070: [SCOI2007]修车
    BZOJ 4552: [Tjoi2016&Heoi2016]排序
  • 原文地址:https://www.cnblogs.com/cszlg/p/3209075.html
Copyright © 2020-2023  润新知