B. Repainting Street
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
There is a street with n houses in a line, numbered from 1 to n. The house i is initially painted in color ci. The street is considered beautiful if all houses are painted in the same color. Tom, the painter, is in charge of making the street beautiful. Tom’s painting capacity is defined by an integer, let’s call it k.
On one day, Tom can do the following repainting process that consists of two steps:
He chooses two integers l and r such that 1≤l≤r≤n and r−l+1=k.
For each house i such that l≤i≤r, he can either repaint it with any color he wants, or ignore it and let it keep its current color.
Note that in the same day Tom can use different colors to repaint different houses.
Tom wants to know the minimum number of days needed to repaint the street so that it becomes beautiful.
Input
The first line of input contains a single integer t (1≤t≤104), the number of test cases. Description of test cases follows.
In the first line of a test case description there are two integers n and k (1≤k≤n≤105).
The second line contains n space-separated integers. The i-th of these integers represents ci (1≤ci≤100), the color which house i is initially painted in.
It is guaranteed that the sum of n over all test cases does not exceed 105.
Output
Print t lines, each with one integer: the minimum number of days Tom needs to make the street beautiful for each test case.
Example
inputCopy
3
10 2
1 1 2 2 1 1 2 2 2 1
7 1
1 2 3 4 5 6 7
10 3
1 3 3 3 3 1 2 1 3 3
outputCopy
3
6
2
Note
In the first test case Tom should paint houses 1 and 2 in the first day in color 2, houses 5 and 6 in the second day in color 2, and the last house in color 2 on the third day.
In the second test case Tom can, for example, spend 6 days to paint houses 1, 2, 4, 5, 6, 7 in color 3.
In the third test case Tom can paint the first house in the first day and houses 6, 7, and 8 in the second day in color 3.
题意:对于一串数字,每次可以修改k个数字,问最少需要多少次操作数,可以使这一串数字变成相同的数字。
题解:对于已经输入进去的数字进行标记,对于每个存在的数字进行循环遍历,是这一串数字变成当前标记的数字所用的数字,一旦有比它所需要的操作数小的话,即进行替换,此时即为最小的操作数。
#include<bits/stdc++.h>
#define N 10005
using namespace std;
int n,k,a[N];
bool z[105];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(z,0,sizeof (z));
cin>>n>>k;
for(int i=1;i<=n;++i)
cin>>a[i],z[a[i]]=1;
int ans=0x3f3f3f3f,temp=0;
for(int i=1;i<=100;++i)
{
if(z[i])
{
temp=0;
for(int j=1;j<=n;++j)
{
if(a[j]!=i)j+=k-1,++temp;
}
if(temp<ans)ans=temp;
}
}
printf("%d\n",ans);
}
return 0;
}