zoj 3365 (map容器)

Sample Input

6
5 4 5 2 1 8

Sample Output

3
3 4 5 6 7 8


题意 : 修改最少的数,使得序列变成连续的 。


一个序列连续,那么每个数的值和下标的差值相等 ;  所以先把每个数与自身下标的差值存起来,差值相等最多的就是不需要懂的,其他的都需要改  ;数据太大,要用map容器存 ;
科普几个容器的函数  ;
begin()  ;返回指向容器中第一个元素的迭代器地址 ;
end() ;    返回最后一个元素的后面迭代器地址;注意,不是最后一个元素的地址,而是最后一个元素的后一位的地址 ;
map<int,int>::iterator it    定义迭代器地址,it是一个地址;
 容器 m[i]=a ; 当取下标i时,用it->first ,当取a时,it->second ; it是指向m[i]=a的指针 ;


#include <string>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#define maxn 1500
#define maxe 10000*8
#define inf 1000000000
#define eps 1e-9
using namespace std;




int N;
int main()
{
  //  freopen("input.txt","r",stdin);
    int i,j;
    while( scanf("%d",&N)==1&&N)
    {
        map<int,int> mm;
        for(i=0; i<N; i++)
        {
            int temp;
            scanf("%d",&temp);
            mm[temp-i]++;     //差值存入容器中
        }




        int temp=0;
        int one;          
        for(map<int,int>::iterator it=mm.begin(); it!=mm.end(); it++)
        {
            if(temp<(it->second))    //比较差值的个数,选差值个数最多的
            {
                temp=(it->second);  
                one=(it->first);         //记录差值最多的值,也就是下标;
            }
        }
        printf("%d\n%d",N-temp,one);  
        for(i=1; i<N; i++)
        {
            printf(" %d",one+i);    //输出结果;
        }
        puts("");
    }
    return 0;
}

你可能感兴趣的:(zoj 3365 (map容器))