ZOJ 3365 Integer Numbers

Integer Numbers

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on  ZJU. Original ID: 3365
64-bit integer IO format: %lld      Java class name: Main
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

 

Source

Author

Andrew Stankevich
 
解题:很容易发现规律,比如 1 2 3 4 5 6,你发现什么了?任意两个数字的差 恰好等于 他们的位置之差,所以只要统计各数字跟自己位置的差就可以了,看看哪种差最多!取差最多的那种,当然要修改的数字就最小了。
 
 1 #include <iostream>

 2 #include <cstdio>

 3 #include <cstring>

 4 #include <cmath>

 5 #include <algorithm>

 6 #include <climits>

 7 #include <vector>

 8 #include <queue>

 9 #include <cstdlib>

10 #include <string>

11 #include <set>

12 #include <stack>

13 #define LL long long

14 #define pii pair<int,int>

15 #define INF 0x3f3f3f3f

16 using namespace std;

17 const int maxn = 51000;

18 vector<int>g[maxn];

19 int lisan[maxn],d[maxn],n;

20 int main() {

21     while(~scanf("%d",&n)){

22         for(int i = 0; i < n; ++i){

23             g[i].clear();

24             scanf("%d",d+i);

25             lisan[i] = d[i] - i;

26         }

27         sort(lisan,lisan+n);

28         int cnt = 1;

29         for(int i = 1; i < n; ++i)

30             if(lisan[cnt-1] != lisan[i]) lisan[cnt++] = lisan[i];

31         for(int i = 0; i < n; ++i){

32             int index = lower_bound(lisan,lisan+cnt,d[i]-i)-lisan;

33             g[index].push_back(i);

34         }

35         int idx = -1,mx = 0;

36         for(int i = 0; i < cnt; ++i)

37             if(g[i].size() > mx) mx = g[idx = i].size();

38         printf("%d\n",n - g[idx].size());

39         int tmp = g[idx][0];

40         tmp = d[tmp] - tmp;

41         for(int i = 0; i < n; ++i)

42             printf("%d%c",tmp + i,i+1 == n?'\n':' ');

43     }

44     return 0;

45 }
View Code

 

你可能感兴趣的:(Integer)