답안 #205736

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
205736 2020-02-29T15:50:12 Z ics0503 Kitchen (BOI19_kitchen) C++17
100 / 100
162 ms 28024 KB
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;

short D[313][91919];
vector<int>Q[313];
vector<int>V[91919];
int A[313], B[313], S[313], tmp[313];
bool simul(vector<int>B, int n, int k) {
	priority_queue<int>H;
	int bSum = 0;
	for (int v : B) H.push(v);
	for (int i = n; i >= 1; i--) {
		// int g = A[i] - k;
		for (int j = 0; j < k; j++) {
			tmp[j] = H.top() - 1; H.pop();
			if (tmp[j] < 0)
				return false;
		}
		for (int j = 0; j < k; j++)H.push(tmp[j]);
	}
	return true;
}
bool sort_a(int a, int b) {
	return a < b;
}
int main() {
	int n, m, k, i, j, aSum = 0; scanf("%d%d%d", &n, &m, &k);
	for (i = 1; i <= n; i++) {
		scanf("%d", &A[i]), aSum += A[i];
		if (A[i] < k) {
			puts("Impossible");
			return 0;
		}
	}
	for (i = 1; i <= m; i++) scanf("%d", &B[i]);
	sort(A + 1, A + 1 + n);
	sort(B + 1, B + 1 + m, sort_a);
	for (i = 1; i <= m; i++)S[i] = S[i - 1] + B[i];
	D[0][0] = 0;
	for (i = 1; i <= S[1]; i++)D[0][i] = -1;
	for (i = 1; i <= m; i++) {
		for (j = 0; j <= S[i]; j++) {
			D[i][j] = D[i - 1][j];
			if (B[i] <= j && D[i - 1][j - B[i]] != -1) {
				if (D[i][j] == -1 || D[i][j] < D[i-1][j - B[i]] + 1) {
					D[i][j] = D[i - 1][j - B[i]] + 1;
				}
			}
		}
		for (j = S[i] + 1; j < S[i + 1]; j++)D[i][j] = -1;
	}
	int ans = -1;
	for (i = aSum; i <= S[m]; i++) for (int v = 1; v <= m; v++) if (D[v][i] >= k && V[i].empty()) {
		Q[D[v][i]].push_back(i);
		int now = i;
		for (j = v; j > 0; j--) {
			if (now >= B[j] && D[j][now] == D[j - 1][now - B[j]] + 1) {
				V[i].push_back(B[j]);
				now -= B[j];
			}
		}
		if (simul(V[i], n, k)) {
			printf("%d\n", i - aSum);
			return 0;
		}
	}
	printf("Impossible");
	return 0;
}

Compilation message

kitchen.cpp: In function 'bool simul(std::vector<int>, int, int)':
kitchen.cpp:13:6: warning: unused variable 'bSum' [-Wunused-variable]
  int bSum = 0;
      ^~~~
kitchen.cpp: In function 'int main()':
kitchen.cpp:55:6: warning: unused variable 'ans' [-Wunused-variable]
  int ans = -1;
      ^~~
kitchen.cpp:30:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  int n, m, k, i, j, aSum = 0; scanf("%d%d%d", &n, &m, &k);
                               ~~~~~^~~~~~~~~~~~~~~~~~~~~~
kitchen.cpp:32:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &A[i]), aSum += A[i];
   ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
kitchen.cpp:38:32: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for (i = 1; i <= m; i++) scanf("%d", &B[i]);
                           ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 2424 KB Output is correct
2 Correct 6 ms 2424 KB Output is correct
3 Correct 6 ms 2424 KB Output is correct
4 Correct 6 ms 2424 KB Output is correct
5 Correct 6 ms 2552 KB Output is correct
6 Correct 6 ms 2424 KB Output is correct
7 Correct 7 ms 2424 KB Output is correct
8 Correct 6 ms 2552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 2424 KB Output is correct
2 Correct 6 ms 2424 KB Output is correct
3 Correct 6 ms 2424 KB Output is correct
4 Correct 6 ms 2424 KB Output is correct
5 Correct 6 ms 2552 KB Output is correct
6 Correct 6 ms 2424 KB Output is correct
7 Correct 7 ms 2424 KB Output is correct
8 Correct 6 ms 2552 KB Output is correct
9 Correct 6 ms 2552 KB Output is correct
10 Correct 6 ms 2552 KB Output is correct
11 Correct 6 ms 2552 KB Output is correct
12 Correct 7 ms 2552 KB Output is correct
13 Correct 6 ms 2552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 20472 KB Output is correct
2 Correct 24 ms 16120 KB Output is correct
3 Correct 21 ms 13688 KB Output is correct
4 Correct 41 ms 27384 KB Output is correct
5 Correct 40 ms 28024 KB Output is correct
6 Correct 20 ms 13176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 2680 KB Output is correct
2 Correct 7 ms 2684 KB Output is correct
3 Correct 6 ms 2680 KB Output is correct
4 Correct 6 ms 2680 KB Output is correct
5 Correct 6 ms 2428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 2424 KB Output is correct
2 Correct 6 ms 2424 KB Output is correct
3 Correct 6 ms 2424 KB Output is correct
4 Correct 6 ms 2424 KB Output is correct
5 Correct 6 ms 2552 KB Output is correct
6 Correct 6 ms 2424 KB Output is correct
7 Correct 7 ms 2424 KB Output is correct
8 Correct 6 ms 2552 KB Output is correct
9 Correct 6 ms 2552 KB Output is correct
10 Correct 6 ms 2552 KB Output is correct
11 Correct 6 ms 2552 KB Output is correct
12 Correct 7 ms 2552 KB Output is correct
13 Correct 6 ms 2552 KB Output is correct
14 Correct 29 ms 20472 KB Output is correct
15 Correct 24 ms 16120 KB Output is correct
16 Correct 21 ms 13688 KB Output is correct
17 Correct 41 ms 27384 KB Output is correct
18 Correct 40 ms 28024 KB Output is correct
19 Correct 20 ms 13176 KB Output is correct
20 Correct 7 ms 2680 KB Output is correct
21 Correct 7 ms 2684 KB Output is correct
22 Correct 6 ms 2680 KB Output is correct
23 Correct 6 ms 2680 KB Output is correct
24 Correct 6 ms 2428 KB Output is correct
25 Correct 31 ms 15480 KB Output is correct
26 Correct 49 ms 17528 KB Output is correct
27 Correct 162 ms 7032 KB Output is correct
28 Correct 24 ms 14712 KB Output is correct
29 Correct 35 ms 20472 KB Output is correct
30 Correct 45 ms 28000 KB Output is correct