当前位置:网站首页>B. Arrays Sum
B. Arrays Sum
2022-08-09 04:33:00 【秦小咩】
B. Arrays Sum
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given a non-decreasing array of non-negative integers a1,a2,…,ana1,a2,…,an. Also you are given a positive integer kk.
You want to find mm non-decreasing arrays of non-negative integers b1,b2,…,bmb1,b2,…,bm, such that:
- The size of bibi is equal to nn for all 1≤i≤m1≤i≤m.
- For all 1≤j≤n1≤j≤n, aj=b1,j+b2,j+…+bm,jaj=b1,j+b2,j+…+bm,j. In the other word, array aa is the sum of arrays bibi.
- The number of different elements in the array bibi is at most kk for all 1≤i≤m1≤i≤m.
Find the minimum possible value of mm, or report that there is no possible mm.
Input
The first line contains one integer tt (1≤t≤1001≤t≤100): the number of test cases.
The first line of each test case contains two integers nn, kk (1≤n≤1001≤n≤100, 1≤k≤n1≤k≤n).
The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤a1≤a2≤…≤an≤1000≤a1≤a2≤…≤an≤100, an>0an>0).
Output
For each test case print a single integer: the minimum possible value of mm. If there is no such mm, print −1−1.
Example
input
Copy
6 4 1 0 0 0 1 3 1 3 3 3 11 3 0 1 2 2 3 3 3 4 4 4 4 5 3 1 2 3 4 5 9 4 2 2 3 5 7 11 13 13 17 10 7 0 1 1 2 3 3 4 5 5 6
output
Copy
-1 1 2 2 2 1
Note
In the first test case, there is no possible mm, because all elements of all arrays should be equal to 00. But in this case, it is impossible to get a4=1a4=1 as the sum of zeros.
In the second test case, we can take b1=[3,3,3]b1=[3,3,3]. 11 is the smallest possible value of mm.
In the third test case, we can take b1=[0,1,1,1,2,2,2,2,2,2,2]b1=[0,1,1,1,2,2,2,2,2,2,2] and b2=[0,0,1,1,1,1,1,2,2,2,2]b2=[0,0,1,1,1,1,1,2,2,2,2]. It's easy to see, that ai=b1,i+b2,iai=b1,i+b2,i for all ii and the number of different elements in b1b1 and in b2b2 is equal to 33 (so it is at most 33). It can be proven that 22 is the smallest possible value of mm.
本题并不是多难构造,难在本题题意不易理解
突破点在于a数组是不减的,意味着相同的数字是堆积在一起的,另一个突破点是b数组的长度也要为n,每次我们最多用k种,那么第一次可以用k种,第二次我们在原来k种的位置上全部填上0,在扩展k-1种即可,第三次也是k-1种,这样就能最少的次数了,进行一次特判无解,种类数不是1,但是每次只能用1种的时候,剩下的我们都能构造出来用解的情况,因为每次至少是可以扩展新的一种的
#include<iostream>
# include<algorithm>
# include<unordered_map>
# include<cstring>
using namespace std;
typedef long long int ll;
bool mp[110];
int a[110];
int main ()
{
int t;
cin>>t;
while(t--)
{
int n,k;
cin>>n>>k;
int cnt=0;
memset(mp,0,sizeof(mp));
for(int i=1;i<=n;i++)
{
cin>>a[i];
if(mp[a[i]]==0)
{
mp[a[i]]=1;
cnt++;
}
}
if(k==1&&cnt!=1)
{
cout<<"-1"<<endl;
}
else
{
if(cnt==1)
{
cout<<1<<endl;
continue;
}
cnt--;
int ans=cnt/(k-1);
if(cnt%(k-1))
ans++;
cout<<ans<<endl;
}
}
return 0;
}
边栏推荐
猜你喜欢
How to do the stability test, this article thoroughly explains it!
MySQL: Intent Shared Locks and Intentional Exclusive Locks | Deadlocks | Lock Optimization
软件质效领航者 | 优秀案例•国金证券DevOps建设项目
安装pytorch和cuda
TCP/IP协议中分包与重组原理介绍、分片偏移量的计算方法、IPv4报文格式
[math] dot product and cross product
y91.第六章 微服务、服务网格及Envoy实战 -- 服务网格基础(二)
【服务器数据恢复】Ext4文件系统fsck后mount不上并报错的数据修复案例
杰理之智能充电仓低电发码关机 触摸不开机【篇】
Improve the user experience and add a small detail to your modal popup
随机推荐
2022年安全员-B证考试练习题及在线模拟考试
2022 Security Officer-B Certificate Exam Practice Questions and Online Mock Exam
杰理之智能充电仓低电发码关机 触摸不开机【篇】
HP路由器和交换机日志分析
抖音直播间带货最新玩法和运营技巧
MySQL:已提交读和可重复读的实现原理 | MVCC(多版本并发控制)——笔记自用
[21天学习挑战赛——内核笔记](四)——内核常见调试手段(printf、dump_stack、devmem)
查询某时间段获得的积分总积分的大小进行排序
I.MX6U-ALPHA开发板(高精度定时器)
Ali YunTianChi competition problem (deep learning) - video enhancement (complete code)
ceph create pool, map, delete exercises
XJTUSE Professional Course and Experiment Guide
消失的遗传力--wiki
特征工程实战篇
MySQL:redo log日志——笔记自用
【二叉树】重建二叉树
供应商对接Chewy的EDI需求
Ali YunTianChi competition problem (machine learning) - O2O coupons prediction (complete code)
高效回顾深度学习DL、CV、NLP
pytorch implementation of Poly1CrossEntropyLoss