CF1481B New Colony

B. New Colony

本题我在 14min 时通过,当时只有 647 人通过。

本题 \(k\) 很大,其实是个纸老虎。我们想最简单的,如果有一次我的石头滚到了 \(n+1\) 处,那么下一个,还会滚到 \(n+1\) 处,因此我们只需要一直模拟,直到有石头滚到 \(n+1\) 时,退出循环,答案为 \(-1\)。若 \(k\) 次前,没有滚到 \(n+1\) 处,把模拟得出的答案输出即可,可以证明,时间复杂度为 \(O(\sum hn)\)

//Don't act like a loser.
//This code is written by huayucaiji
//You can only use the code for studying or finding mistakes
//Or,you'll be punished by Sakyamuni!!!
#include<bits/stdc++.h>
#define int long long
using namespace std;

int read() {
	char ch=getchar();
	int f=1,x=0;
	while(ch<'0'||ch>'9') {
		if(ch=='-')
			f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9') {
		x=x*10+ch-'0';
		ch=getchar();
	}
	return f*x;
}

const int MAXN=100+10;

int n,h[MAXN],k;

signed main() {
	int t=read();
	while(t--) {
		n=read();k=read();
		for(int i=1;i<=n;i++) {
			h[i]=read();
		}
		h[n+1]=0;
		
		bool flag=1;
		int pos;
		while(k--&&flag) {
			pos=1;
			while(pos<=n&&h[pos]>=h[pos+1]) {
				pos++;
			}
			h[pos]++;
			if(pos==n+1) {
				flag=0;
				pos=-1;
			}
		}
		cout<<pos<<endl;
	}
	return 0;
}


posted @ 2021-02-06 15:15  huayucaiji  阅读(125)  评论(0编辑  收藏  举报