zoukankan      html  css  js  c++  java
  • [CF1353E]K-periodic Garland

    【原题】

    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 sisi 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.

    Input

    The first line of the input contains one integer tt (1≤t≤25 0001≤t≤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 (1≤n≤106;1≤k≤n1≤n≤106;1≤k≤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 106106 (∑n≤106∑n≤106).

    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.

    Example

    input

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

    output

    1
    2
    5
    4
    0
    0
    

    【思路】

    将%k相等的位置存起来,分别考虑。

    如果 a[m] == 1 && a[m + k] == 1,则两者在一个区间。

    初始化dp[i] = tot - cnt[i] , 即只保留这个区间的灯, 关闭所有其它的灯的代价。tot为所有亮灯的个数,cnt[i]为当前区间亮灯的个数。

    考虑区间合并的最小代价,当前区间和上一区间合并的代价为上一个区间的代价 + (当前区间的第一个数 - 上一个区间的最后一个数) / k - 1 - 当前区间亮灯的个数 因为上个区间的代价中包含了灭这个区间灯的代价,所以要减去, 再加上使两个区间连贯的需要开的灯的个数。

    #include <algorithm>
    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <list>
    #include <map>
    #include <iostream>
    #include <queue>
    #include <set>
    #include <stack>
    #include <string>
    #include <vector>
    #define LL long long
    #define inf 0x3f3f3f3f
    #define INF 0x3f3f3f3f3f3f
    #define PI 3.1415926535898
    #define F first
    #define S second
    #define endl '
    '
    #define lson  rt << 1
    #define rson  rt << 1 | 1
    #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x)
    #define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
    using namespace std;
    
    const int maxn =1e6 + 7;
    const int maxm = 207;
    int n, k, a[maxn],  dp[maxn], nub[maxn];
    vector<int> v[maxn];
    
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        int t;
        cin >> t;
        while (t--)
        {
            cin >> n >> k;
            _rep(i, 0, k) v[i].clear();
            char c;
            int tot = 0;
            _rep(i, 1, n)
            {
                cin >> c;
                a[i] = c - '0';
                tot += a[i];
                if (a[i]) v[i % k].push_back(i);
            }
            int ans = inf;
            _rep(i, 0, k - 1)
            {
                if (!v[i].size() || tot - v[i].size() >= ans ) continue;
                _rep(i, 0, n / k) dp[i] = 0;
                int cnt = 0;
                int tmp = 1;
                f(j, 1, v[i].size())
                {
                    if (v[i][j] != v[i][j - 1] + k)
                    {
                        nub[++cnt] = tmp;
                        tmp = 1;
                    }
                    else tmp++;
                }
                nub[++cnt] = tmp;
                _rep(j, 1, cnt) dp[j] = tot - nub[j];
                int p = nub[1];
                int mn = dp[1];
                _rep(j, 2, cnt)
                {
                    dp[j] = min(dp[j], dp[j - 1] + (v[i][p] - v[i][p - 1]) / k - 1 - nub[j]);
                    mn = min(mn, dp[j]);
                    p += nub[j];
                }
                ans = min(mn, ans);
            }
            if (ans == inf) ans = 0;
            cout << ans << endl;
        }
    }
    
  • 相关阅读:
    Windows 08 R2_组策略
    Nginx常用配置实例(4)
    Nginx日常维护操作(3)
    Nginx配置文件(2)
    Nginx概述和安装(1)
    Zabbix实战-简易教程--通过公众平台企业号发送短信
    HDFS ZKFC自动切换原理分析
    HDFS ZKFC自动切换原理分析
    DataNode启动优化改进:磁盘检测并行化
    DataNode启动优化改进:磁盘检测并行化
  • 原文地址:https://www.cnblogs.com/hfcdyp/p/13402150.html
Copyright © 2011-2022 走看看