Description
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
题型:模拟
题意:给出一串数,通过最少的改变次数,使这一串数变成连续的升序数列。
分析:
当时写的时候,一开始题意没捋清思路,后来明白题意之后,又开始想的太多,却愣是木有想到点子上,导致了一系列的WA,后来想到,通过统计频率的办法就很完美的解决问题。用每个数减去其下标得到一个差值,由于这个差值可能会很大,就不能用老方法来用数组统计频率(→_→数组也开不了那么大。。。),将这些差值sort一下,就能够找出其中最长的一段相同的数,这就是频率最高的数num。这样,我们在输入的数中将那些与下标差值不是num的数改成“下标+num”,而那些与下标差值是nun的数不用变,再将变得数的个数统计一下,这样就能得出答案。
注意:当n == 1的时候要直接输出0。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int n;
int a[56789],cha[56789];
int main()
{
int i,j;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
cha[i]=a[i]-i;
}
sort(cha+1,cha+n+1);
int cnt=1;
int maxup=0;
int t=cha[1];
for(i=1;i<n;i++)
{
if(cha[i]==cha[i+1])
cnt++;
else
{
if(maxup<cnt)
{
maxup=maxup>cnt?maxup:cnt;
t=cha[i];
}
cnt=1;
}
}
if(cha[n]==cha[n-1])
{
maxup=maxup>cnt?maxup:cnt;
t=cha[i];
}
if(n==1){
printf("0\n");
}
else{
printf("%d\n",n-maxup);
}
for(i=1;i<=n;i++)
{
a[i]=i+t;
}
for(i=1;i<n;i++){
printf("%d ",a[i]);
}
printf("%d\n",a[n]);
}
}