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 }
     
  • 相关阅读:
    [转]Sublime Text 3安装Json格式化插件
    Golang 新手可能会踩的 50 个坑【转】
    [golang]svg图片默认按照左上角旋转,改为按中心旋转,重新计算中心偏移量
    序列化是干什么的,有什么作用,什么情况下会用到?
    Hbase设置多个hmaster
    基于JMX动态配置Log4J日志级别
    面向过程与面向对象编程的区别和优缺点
    log4j自带的两个类MDC和NDC作用以及用途
    【架构师之路】集群/分布式环境下5种session处理策略
    Java Web项目如何做到升级不断掉服务,同时涉及到的相关问题
  • 原文地址:https://www.cnblogs.com/cszlg/p/3209075.html
Copyright © 2011-2022 走看看