题目链接
题意:
给你一个长度为n的a数组,问你这个数组最少能由几个只含k个不同数的b数组对应位置相加的得到。
思路:
第一个b数组可以得到a中前k个不同的数,但是从第一个b数组之后的数组必须有一个数为0来让前面已经变过的数保持不变,所以后面的每个b数组只能变出k-1个不同的数,所以我们算出a中不同的数的个数之后再用刚刚的方法算出结果即可。
代码:
#include<bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define ll long long
#define pii pair<int,int>
#define fi first
#define ls x*2
#define rs x*2+1
const int N=2e5+7;
const int M=2e4+5;
const double eps=1e-8;
const int mod=1e9+7;
const int inf=0x7fffffff;
const int INF=0x3f3f3f3f;
const double pi=3.1415926;
using namespace std;
char *fs,*ft,buf[1<<20];
#define gc() (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<20,stdin),fs==ft))?0:*fs++;
inline int read()
{int x=0,f=1;char ch=gc();while(ch<'0'||ch>'9'){if(ch=='-'){f=-1;}
ch=gc();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=gc();}return x*f;}
int a[N],b[N],c[N];
signed main()
{
IOS;
int T;
cin>>T;
while(T--)
{
int n,k,ans=1;
cin>>n>>k;
for(int i=0;i<n;i++)
{
cin>>a[i];
if(i>0&&a[i]!=a[i-1])
{
ans++;
}
}
int x=ans/k;
if(ans==1)
{
cout<<1<<endl;
}
else
{
if(k==1)
{
cout<<-1<<endl;
}
else
{
if(ans>k)
{
int x=(ans-k)/(k-1);
if(x*(k-1)==ans-k)
{
cout<<x+1<<endl;
}
else
{
cout<<x+2<<endl;
}
}
else
{
cout<<1<<endl;
}
}
}
}
return 0;
}