Codeforces Round #699 (Div. 2) B New Colony

Codeforces Round #699 (Div. 2) B 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 ℎ1,ℎ2,…,ℎ𝑛, where ℎ𝑖 is the height of the 𝑖-th mountain, and 𝑘 — 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 ℎ𝑖):

if ℎ𝑖≥ℎ𝑖+1, the boulder will roll to the next mountain;

if ℎ𝑖<ℎ𝑖+1, the boulder will stop rolling and increase the mountain height by 1 (ℎ𝑖=ℎ𝑖+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 𝑘 -th boulder or determine that it will fall into the waste collection system.

输入:

The first line contains a single integer 𝑡 (1≤𝑡≤100) — the number of test cases.

Each test case consists of two lines. The first line in each test case contains two integers 𝑛
and 𝑘 (1≤𝑛≤100; 1≤𝑘≤109) — the number of mountains and the number of boulders.

The second line contains 𝑛 integers ℎ1,ℎ2,…,ℎ𝑛 (1≤ℎ𝑖≤100) — the height of the mountains.

It is guaranteed that the sum of 𝑛 over all test cases does not exceed 100.

输出:

For each test case, print −1 if the 𝑘-th boulder will fall into the collection system. Otherwise, print the position of the 𝑘-th boulder.

样例:

输入:

4
4 3
4 1 2 3
2 7
1 8
4 5
4 1 2 3
3 1
5 3 1

输出:

2
1
-1
-1

分析:

经过分析,题意大概就是对于每一做山h增加的条件是h[i] <= h[i - 1] && h[i] < h[i + 1],可以发现这是一个递归的过程,每一次的判断的抉择方式相同,终止条件就是山的边缘即collection system和k == 0,同时也要记录boulder是从哪里滚下来的,因为要输出嘛。

代码:

//b
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>

using namespace std;

int n;
long long k;
long long stand_k = 0;
long long b[200] = {0};
long long q = 0;
long long ans = 0;
int a[200] = {0};
int t;

void func(int x, int be_x)
{
    if(x == n - 1)
    {
        ans = n;
        return;
    }
    if(k == 0)
    {
        ans = be_x + 1;
        return;
    }
    if(a[x] < a[x + 1])
    {
        a[x]++;
        k--;
        func(0,x);
    }
    else
    {
        func(x + 1,x);
    }
}

int main()
{
    cin>>t;

    while(t--)
    {
        memset(a,0,sizeof(a));
        ans = 0;
        cin>>n>>k;
        for(int i = 0;i < n;i++)
        {
            cin>>a[i];
        }
        a[n] = 0x3f3f3f3f;
        func(0,0);
        if(ans == n)
        {
            cout<<-1<<endl;
        }
        else
        {
            cout<<ans<<endl;
        }
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值