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 }
     
  • 相关阅读:
    Oracle 数据库管理脚本 命名规范
    Oracle 插入大量数据
    9i Data Gurad 报ORA12154: TNS:could not resolve service name 错误
    Oracle OCM 认证指南
    Redo Log 和Checkpoint not complete
    自己录制的Oracle 相关视频(陆续更新)
    自己录制的Oracle 相关视频(陆续更新)
    Oracle Rman 命令详解(List report backup configure)
    linux下手动删除数据库实例
    SELECT INTO 和 INSERT INTO SELECT 两种表复制语句
  • 原文地址:https://www.cnblogs.com/cszlg/p/3209075.html
Copyright © 2011-2022 走看看