codeforce日记 371A
2013-12-24 20:50
225 查看
题目:
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 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 print0.
Sample test(s)
input
output
input
output
input
output
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.
解题思路:
对于每个周期为k的数组来说 a1,=a(1+i*k)。所以对这样的序列来说记录2和1的个数,其差值就是需要改变的最小数目。依次类推得到a2……ak需要改变的最小数目,求和即可。
AC代码:
#include <iostream>;
using namespace std;
int main(){
int n,k;
int a[100];
int numof2=0,numof1=0;
int change=0;
cin>>n>>k;
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<k;i++){
for(int j=0;j<n/k;j++){
if(a[i+j*k]==1){
numof1++;
}
else
{
numof2++;
}
}
change+=min(numof2,numof1);
numof1=0;
numof2=0;
}
cout<<change;
}
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 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 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.
解题思路:
对于每个周期为k的数组来说 a1,=a(1+i*k)。所以对这样的序列来说记录2和1的个数,其差值就是需要改变的最小数目。依次类推得到a2……ak需要改变的最小数目,求和即可。
AC代码:
#include <iostream>;
using namespace std;
int main(){
int n,k;
int a[100];
int numof2=0,numof1=0;
int change=0;
cin>>n>>k;
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<k;i++){
for(int j=0;j<n/k;j++){
if(a[i+j*k]==1){
numof1++;
}
else
{
numof2++;
}
}
change+=min(numof2,numof1);
numof1=0;
numof2=0;
}
cout<<change;
}
相关文章推荐
- Codeforce 429B(递推)
- CodeForce 448C 木片填涂问题
- Codeforce 338 C. Divisor Tree(爆搜,3级)
- CodeForce 534B Covered Path(简单推理)
- Codeforce 128B:String
- code force 401B. Game of Credit Cards
- codeforce C - Vasya and Robot
- codeforceB. Mishka and trip
- Codeforce Round #211 Div2
- codeforce No to Palindromes!(枚举)
- CodeForce 89A.Robbery
- codeforce 493B. Vasya and Wrestling(多重条件比较)
- codeforce 766D Mahmoud and a Dictionary 带权并查集
- Codeforce 609 C—— Load Balancing ——————【想法题】
- Codeforce # A Plug-in
- Codeforce GYM 100741 A. Queries
- CodeForce 396A On Number of Decompositions into Multipliers
- codeforce 796 c Bank Hacking (思维题,分类讨论)
- codeforce 789 C Functions again (dp)
- codeforce-404D---Minesweeper 1D(dp)