题目描述:
解题思路:
本题采用贪心的策略,题目说一共有60种颜色,枚举每一种颜色的最小刷漆次数,再进行比较,取最小次数为答案。
刷漆次数思路: 从左到右遍历,颜色相同时,跳过到第一个不相同颜色前一个位置;颜色不同时加上k并将天数加1。(注意不要越界)
题解:
#include<bits/stdc++.h>
using namespace std;
const int N = 1e4 + 9;
int a[N];
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t;cin >> t;
while(t--)//可以是输入一个在输出一个样例,也可以是输入全部样例再输出。不过后者就需要写承接函数,相对麻烦
{
int n; cin >> n;
int res = n;
int k; cin >> k;
for(int i = 1; i <= n; i++)//下标从1开始,建议下标值都这样写,形成习惯
{
cin >> a[i];
}
for(int i = 1; i <= 60; i++)
{
int cnt = 0;//记得每次都要重置
int j = 0;//j要从0开始,为了使第一个能被判断到,a[0 + 1]
while(j < n && a[j + 1] == i)//颜射相同情况,注意j<n而不是等于,因为等于时会a[j+1]将会越界
{
j++;
}
while(j < n)
{
j+=k;
while(j < n && a[j+1] == i)
{
j++;
}
cnt++;
}
res = min(cnt, res);
}
cout << res << "\n";
}
return 0;
}