Google Kick Start 2021 Round G - C (Banana Bunches)

Dongjae Lee·2021년 10월 20일
0
#include <bits/stdc++.h>
using namespace std;

int main() {
    int T;
	scanf("%d", &T);
	
	for(int tc = 1; tc <= T; tc++) {
		int N;
		unsigned long long K;
		scanf("%d %llu", &N, &K);
		
		vector<unsigned long long> banana;
		for(int i = 0; i < N; i++) {
			unsigned long long temp;
			scanf("%llu", &temp);
			banana.push_back(temp);
		}
		
		vector<unsigned long long> sum_banana;
		sum_banana.push_back(0);
		for(int i = 0; i < N; i++)
			sum_banana.push_back(sum_banana[i]+banana[i]);
		
		int result = N+1;
		
		vector<int> right(K+1,N+1);
		for(int i = N-1; i >= 0; i--) {
			for(int j = i; j >= 0; j--) {
				unsigned long long temp = sum_banana[i+1]-sum_banana[j];
				if(temp > K) break;
				if(temp == K && result > i-j+1)
					result = i-j+1;
				else if(right[K-temp] <= N) {
					if(result > i-j+1+right[K-temp])
						result = i-j+1+right[K-temp];
				}
			}
			for(int j = i; j < N; j++) {
				unsigned long long temp = sum_banana[j+1]-sum_banana[i];
				if(temp > K) break;
				if(j-i+1 < right[temp])
					right[temp] = j-i+1;
			}
		}
		if(result == N+1) printf("Case #%d: -1\n", tc);
		else printf("Case #%d: %d\n", tc, result);
	}
}
profile
Hello Everything!

0개의 댓글