Codeforces #218 (Div. 2) A. K-Periodic Array

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

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 nk (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 print0.

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.


题意是给出n个数,从左到右每k个作为一组,判断最少需要改变几个数使得这n/k组数都相同。
我的想法是用一个二维字符数组来读取n/k块数字。
例如样例一中: 读取后
str[1]=21
str[2]=22
str[3]=21
再依次判断相应列是1个数多还是2的个数多
用s来存储标准模板
对应s为21
最后再遍历二位字符数组和s进行比较,不同就是计数器count++;
因为今天把我的DEV从4.9.9.2更新到5.5
所以做题时很不方便啊,不过好处就是有助于我养成好的代码风格
代码如下:
#include <stdio.h>
int main(void){
	int n, k;
	char s[105];
	while(scanf("%d%d",&n, &k)!=EOF){
		char str[105][105] = {'0'};
		int count1[105] = {0};
		int count2[105] = {0};
		int count = 0;
		getchar ();
		for (int i=0; i<n/k; i++){
			for (int j=0; j<k; j++){
				scanf("%c",&str[i][j]);
				getchar ();
			}
		}
		for (int i=0; i<n/k; i++){
			for (int j=0; j<k; j++){
				if(str[i][j]=='1')
				 count1[j]++;
				else count2[j]++;
			}
		}
		for (int i=0; i<k; i++)
		 if(count1[i]>count2[i])
		  s[i] = '1';
		 else s[i] = '2';
		s[k] = '\0';
		for (int i=0; i<n/k; i++)
		 for (int j=0; j<k; j++)
		  if(str[i][j]==s[j])
		   count++;
		printf("%d\n",count);
	}
} 

刚刚又看了下大神的代码。。。不得不佩服人家代码的简洁
大神的思路是(分析第一个样例):
用一维数组a来存储数,接着按相应列判断1的个数和2的个数,取其中较小值
代码如下:
#include<stdio.h>

int min(int a,int b){
	return a>b?b:a;
}

int a[105];

int main()
{
    int n,k;
    while(scanf("%d%d",&n,&k)+1)
    {
        for(int i=0;i<n;i++) scanf("%d",&a[i]);
        int ans=0;
        for(int i=0;i<k;i++)//判别k次 
        {
            int cnt=0;
            for(int j=i;j<n;j+=k)//判断对应列 
            {
                if(a[j]==1) cnt++;
            }
            ans+=min(cnt,n/k-cnt);//cnt表示1的个数,n/k-cnt表示2的个数,取其中较小值 
        }
        printf("%d\n",ans);
    }
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值