1481B New Colony (暴力)

题目

看到题目中山的数量最多只有100,那么我们直接暴力模拟O(N^2),对于每一个石块都从第一座山放下去,如果发现有前面的山高于自己则将自己所在的山高度加一,再开始下一块石块,如果有一块石块在这个过程中没发现有比当前山高的顺利度过了所有山那么直接输出-1,结束。如果当到了最后一块石块,则记录最后一个填补的位置,输出即可。

#include<iostream>
#include<memory.h>
using namespace std;
typedef long long ll;
const int Max = 1e3 + 5;
int h[Max];

int main()
{
    
    
	int t;cin >> t;
	while (t--)
	{
    
    
		int n, k;cin >> n >> k;
		memset(h, 0, sizeof(h));
		for (int i = 1;i <= n;i++)cin >> h[i];
		int f = 0, d = 0;
		for (int i = 1;i <= k;i++)
		{
    
    
			for (int j = 1;j <= n;j++)
			{
    
    
				if (h[j] < h[j + 1]) {
    
    
					h[j]++;d = j;break;
				}
				if (j == n) f = 1;
			}
			if (f){
    
    cout << -1 << endl;break;}
			if (i == k) cout << d << endl;
		}
	}
}

猜你喜欢

转载自blog.csdn.net/asbbv/article/details/113706416