• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

Codeforce Round #218 Div2 A

A. K-Periodic Array
time limit per test 1 second
memory limit per test 256 megabytes
 

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 test(s)
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
Note

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.

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <vector>
 4 #include <stack>
 5 #include <cstring>
 6 #include <algorithm>
 7 using namespace std;
 8 #define maxn 1000
 9 #define INF 0x7fffffff
10 int n, m, k, x, y, d,t;
11 int a[maxn][maxn];
12 int gcd(int n, int m){
13     if (m == 0)return n;
14     return gcd(m, n%m);
15 }
16 int main(){
17     scanf("%d%d", &n, &m);
18     for (int i = 0; i < n/m; i++){
19         for (int j = 0; j < m; j++)
20             scanf("%d", &a[i][j]);
21     }
22     int s = 0;
23     for (int i = 0; i < m; i++){
24         x = 0; y = 0;
25         for (int j = 0; j < n/m;j++)
26         if (a[j][i] == 1)x++;
27         else y++;
28         s += min(x,y);
29     }
30     printf("%d\n", s);
31     return 0;
32 }
View Code
posted @ 2013-12-09 12:53  HaibaraAi  阅读(171)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3