New Colony

本文介绍了一道编程竞赛题目,涉及到模拟石头滚动过程来平山的算法。题目要求根据给定的山的高度和石头数量,确定最后一个石头会停留在哪座山或者是否会滚落。解题思路是暴力遍历所有可能的情况,通过比较相邻山的高度来判断石头的滚动路径。文章提供了示例输入和输出,并给出了一段Java代码实现。
摘要由CSDN通过智能技术生成

New Colony Codeforces-1481B

2021.05.02 训练题E
**题目大意:**有n座山,每座山的高度为hi,每次从第一座山开始扔石头,若石头所在的山的高度 hi >= h(i+1) 则石头会滚向下一座山,否则就会停留在这座山,这座山的高度自增1,求最后一块石头停留在第几座山,若滚过所有的山,则输出-1.
**思路:**暴力遍历即可
题目:

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
Input
4
4 3
4 1 2 3
2 7
1 8
4 5
4 1 2 3
3 1
5 3 1
Output
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.

代码实现:

import java.util.Scanner;

public class Add_and_Divide {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		while(t--!=0) {
			int a = sc.nextInt();
			int b = sc.nextInt();
			if(a < b) {
				System.out.println(1);
			}else {
				int temp = a;
				int min = Integer.MAX_VALUE;
				int cnt = 0;
				int mark = 0;
				if(b==1) {
					b++;
					mark = 1;
				}
				for(int i=b;;i++) {
					cnt = i-b+mark;
					while(temp != 0) {
						temp /= i;
						cnt++;
					}
					if(cnt <= min) min = cnt;
					else break;
					cnt = 0;
					temp = a;
				}
				System.out.println(min);
			}
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值