A. K-Periodic Array----思维
来源:互联网 发布:供销大数据集团多少人 编辑:程序博客网 时间:2024/06/08 18:52
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.
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.
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.
6 22 1 2 2 2 1
1
8 41 1 2 1 1 1 2 1
0
9 32 1 1 1 2 1 1 1 2
3
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.
题目链接:http://codeforces.com/contest/371/problem/A
题目的意思是说给你n个数,只有1和2,现在k个为一组,问你最少改变几个数,可以使k组里面的数都相同。
思路很简单,我们把k组排成一个二维数组,只需要把答案加上每一列1和2比较少的个数即可。
代码:
#include <cstdio>#include <cstring>#include <iostream>using namespace std;int a[10000];int main(){ int n,k; scanf("%d%d",&n,&k); for(int i=0;i<n;i++){ scanf("%d",&a[i]); } int ans=0; for(int i=0;i<k;i++){ int cnt=0; for(int j=i;j<n;j+=k){ if(a[j]==1) cnt++; } ans+=min(cnt,n/k-cnt); } cout<<ans<<endl; return 0;}
- A. K-Periodic Array----思维
- A. K-Periodic Array
- CF 371A - K-Periodic Array
- Codeforces #218 (Div. 2) A. K-Periodic Array
- Sort a K sorted array
- 【CodeForces】300A - Array(思维)
- 【codeforces】A. Array(思维水题)
- A. Levko and Array Recovery----思维题
- [思维]K
- codeforces 181.div2 300A --Array 思维问题
- CodeForces 360A - Levko and Array Recovery (思维)
- CodeForces 660 A. Co-prime Array【互质+思维】
- Codeforces 660A:Co-prime Array(水题+思维)
- CodeForces 274A k-Multiple Free Set【思维】
- hdoj 5587 Array 【思维】
- Array 【思维】水
- Codeforces Round #390(Div. 2)A. Lesha and array splitting【思维】
- Codeforces Round #424 (Div. 2) A Unimodal Array 思维题
- 把jsp+servlet+mysql项目发布到腾讯云服务器,允许外网访问
- git的介绍
- hdu--6121:Build a tree
- caffe中 solver.prototxt文件
- POJ 2823 Sliding Window (单调队列)
- A. K-Periodic Array----思维
- Python高级
- 剑指OFFER--二进制中1的个数
- c# RichTextBox用法——设置指定字符串的颜色
- 函数指针与回调函数的用法
- c++virtual的动态调用验证
- 线程状态及属性
- struts2--struts2标签库(部分)
- 【并查集入门专题1】poj1182【带权值的并查集】【思维思维~~】