CF 1481B New Colony 题解
题目传送门
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you).
You are given an array h1,h2,…,hn, where hi is the height of the i-th mountain, and k — the number of boulders you have.
You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is hi):
if hi≥hi+1, the boulder will roll to the next mountain;
if hi<hi+1, the boulder will stop rolling and increase the mountain height by 1 (hi=hi+1);
if the boulder reaches the last mountain it will fall to the waste collection system and disappear.
You want to find the position of the k-th boulder or determine that it will fall into the waste collection system.
Input
The first line contains a single integer t (1≤t≤100) — the number of test cases.
Each test case consists of two lines. The first line in each test case contains two integers n and k (1≤n≤100; 1≤k≤109) — the number of mountains and the number of boulders.
The second line contains n integers h1,h2,…,hn (1≤hi≤100) — the height of the mountains.
It is guaranteed that the sum of n over all test cases does not exceed 100.
Output
For each test case, print −1 if the k-th boulder will fall into the collection system. Otherwise, print the position of the k-th boulder.
Example
inputCopy
4
4 3
4 1 2 3
2 7
1 8
4 5
4 1 2 3
3 1
5 3 1
outputCopy
2
1
-1
-1
Note
Let's simulate the first case:
The first boulder starts at i=1; since h1≥h2 it rolls to i=2 and stops there because h2<h3.
The new heights are [4,2,2,3].
The second boulder starts at i=1; since h1≥h2 the boulder rolls to i=2; since h2≥h3 the boulder rolls to i=3 and stops there because h3<h4.
The new heights are [4,2,3,3].
The third boulder starts at i=1; since h1≥h2 it rolls to i=2 and stops there because h2<h3.
The new heights are [4,3,3,3].
The positions where each boulder stopped are the following: [2,3,2].
In the second case, all 7 boulders will stop right at the first mountain rising its height from 1 to 8.
The third case is similar to the first one but now you'll throw 5 boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to [4,3,3,3], that's why the other two boulders will fall into the collection system.
In the fourth case, the first and only boulders will fall straight into the collection system.
题目解析
首先看一下数据范围:
我们发现这个 很大,貌似不能直接膜你。
但是我们仔细算一下:
最坏情况下时,我们发现,我们最多只要推 次就可以将所有的坑填平,并且不难得出,如果第 个滚到最后的话,第 个肯定也是滚到最后的。
直接模拟就可以了。
#include<cstdio> #include<cstring> #define maxn 139 using namespace std; //#define debug typedef int Type; inline Type read(){ Type sum=0; int flag=0; char c=getchar(); while((c<'0'||c>'9')&&c!='-') c=getchar(); if(c=='-') c=getchar(),flag=1; while('0'<=c&&c<='9'){ sum=(sum<<1)+(sum<<3)+(c^48); c=getchar(); } if(flag) return -sum; return sum; } int T,n,k; int a[maxn]; int js(){ int ans; for(int i=1;i<=k;i++){ int now=1; for(now=1;now<=n;now++) if(a[now]<a[now+1]){ a[now]++; ans=now; break; } else if(now==n) return -1; } return ans; } int main(){ //freopen("1.in","r",stdin); T=read(); while(T--){ memset(a,0,sizeof(a)); n=read(); k=read(); for(int i=1;i<=n;i++) a[i]=read(); printf("%d\n",js()); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具