답안 #1102416

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1102416 2024-10-18T05:59:03 Z xnqs Topical (NOI23_topical) C++17
12 / 100
311 ms 219720 KB
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <utility>
#include <algorithm>
#include <map>

struct Module {
	int n;
	std::vector<int> req;
	std::vector<int> add;

	Module(int n = 0): n(n) {
		req.assign(n, 0);
		add.assign(n, 0);
	}
};

struct TrieNode {
	int idx = -1;
	int pfx_cnt = 0;
	std::map<int, TrieNode*> next;

	~TrieNode() {
		for (auto& [key, val] : next) {
			if (val) {
				delete val;
			}
		}
		next.clear();
	}
};

int x, k;
std::vector<Module> arr;
TrieNode* root = new TrieNode;

void add_word(TrieNode* root, const std::vector<int>& word, int val) {
	for (int i = 0; i < k; i++) {
		if (!root->next[word[i]]) {
			root->next[word[i]] = new TrieNode;
		}

		root = root->next[word[i]];
		++root->pfx_cnt;
	}

	root->idx = val;
}

int search(TrieNode* root, const std::vector<int>& cap, std::vector<int>& add, int pos = 0) {
	if (pos == k) {
		for (int i = 0; i < k; i++) {
			add[i] += arr[root->idx].add[i];
		}
		root->pfx_cnt -= 1;
		root->idx = -1;
		return 1;
	}

	int ret = 0;
	for (const auto& [key, node] : root->next) {
		//std::cout << key << " " << cap[pos] << "\n";
		if (key > cap[pos]) {
			break;
		}
		if (node->pfx_cnt <= 0) {
			continue;
		}

		--node->pfx_cnt;
		ret += search(node, cap, add, pos+1);
	}

	return ret;
}

int main() {
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(NULL);
	std::cout.tie(NULL);

	std::cin >> x >> k;
	arr.reserve(x);
	for (int i = 0; i < x; i++) {
		arr.emplace_back(k);
		for (int j = 0; j < k; j++) {
			std::cin >> arr[i].req[j];
		}
	}
	for (int i = 0; i < x; i++) {
		for (int j = 0; j < k; j++) {
			std::cin >> arr[i].add[j];
		}
	}
	
	for (int i = 0; i < x; i++) {
		add_word(root, arr[i].req, i);
	}

	std::vector<int> cap(k, 0);
	int ans = 0;
	while (1) {
		std::vector<int> add(k, 0);
		int nodes = search(root, cap, add);
		if (!nodes) {
			break;
		}

		ans += nodes;
		for (int i = 0; i < k; i++) {
			cap[i] += add[i];
		}
	}

	std::cout << ans << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 5 ms 1872 KB Output is correct
4 Correct 311 ms 219476 KB Output is correct
5 Correct 303 ms 219720 KB Output is correct
6 Correct 290 ms 219464 KB Output is correct
7 Correct 185 ms 141144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Incorrect 1 ms 336 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Incorrect 12 ms 2640 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 5 ms 1872 KB Output is correct
4 Correct 311 ms 219476 KB Output is correct
5 Correct 303 ms 219720 KB Output is correct
6 Correct 290 ms 219464 KB Output is correct
7 Correct 185 ms 141144 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Incorrect 1 ms 336 KB Output isn't correct
10 Halted 0 ms 0 KB -