当前位置:网站首页>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;
}
边栏推荐
- [OpenCV] - Find and draw contours
- Talking about the process and how to create it
- Flask框架实现异步处理请求
- 2022-08-08 mysql慢SQL-Q18-10GB数据量-mysql/innodb测试
- 串扰与防护
- 简单的数学公式计算
- LeetCode - remove consecutive nodes with a sum of zero from a linked list
- TASSEL软件导入plink格式文件报错
- 电脑重装系统如何在 Win11查看显卡型号信息
- MySQL: Intent Shared Locks and Intentional Exclusive Locks | Deadlocks | Lock Optimization
猜你喜欢
使用ceph-deploycep集群部署,并用3个磁盘作为专用osd
抖音直播新号怎么起号?抖音直播间不进人怎么办?
人类微生物组和缺失遗传力--读论文
[Server data recovery] A case of data recovery when the Ext4 file system cannot be mounted and an error is reported after fsck
杰理之手机OTG问题【篇】
【周赛复盘】力扣第 305 场单周赛
松柏集(江风起)
ceph create pool, map, delete exercises
松柏集(浮窗思)
必须指定GDAL API版本。提供一个路径使用GDAL_CONFIG gdal-config环境
随机推荐
337. 打家劫舍 III
Efficient review of deep learning DL, CV, NLP
笔记本电脑重装系统后开机蓝屏要怎么办
浅谈进程与其创建方式
阿里云天池大赛赛题(机器学习)——O2O优惠券预测(完整代码)
阿里云天池大赛赛题(机器学习)——工业蒸汽量预测(完整代码)
MySQL:意向共享锁和意向排它锁 | 死锁 | 锁的优化
UI中级操作(倾斜和雷达效果)
查询某时间段获得的积分总积分的大小进行排序
自动化测试-图片中添加文字注释,添加到allure测试报告中
AttributeError: partially initialized module 'cv2' has no attribute 'gapi_wip_gst_GStreamerPipeline'
2022年熔化焊接与热切割考试模拟100题及在线模拟考试
基于ABP和Magicodes实现Excel导出操作
BaseDexClassLoader的正确使用方式
特征工程实战篇
Understanding ML Cross Validation Fast
HyperLynx(四)差分传输线模型
松柏集(江风起)
【二叉树】重建二叉树
Disappearance of heritability - wiki