hust-Integer Numbers

hust-Integer Numbers解题报告

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

大致题意:

给一串数字,要求不改变这些数字的顺序,只改变其中一些数字的值,使这一串数字变为一个连续的数字。求出最小的改变方案。

做题思路:

给的数字啊a[n],每一个都减去自己的序号数,得到一串数字b[n]。例如:5 4 5 2 1 8减完之后就得到:5 3 3 -1 -3 3;然后再对b[n]排序,之后找出b[n]中出现次数最多的数字,将其作为temp.并且记录temp出现的次数,记为max.然后以temp作为标准,再一次加上序号,就可以得到答案。如:5 4 5 2 1 8其temp= 3,max=3.所以最终结果就是3 4 5 6 7 8.更改的数字个数为n-max=6-3=3.

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#define maxn 50010
using namespace std;
bool cmp(int x,int y)
{
    if(x<y)
        return true;
    return false;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int a[maxn],b[maxn];
        int i,j;
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            b[i]=a[i]-i;
        }
        sort(b,b+n,cmp);//对b[n]进行排序
        int temp,max=1,count=1;
        temp=b[0];//找出temp与max
        for(i=0;i<n-1;i++)
        {
            if(b[i]==b[i+1])
                count++;
            else
            {
                if(count>max)
                {
                    max=count;
                    temp=b[i];
                }
                count=1;
            }
        }
        if(count>max)
        {max=count;temp=b[i];}
        for(i=0;i<n;i++)//依次用temp加上序号
            a[i]=i+temp;
        printf("%d\n",n-max);
        for(i=0;i<n-1;i++)
            printf("%d ",a[i]);
        printf("%d\n",a[i]);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值