题目:
思路:
很简单。对于每一堆,你只需要留下最少的积木就可以了,第i堆积木最少要i个木块才可以保证严格单调递增。如果多了,把多的往背包里塞;如果少了,把从背包里拿出来补充这个堆,若不够补充,则失败。
#include <iostream>
#include <vector>
using namespace std;
int main(){
int n;
int heap_num;
double block_num;
cin >> n;
for (int k = 0; k < n; ++k){
bool flag = true;
cin >> heap_num >> block_num;
vector<double> heap;
for (int i = 0; i < heap_num; ++i){
double t;
cin >> t;
heap.push_back(t);
}
for (int i = 0; i < heap.size(); ++i){
if (heap[i] == i){
continue;
}else if(heap[i] > i){
block_num += heap[i] - i;
heap[i] = i;
}else{
if (block_num >= (i - heap[i])){
block_num -= (i - heap[i]);
heap[i] = i;
}else{
flag = false;
break;
}
}
}
if (flag){
cout << "YES" <<endl;
}else{
cout << "NO" << endl;
}
}
return 0;
}