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

 

转载于:https://www.cnblogs.com/crackpotisback/p/4082809.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值