码迷,mamicode.com
首页 > 其他好文 > 详细

ZOJ 3365 Integer Numbers

时间:2014-11-08 12:00:15      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   os   java   sp   

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,你发现什么了?任意两个数字的差 恰好等于 他们的位置之差,所以只要统计各数字跟自己位置的差就可以了,看看哪种差最多!取差最多的那种,当然要修改的数字就最小了。
 
bubuko.com,布布扣
 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

 

ZOJ 3365 Integer Numbers

标签:style   blog   http   io   color   ar   os   java   sp   

原文地址:http://www.cnblogs.com/crackpotisback/p/4082809.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!