QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#122450 | #87. Devil's Share | bashkort# | 0 | 0ms | 0kb | C++20 | 2.9kb | 2023-07-10 15:21:47 | 2024-07-04 00:35:52 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int A = 10;
map<array<int, 5>, string> dp, as;
string re;
array<int, 5> freq;
string calc(int k) {
string res;
for (int i = 0; i + k <= size(re); ++i) {
res = max(res, re.substr(i, k));
}
return res;
}
void rec() {
if (!re.empty()) {
for (int k = 1; k <= size(re); ++k) {
freq[4] = k;
string res = calc(k);
if (dp[freq].empty() || dp[freq] > res) {
dp[freq] = res;
as[freq] = re;
}
}
}
for (int x = 1; x <= 4; ++x) {
if (freq[x - 1] < 3) {
++freq[x - 1];
re += char('0' + x);
rec();
--freq[x - 1];
re.pop_back();
}
}
}
void solve() {
int k;
cin >> k;
int d[A]{}, dd[A]{};
for (int i = 1; i < A; ++i) {
cin >> d[i];
dd[i] = d[i];
}
const int n = accumulate(d, d + A, 0);
if (n == 1) {
for (int i = A - 1; i >= 0; --i) {
if (d[i]) {
cout << i << '\n';
return;
}
}
}
vector<int> ans(n);
if (k == 2) {
ans[n - 1] = A - 1;
while (!d[ans.back()]) {
--ans.back();
}
--d[ans.back()];
int x = ans.back();
while (!d[x]) {
--x;
}
int p = n - 2;
for (int i = 1; i < x; ++i) {
while (d[i]--) {
ans[p--] = i;
if (d[x] > 0) {
ans[p--] = x;
--d[x];
}
}
}
while (d[x]--) {
ans[p--] = x;
}
} else {
array<int, 5> freq{d[1], d[2], d[3], d[4], k};
cout << as[freq] << '\n';
return;
int x = 2;
for (int i = 1; i < k; ++i) {
ans[n - i] = x;
if (--d[x] == 0) {
x -= 1;
}
}
if (d[2] == 0) {
for (int i = k; i <= n; ++i) {
ans[n - i] = 1;
}
} else {
int cnt = d[2];
int f = d[1] / cnt, p = 0;
for (int i = 0; i < cnt; ++i) {
ans[p++] = 2;
int s = i + 1 == cnt ? d[1] - f * (cnt - 1) : f;
for (int t = 0; t < s; ++t) {
ans[p++] = 1;
}
}
}
}
for (int i = 0; i < n; ++i) {
cout << ans[i];
--dd[ans[i]];
}
for (int x : dd) {
assert(x == 0);
}
cout << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
rec();
int test = 1;
cin >> test;
while (test--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Time Limit Exceeded
Test #1:
score: 0
Time Limit Exceeded
input:
1536 4 2 1 2 2 0 0 0 0 0 2 3 2 3 3 0 0 0 0 0 3 1 2 0 3 0 0 0 0 0 4 2 2 3 2 0 0 0 0 0 1 3 3 2 2 0 0 0 0 0 3 1 2 2 0 0 0 0 0 0 3 2 1 2 3 0 0 0 0 0 6 1 3 3 0 0 0 0 0 0 4 1 0 1 2 0 0 0 0 0 4 2 1 2 3 0 0 0 0 0 4 2 3 0 2 0 0 0 0 0 5 3 2 3 3 0 0 0 0 0 3 2 2 1 1 0 0 0 0 0 3 2 3 2 0 0 0 0 0 0 8 1 3 1 3 0 0 0...
output:
result:
Subtask #2:
score: 0
Time Limit Exceeded
Test #2:
score: 0
Time Limit Exceeded
input:
35960 2 0 0 5 2 0 0 17 0 7 2 0 6 0 15 0 0 0 4 5 2 3 0 0 1 20 0 0 0 8 2 0 5 0 0 15 0 5 7 0 2 0 0 2 11 0 0 4 0 10 2 0 14 0 0 11 0 0 6 1 2 0 0 10 3 0 0 8 0 1 2 0 1 9 0 2 0 0 6 0 2 0 0 0 0 5 0 12 7 3 2 0 0 5 0 0 2 0 8 9 2 7 2 0 0 0 0 0 6 8 2 0 0 0 4 1 0 3 18 0 2 0 0 14 4 8 0 0 0 1 2 0 2 0 0 0 13 3 9 0 2...
output:
result:
Subtask #3:
score: 0
Time Limit Exceeded
Test #11:
score: 0
Time Limit Exceeded
input:
26488 21 7 19 0 0 0 0 0 0 0 3 15 21 0 0 0 0 0 0 0 7 4 35 0 0 0 0 0 0 0 5 28 12 0 0 0 0 0 0 0 22 40 3 0 0 0 0 0 0 0 1 7 6 0 0 0 0 0 0 0 5 12 21 0 0 0 0 0 0 0 18 27 13 0 0 0 0 0 0 0 2 36 6 0 0 0 0 0 0 0 15 19 14 0 0 0 0 0 0 0 34 17 20 0 0 0 0 0 0 0 11 17 5 0 0 0 0 0 0 0 19 10 12 0 0 0 0 0 0 0 28 29 9 ...
output:
result:
Subtask #4:
score: 0
Skipped
Dependency #1:
0%