CF1200B

CF1200B

解法:

贪心。当在第i列时,尽可能多的取走第i列的木块使得袋子里的木块尽可能多

CODE:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>


using namespace std; 

const int N = 1e3 + 5; 

int n,m,k,T,h[N]; 

int main() {
    scanf("%d",&T); 
	while(T--) {
        scanf("%d%d%d",&n,&m,&k);
		for(int i = 1 ; i <= n ; i++)
            scanf("%d",&h[i]);
		if (n == 1) {
			puts("YES"); 
			continue; 
		}
		int pos = 1; 
		for(int i = 2 ; i <= n ; i++) {
			if(h[pos] > h[i]) {
				int t = h[pos] - h[i] + k; 
				if (t > h[pos])
					t = h[pos]; 
				m += t; 
				pos++; 
			} else if (h[i] - h[pos] <= k) {
				int t = k - h[i] + h[pos]; 
				if (t > h[pos])
					t = h[pos]; 
				m += t; 
				pos++; 
			} else if (h[i] - h[pos] <= k + m) {
				m -= h[i] - h[pos] - k; 
				pos++; 
			} else {
				puts("NO");
				break; 
			}
		}
		if (pos == n) puts("YES");
	}
    //system("pause");
	return 0; 
}
posted @ 2019-08-29 20:52  西窗夜雨  阅读(313)  评论(0编辑  收藏  举报