原题:
思路:
首先一定要弄懂最小可能的最大木板数(看下面)
相同颜色中间会间隔或不间隔一些板子,找出每种颜色间隔板子数量的第一大值和第二大值,每种颜色的第一大值除以2后再来和第二大值比较,取最大的一个,即得到该种颜色必须跨过的最大木板数,最后在k种颜色中取最小值,即得到必须跨过的最小可能的最大木板数。
AC代码:
#include <bits/stdc++.h>
#define ll long long
#define db double
#define inf 0x3f3f3f3f
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
const int N = 2e5 + 100;
void solve() {
int n, k;
n=read(),k=read();
int x;
vector<vector<int> >v(k + 2), vv(k + 2);
for (int i = 1; i <= k; i++)
v[i].push_back(0);
for (int i = 1; i <= n; i++) {
v[x = read()].push_back(i);
}
for (int i = 1; i <= k; i++) {
v[i].push_back(n + 1);
}
for (int i = 1; i <= k; i++) {
for (int j = 0; j < v[i].size() - 1; j++) {
vv[i].push_back(v[i][j + 1] - v[i][j] - 1);
}
}
int a[k + 3];
for (int i = 1; i <= k; i++) {
sort(vv[i].begin(), vv[i].end());
auto q = vv[i].back();
vv[i].pop_back();
auto qq = vv[i].back();
q /= 2;
if (q > qq)
a[i] = q;
else
a[i] = qq;
}
sort(a + 1, a + k + 1);
cout << a[1];
cout << endl;
}
signed main() {
int t = read();
while (t--) {
solve();
}
}