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 }
     
  • 相关阅读:
    广播系统android安全:flag FLAG_RECEIVER_REGISTERED_ONLY的意义
    产品类大话设计模式——简单工厂模式
    打印数组算法:堆栈与深度优先搜索(迷宫问题)
    函数声明第四章利用函数实现指定的功能
    构造函数调用C++ 类和动态内存分配
    命令密码MySQL忘记密码恢复密码的实现方法
    线程资源PHP源码分析之线程安全模型
    编译类【COCOS2DXLUA 脚本开发之十四】解决自定义CPP类通过TOLUA++ BINDING LUACOCOS2D后编译到ANDROID运行黑屏(没有调用自定义CPP类)的问题!
    语句数据库ubuntu下mysql的常用命令
    eclipse中配置tomcat
  • 原文地址:https://www.cnblogs.com/cszlg/p/3209075.html
Copyright © 2011-2022 走看看