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:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <string> using namespace std; const int SZ = ;
int n, a[SZ];
map<int, pair<int, int> > cnt; int main()
{
while(scanf("%d", &n) != EOF)
{
cnt.clear();
for(int i = ; i < n; i++)
{
scanf("%d", a + i);
int x = a[i] - i;
if(cnt.find(x) == cnt.end())
{
cnt[x] = make_pair(i, );
}
else
{
cnt[x].second++;
}
}
int max = -, p;
for(map<int, pair<int, int> >::iterator it = cnt.begin(); it != cnt.end(); it++)
{
if((it->second).second > max)
{
max = (it->second).second;
p = (it->second).first;
}
}
printf("%d\n", n - max);
for(int i = a[p] - p; i < a[p]; i++)
printf("%d ", i);
printf("%d", a[p]);
for(int i = a[p] + ; i < a[p] + n - p; i++)
printf(" %d", i);
printf("\n");
}
return ;
}
 

Integer Numbers的更多相关文章

  1. ZOJ 3365 Integer Numbers

    Integer Numbers Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on ZJU. Origina ...

  2. NYOJ题目436sum of all integer numbers

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAr0AAAHKCAIAAACBiWRrAAAgAElEQVR4nO3dP1LjSts34G8T5CyEFB

  3. 【CSU 1556】Pseudoprime numbers

    题 Description Jerry is caught by Tom. He was penned up in one room with a door, which only can be op ...

  4. UVALive 7279 Sheldon Numbers (暴力打表)

    Sheldon Numbers 题目链接: http://acm.hust.edu.cn/vjudge/contest/127406#problem/H Description According t ...

  5. Educational Codeforces Round 2 A. Extract Numbers 模拟题

    A. Extract Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...

  6. 关于Object数组强转成Integer数组的问题:Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;

    一.当把Object数组,强转的具体的Integer数组时,会报错. 代码如下: //数组强转报错演示 Object[] numbers = {1,2,3}; Integer[] ints = (In ...

  7. Code Signal_练习题_Circle of Numbers

    Consider integer numbers from 0 to n - 1 written down along the circle in such a way that the distan ...

  8. Codeforces 600A. Extract Numbers 模拟

    A. Extract Numbers time limit per test: 2 seconds memory limit per test: 256 megabytes input: standa ...

  9. SGU 140. Integer Sequences 线性同余,数论 难度:2

    140. Integer Sequences time limit per test: 0.25 sec. memory limit per test: 4096 KB A sequence A is ...

随机推荐

  1. c# webBrowser清除缓存问题

    1.webBrowser的浏览器为ie. 2.通过清除ie缓存即可. 3.代码调用如下: public enum ShowCommands : int { SW_HIDE = , SW_SHOWNOR ...

  2. 用vs调试项目的时候报HTTP 错误 403.14 - Forbidden

    曾经遇到过这种诡异的问题,你一定想不到,这个可能是因为你用svn合并的时候,导致了你的dll文件出了问题. 竟然可以用主干的dll替换的方式,解决掉这个问题.

  3. 第5题 查找字符串中的最长回文字符串---Manacher算法

    转载:https://www.felix021.com/blog/read.php?2040 首先用一个非常巧妙的方式,将所有可能的奇数/偶数长度的回文子串都转换成了奇数长度:在每个字符的两边都插入一 ...

  4. 一次WebSphere性能问题诊断过程

    一次接到用户电话,说某个应用在并发量稍大的情况下就会出现响应时间陡然增大,同时管理控制台的响应时间也很慢,几乎无法进行正常工作. 赶到现场后,查看平台版本为Webshpere6.0.2.29,操作系统 ...

  5. 单机安装 consul ui 对外提供服务

    Consul 安装启动ui,外网无法访问,应为Consul 默认绑定127.0.0.1 ,所以外网无法访问. 通过设置 -client 参数来设置     consul agent -server - ...

  6. javascript之彻底理解this

    彻底理解this,需要彻底理解函数 函数是复杂类型,存储在堆中.  函数是独立的, 对象中的方法只是对象中有个函数的引用 函数被调用时,调用者会像被调用者提供个上下文环境, 这个环境就是this 构造 ...

  7. 利用C#进行文件读写的方法选择总结

    小的文本文件(100M以下)直接用File类的ReadAllText()和WriteAllText()方法 这两个方法内部其实就是封装了StreamReader类的ReadToEnd()和Stream ...

  8. 【.Net】在WinForm中选择本地文件

    相信很多朋友在日常的编程中总会遇到各钟各样的问题,关于在WinForm中选择本地文件就是很多朋友们都认为很难的一个学习.net的难点, 在WebForm中提供了FileUpload控件来供我们选择本地 ...

  9. UVA11625_Lines of Containers

    题意很简单,给你一个n*m的矩阵,现在问你这个矩阵能否变为标准矩阵(即数字从小到大),如果能最少需要几步呢? 其实是个赤果果的水题.记得暑假安叔也出过一个类似的题目,那个好像是在codeforces上 ...

  10. P4169 [Violet]天使玩偶/SJY摆棋子

    题目背景 感谢@浮尘ii 提供的一组hack数据 题目描述 Ayu 在七年前曾经收到过一个天使玩偶,当时她把它当作时间囊埋在了地下.而七年后 的今天,Ayu 却忘了她把天使玩偶埋在了哪里,所以她决定仅 ...