1367C - Social Distance - CodeForces Solution


constructive algorithms greedy math *1300

Please click on ads to support us..

Python Code:

for t in range(int(input())):
    n, k = map(int, input().split())
    a = ('0' * k + input() + '0' * k).split('1')

    ans = 0

    for i in a:
        ans += max((len(i) - k) // (k + 1), 0)

    print(ans)

C++ Code:

#include <bits/stdc++.h>

using namespace std;
#define int long long
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
typedef vector<int> vi;
typedef pair<int, int> pi;
#define sa cout << "YES" << endl
#define wa cout << "NO" << endl
int M = 1000000007;

// void dfs(int i, int j, vector<string> &v)
// {
//     if (i < 0 || j < 0)
//     {
//         return;
//     }
//     if (i >= v.size() || j >= v[0].length())
//     {
//         return;
//     }
//     if (v[i][j] == '.' || v[i][j] == '+')
//     {
//         return;
//     }

//     v[i][j] = '+';

//     dfs(i + 1, j, v);
//     dfs(i - 1, j, v);
//     dfs(i, j + 1, v);
//     dfs(i, j - 1, v);
// }
// bool cmp(pair<int, int> a, pair<int, int> b)
// {

//     return a.ss - a.ff < b.ss - b.ff;
// }
// int dp[1000002];
// vector<int> v;
// int func(int n)
// {
//     if (n == 0)
//         return 1;
//     if (dp[n] != -1)
//     {
//         return dp[n];
//     }
//     int ways = 0;

//     for (int i = 0; i < v.size(); i++)
//     {
//         if (n - v[i] >= 0)
//             ways += func(n - v[i]) % M;
//     }
//     return dp[n] = ways % M;
// }

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    // freopen("div7.in", "r", stdin);
    // freopen("div7.out", "w", stdout);

    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        int k;
        cin >> k;

        string s;
        cin >> s;
        int ans = 0;
        vector<int> pos;
        int count = 0;
        int ind = 0;
        for (int i = 0; i < s.size(); i++)
        {
            if (s[i] == '0')
            {
                count++;
                if (i == s.size() - 1)
                {
                    if (count >= 1)
                    {
                        ans += (count + k) / (k + 1);
                        ind = i + 1;
                    }
                }
            }
            else
            {
                if (count >= k + 1)
                {
                    ans += (count) / (k + 1);
                }
                ind = i + 1;
                break;
            }
        }
        count = 0;
        for (int i = ind; i < s.size(); i++)
        {
            if (s[i] == '0')
            {
                count++;
                if (i == s.size() - 1)
                {
                    if (count >= k + 1)
                    {
                        ans += (count) / (k + 1);
                    }
                }
            }
            else
            {
                if (count >= 2 * k + 1)
                {
                    ans += (count - k) / (k + 1);
                }
                count = 0;
            }
        }
        cout << ans << endl;
    }
}


Comments

Submit
0 Comments
More Questions

1382A - Common Subsequence
1512D - Corrupted Array
667B - Coat of Anticubism
284B - Cows and Poker Game
1666D - Deletive Editing
1433D - Districts Connection
2B - The least round way
1324A - Yet Another Tetris Problem
246B - Increase and Decrease
22E - Scheme
1566A - Median Maximization
1278A - Shuffle Hashing
1666F - Fancy Stack
1354A - Alarm Clock
1543B - Customising the Track
1337A - Ichihime and Triangle
1366A - Shovels and Swords
919A - Supermarket
630C - Lucky Numbers
1208B - Uniqueness
1384A - Common Prefixes
371A - K-Periodic Array
1542A - Odd Set
1567B - MEXor Mixup
669A - Little Artem and Presents
691B - s-palindrome
851A - Arpa and a research in Mexican wave
811A - Vladik and Courtesy
1006B - Polycarp's Practice
1422A - Fence