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 n n lamps. States of the lamps are represented by the string s s of length n n . The i i -th character of the string s i s_i equals ‘0’ if the i i -th lamp is turned off or ‘1’ if the i i -th lamp is turned on. You are also given a positive integer k k .

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 k k -periodic if the distance between each pair of adjacent turned on lamps is exactly k k . Consider the case k = 3 k=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 k k -periodic garland from the given one.

You have to answer t t independent test cases.

1.3 Input

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

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

It is guaranteed that the sum of n n over all test cases does not exceed 1 0 6 10^6 ( n 1 0 6 \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 k k -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. 解读

动态规划。

题目只要求所有相邻的亮着的灯之间的相隔距离为 k k ,所有灯都灭 $ (000)$ 和只有一盏灯亮 ( 010 ) (010) 的情况都符合要求。

使 s ( x ) s(x) 表示第 x x 盏灯初始状态为亮 ( s ( x ) = 1 ) (s(x) = 1) 还是灭 ( s ( x ) = 0 ) (s(x) = 0) f ( x ) f(x) 为第 x x 盏灯以前所有的灯的最小开关次数, p ( x ) p(x) 为前缀和,即 [ 0 , x ] [0,x] 区间内所有亮着的灯的数量 p ( x ) = i = 1 n s ( x ) \displaystyle p(x) = \sum_{i=1}^n s(x)

递推计算,比较若使第 x x 个灯亮, x x 以前的灯全灭需要的开关次数 p ( x 1 ) p(x-1) 和按照 k k 间隔打开的开关次数 f ( x k ) + p ( x 1 ) p ( x k 1 ) f(x - k) + p(x - 1)- p(x - k - 1) ,取两者中的最小值。

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

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

使 g ( x ) g(x) 为第 x x 盏灯以后所有的灯的最小开关次数。同上。

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

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

那么最终结果为

a n s = { f ( x ) + g ( x ) , s ( x ) = 1 f ( 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. }

这里的减 1 1 是为了去除当 s ( x ) = 0 s(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,有问题欢迎通过邮箱交流。

猜你喜欢

转载自blog.csdn.net/qq_41729780/article/details/106200861