CodeForces 1353 E. K-periodic Garland 動態規劃

1. 題目描述

1.1. Limit

Time Limit: 1 second

Memory Limit: 256 megabytes

1.2 Problem Description

You are given a garland consisting of nn lamps. States of the lamps are represented by the string ss of length nn. The ii-th character of the string sis_i equals ‘0’ if the ii-th lamp is turned off or ‘1’ if the ii-th lamp is turned on. You are also given a positive integer kk.

In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa).

The garland is called kk-periodic if the distance between each pair of adjacent turned on lamps is exactly kk. Consider the case k=3k=3. Then garlands “00010010”, “1001001”, “00010” and “0” are good but garlands “00101001”, “1000001” and “01001100” are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa.

Your task is to find the minimum number of moves you need to make to obtain kk-periodic garland from the given one.

You have to answer tt independent test cases.

1.3 Input

The first line of the input contains one integer tt (1t25 0001 \le t \le 25~ 000) — the number of test cases. Then tt test cases follow.

The first line of the test case contains two integers nn and kk (1n106;1kn1 \le n \le 10^6; 1 \le k \le n) — the length of ss and the required period. The second line of the test case contains the string ss consisting of nn characters ‘0’ and ‘1’.

It is guaranteed that the sum of nn over all test cases does not exceed 10610^6 (n106\sum n \le 10^6).

1.4 Output

For each test case, print the answer — the minimum number of moves you need to make to obtain kk-periodic garland from the given one.

1.5 Sample Input

6
9 2
010001010
9 3
111100000
7 4
1111111
10 3
1001110101
1 1
1
1 1
0

1.6 Sample Onput

1
2
5
4
0
0

1.7 Source

CodeForces 1353 E. K-periodic Garland


2. 解讀

動態規劃。

題目只要求所有相鄰的亮着的燈之間的相隔距離爲 kk,所有燈都滅 $ (000)$ 和只有一盞燈亮 (010)(010) 的情況都符合要求。

使 s(x)s(x) 表示第 xx 盞燈初始狀態爲亮 (s(x)=1)(s(x) = 1) 還是滅 (s(x)=0)(s(x) = 0)f(x)f(x) 爲第 xx 盞燈以前所有的燈的最小開關次數,p(x)p(x) 爲前綴和,即 [0,x][0,x] 區間內所有亮着的燈的數量 p(x)=i=1ns(x)\displaystyle p(x) = \sum_{i=1}^n s(x)

遞推計算,比較若使第 xx 個燈亮,xx 以前的燈全滅需要的開關次數 p(x1)p(x-1) 和按照 kk 間隔打開的開關次數 f(xk)+p(x1)p(xk1)f(x - k) + p(x - 1)- p(x - k - 1),取兩者中的最小值。

f(x)=min(p(x1),f(xk)+p(x1)p(xk1)) f(x) = \min (p(x-1), f(x - k) + p(x - 1)- p(x - k - 1))

如果 s(x)=0s(x) = 0f(x)f(x) 需要再加 11

使 g(x)g(x) 爲第 xx 盞燈以後所有的燈的最小開關次數。同上。

g(i)=min(p(n)p(x),g(x+k)+p(x+k1)p(x)); g(i) = \min (p(n) - p(x), g(x + k) + p(x + k - 1) - p(x));

如果 s(x)=0s(x) = 0g(x)g(x) 需要再加 11

那麼最終結果爲

ans={f(x)+g(x),s(x)=1f(x)+g(x)1,s(x)=0 ans = {\left\{ \begin{aligned} &f(x) + g(x) & , s(x) = 1 \\ &f(x) + g(x) - 1 & , s(x) = 0 \\ \end{aligned}\right. }

這裏的減 11 是爲了去除當 s(x)=0s(x) = 0 的重複計算。

3. 代碼

#include <iostream>
using namespace std;
const int N = 1e6 + 10;
int t, n, k;
int pre[N], f[N], g[N];
char s[N];
int main()
{
    int i, ans, x;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d", &n, &k);
        scanf("%s", s);
        // 計算前綴和
        for (i = 0; i < n; i++)
            pre[i + 1] = pre[i] + s[i] - '0';
        // 若全爲0,輸出0
        if (pre[n] == 0) {
            printf("0\n");
            continue;
        }
        // 遞推計算,比較若使i爲打開狀態
        // i點以前的燈全滅需要的開關次數pre[i - 1]
        // 和按照k間隔打開的開關次數
        for (i = 1; i <= n; i++) {
            f[i] = pre[i - 1];
            // 若前面有k個元素
            // 前者爲前綴和,即i點以前的燈全滅的開關次數
            if (i - k > 0)
                f[i] = min(f[i], f[i - k] + pre[i - 1] - pre[i - k - 1]);
            if (s[i - 1] == '0')
                f[i]++;
        }
        // 遞推計算,比較i點以後的燈全滅需要的開關次數
        // 和按照k間隔打開的開關次數
        for (i = n; i >= 1; i--) {
            g[i] = pre[n] - pre[i];
            if (i + k <= n)
                g[i] = min(g[i], g[i + k] + pre[i + k - 1] - pre[i]);
            if (s[i - 1] == '0')
                g[i]++;
        }
        //  計算最終結果
        ans = n + 1;
        for (i = 1; i <= n; i++) {
            x = f[i] + g[i];
            // 去除重複計算
            if (s[i - 1] == '0')
                x--;
            if (x < ans)
                ans = x;
        }
        // 輸出
        printf("%d\n", ans);
    }
    return 0;
}


聯繫郵箱:[email protected]

Github:https://github.com/CurrenWong

歡迎轉載/Star/Fork,有問題歡迎通過郵箱交流。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章