Codeforces 371A.K-Periodic Array

186

题目



Description  


This task will exclusively concentrate only on the arrays where all elements equal 1 and/or 2.

Array a is k-period if its length is divisible by k and there is such array b of length k, that a is represented by array b written exactly times consecutively. In other words, array a is k-periodic, if it has period of length k.

For example, any array is n-periodic, where n is the array length. Array [2, 1, 2, 1, 2, 1] is at the same time 2-periodic and 6-periodic and array [1, 2, 1, 1, 2, 1, 1, 2, 1] is at the same time 3-periodic and 9-periodic.

For the given array a, consisting only of numbers one and two, find the minimum number of elements to change to make the array k-periodic. If the array already is k-periodic, then the required value equals 0.



Input  


The first line of the input contains a pair of integers n, k (1 ≤ k ≤ n ≤ 100), where n is the length of the array and the value n is divisible by k. The second line contains the sequence of elements of the given array a1, a2, ..., an (1 ≤ ai ≤ 2), ai is the i-th element of the array.


Output  


Print the minimum number of array elements we need to change to make the array k-periodic. If the array already is k-periodic, then print 0.


Sample Input  


Input  


6 2
2 1 2 2 2 1


Output  


1


Input  


8 4
1 1 2 1 1 1 2 1


Output  


0


Input  


9 3
2 1 1 1 2 1 1 1 2


Output  


3


Hint  


In the first sample it is enough to change the fourth element from 2 to 1, then the array changes to [2, 1, 2, 1, 2, 1].

In the second sample, the given array already is 4-periodic.

In the third sample it is enough to replace each occurrence of number two by number one. In this case the array will look as [1, 1, 1, 1, 1, 1, 1, 1, 1] — this array is simultaneously 1-, 3- and 9-periodic.


题解



判断长度为 n 的字符串
k 为循环长度
最少改变( 1 变成 2 或者 2 变成 1 )多少次可以满足循环

对于每一位,最后的效果是每个循环节,这一位的数字相同
由于只有可能是 12
可以记录 1 出现的次数 cnt
改变的次数就是 min(cnt,k-cnt)

最后将所有的改变次数加起来即可

代码


/*
By:OhYee
Github:OhYee
Blog:http://www.oyohyee.com/
Email:oyohyee@oyohyee.com

かしこいかわいい?
エリーチカ!
要写出来Хорошо的代码哦~
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <bitset>
#include <iomanip> 
using namespace std;

const int maxn = 105;
int num[maxn];

bool Do() {
    int n,k;
    if(!(cin >> n >> k))
        return false;

    int p = n / k;

    memset(num,0,sizeof(num));

    for(int i = 1;i <= n;i++) {
        int t;
        cin >> t;
        if(t == 1)
            num[i%k]++;
    }


    int ans = 0;
    for(int i = 0;i < k;i++) {
        ans += min(p- num[i],num[i]);
    }

    cout << ans << endl;

    return true;
}

int main() {
    cin.tie(0);
    cin.sync_with_stdio(false);

    while(Do());
    return 0;
}
发布评论
  • 点击查看/关闭被识别为广告的评论