博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Integer Numbers
阅读量:6476 次
发布时间:2019-06-23

本文共 2311 字,大约阅读时间需要 7 分钟。

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

65 4 5 2 1 8

Sample Output

33 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 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 11 using namespace std;12 13 const int SZ = 50002;14 int n, a[SZ];15 map
> 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 else31 {32 cnt[x].second++;33 }34 }35 int max = -1, p;36 for(map
>::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", 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("\n");51 }52 return 0;53 }

 

 

转载地址:http://vslko.baihongyu.com/

你可能感兴趣的文章
WPF资料链接
查看>>
再次更新
查看>>
利用Windows自带的Certutil查看文件MD5
查看>>
查询指定名称的文件
查看>>
开篇,博客的申请理由
查看>>
[JSOI2008]星球大战starwar BZOJ1015
查看>>
centos 7 部署LDAP服务
查看>>
iOS项目分层
查看>>
IntelliJ IDEA 注册码
查看>>
String字符串的截取
查看>>
DynamoDB Local for Desktop Development
查看>>
Shell编程-环境变量配置文件
查看>>
Struts2和Spring MVC的区别
查看>>
理解Javascript参数中的arguments对象
查看>>
<<The C Programming Language>>讀書筆記
查看>>
git代码冲突
查看>>
解析查询 queryString 请求参数的函数
查看>>
git bash 风格调整
查看>>
linux操作系统加固软件,系统安全:教你Linux操作系统的安全加固
查看>>
linux中yum源安装dhcp,24.Linux系统下动态网络源部署方法(dhcpd)
查看>>