제출 #238312

#제출 시각아이디문제언어결과실행 시간메모리
238312tfgDevil's Share (RMI19_devil)C++17
27 / 100
679 ms262144 KiB
#include <iostream>
#include <vector>
#include <chrono>
#include <random>
#include <cassert>
#include <algorithm>

std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());

std::vector<int> solve(std::vector<int> a) {
	if(a[0] == a.back()) return a;
	//for(auto x : a) std::cout << x << ' ';
	//std::cout << std::endl;
	int sz = 0;
	int mx = a.back();
	while(a.back() == mx) {
		sz++;
		a.pop_back();
	}
	std::vector<std::vector<int>> b(sz, std::vector<int>(1, mx));
	std::vector<int> wtf(sz, 0), inv(1, 0);
	int pt = 0, c = 1;
	std::reverse(a.begin(), a.end());
	while(!a.empty()) {
		for(int l = pt, r = pt; l < sz; l = r) {
			if(a.empty()) {
				break;
			}
			int got = a.back();
			while(r < sz && !a.empty() && a.back() == got) {
				b[r++].push_back(got);
				a.pop_back();
			}
			if(r != sz) {
				for(int i = l; i < r; i++) {
					wtf[i] = c;
				}
				c++;
				inv.push_back(l);
				pt = r;
			}
		}
	}
	for(int i = pt; i < sz; i++) {
		wtf[i] = c;
	}
	c++;
	inv.push_back(pt);
	auto ha = solve(wtf);
	std::vector<int> ans;
	for(auto id : ha) {
		for(auto x : b[inv[id]]) {
			ans.push_back(x);
		}
	}
	//for(auto x : ans) std::cout << x << ' ';
	//std::cout << std::endl;
	return ans;
}

void solve() {
	int k;
	std::vector<int> a;
	std::cin >> k;
	for(int i = 1; i <= 9; i++) {
		int x;
		std::cin >> x;
		while(x--) {
			a.push_back(i);
		}
	}
	std::string end;
	for(int i = 1; i < k; i++) {
		end += char('0' + a.back());
		a.pop_back();
	}
	a = solve(a);
	for(auto x : a) std::cout << x;
	std::reverse(end.begin(), end.end());
	std::cout << end << '\n';
}

int main() {
	std::ios_base::sync_with_stdio(false); std::cin.tie(NULL);
	int t;
	std::cin >> t;
	while(t--) {
		solve();
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...