Rebranding 找規律

C. Median Smoothing
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
A schoolboy named Vasya loves reading books on programming and mathematics. He has recently read an encyclopedia article that described the method of median smoothing (or median filter) and its many applications in science and engineering. Vasya liked the idea of the method very much, and he decided to try it in practice.

Applying the simplest variant of median smoothing to the sequence of numbers a1, a2, …, an will result a new sequence b1, b2, …, bn obtained by the following algorithm:

b1 = a1, bn = an, that is, the first and the last number of the new sequence match the corresponding numbers of the original sequence.
For i = 2, …, n - 1 value bi is equal to the median of three values ai - 1, ai and ai + 1.
The median of a set of three numbers is the number that goes on the second place, when these three numbers are written in the non-decreasing order. For example, the median of the set 5, 1, 2 is number 2, and the median of set 1, 0, 1 is equal to 1.

In order to make the task easier, Vasya decided to apply the method to sequences consisting of zeros and ones only.

Having made the procedure once, Vasya looked at the resulting sequence and thought: what if I apply the algorithm to it once again, and then apply it to the next result, and so on? Vasya tried a couple of examples and found out that after some number of median smoothing algorithm applications the sequence can stop changing. We say that the sequence is stable, if it does not change when the median smoothing is applied to it.

Now Vasya wonders, whether the sequence always eventually becomes stable. He asks you to write a program that, given a sequence of zeros and ones, will determine whether it ever becomes stable. Moreover, if it ever becomes stable, then you should determine what will it look like and how many times one needs to apply the median smoothing algorithm to initial sequence in order to obtain a stable one.

Input
The first input line of the input contains a single integer n (3 ≤ n ≤ 500 000) — the length of the initial sequence.

The next line contains n integers a1, a2, …, an (ai = 0 or ai = 1), giving the initial sequence itself.

Output
If the sequence will never become stable, print a single number  - 1.

Otherwise, first print a single integer — the minimum number of times one needs to apply the median smoothing algorithm to the initial sequence before it becomes is stable. In the second line print n numbers separated by a space — the resulting sequence itself.

Examples
input
4
0 0 1 1
output
0
0 0 1 1
input
5
0 1 0 1 0
output
2
0 0 0 0 0

存在一個原數組a
可以用一個原數組構建一個新數組b
構建規則:b1=a1, bn=an
對於 1< i < n
bi= mid ( a i-1 , ai ,a i+1) (中位數)
讓你求,經過多少次構建後,這個新的數組經構建後不會變化

可以發現,如果存在00或者11,那麼這兩個位置的值就不會再變。
這時,如果存在一個長度爲k的區間,它的兩端永不會變,區間的每一個位置值都不等於它的前一個位置的值(比如 10101,1010,01010,0101),那麼經過(k-1)/2 次構建後,那麼它就不會再變化
如果k爲奇數(ababa) 最後 => aaaaa
如果k爲偶數(ababab)最後 => aaabbb
那麼我們遍歷原數組,處理所有這樣的區間即可

#include<bits/stdc++.h>
using namespace std;


const int INF  = 0x3f3f3f3f;
const int maxn = 500100;
const int Mod  = 1e9 + 7;

#define ll       long long
#define mem(x,y) memset(x,y,sizeof(x))
#define IO       ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define lson     (id<<1)
#define rson     (id<<1)+1

inline ll gcd(ll a, ll b)           { return a % b == 0 ? b : gcd(b, a % b);}
inline ll lcm(ll a, ll b)           { return a * b / gcd(a, b);}
inline ll quick_pow(ll x, int k)    { ll ans = 1; while (k) { if (k & 1) ans = (ans * x) % Mod;  x = x * x % Mod;  k >>= 1; } return ans;}

int a[maxn], n, ans = 0, b[maxn], L;

int deal(int LL, int RR ) {
    int k = RR - LL + 1;
    if (k % 2) {
        for (int i = LL; i <= RR; i++) b[i] = a[LL];
    } else {
        for (int i = LL; i < LL + (k / 2); i++) b[i] = a[LL];
        for (int i = LL + (k / 2); i <= RR; i++) b[i] = a[RR];
    }
    return (k - 1) / 2;
}

int main() {
    IO;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];

    L = 1, b[1] = a[1], b[n] = a[n], a[n + 1] = a[n];

    for (int i = 2; i <= n + 1; i++) {
        if (a[i] == a[i - 1]) {
            ans = max(ans, deal(L, i - 1));
            L = i;
        }
    }
    cout << ans << endl;
    for (int i = 1; i <= n; i++) {
        cout << b[i] << (char)(i == n ? '\n' : ' ');
    }
}

發佈了146 篇原創文章 · 獲贊 12 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章