zoukankan      html  css  js  c++  java
  • 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 }
     
  • 相关阅读:
    程序是如何在 CPU 中运行的(二)
    程序是如何在 CPU 中运行的(一)
    结构体内存对齐解析
    如何以面向对象的思想设计有限状态机
    union 的概念及在嵌入式编程中的应用
    STM32 内存分配解析及变量的存储位置
    C语言如何实现继承及容器
    C语言指定初始化器解析及其应用
    pandas中的 where 和mask方法
    python中的类变量
  • 原文地址:https://www.cnblogs.com/cszlg/p/3209075.html
Copyright © 2011-2022 走看看