QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#122654 | #87. Devil's Share | somethingnew# | 0 | 0ms | 0kb | C++20 | 3.9kb | 2023-07-10 21:20:45 | 2024-07-04 00:36:09 |
Judging History
answer
// ↘ ⬇ ⬇ ⬇ ⬇ ⬇ ↙
// ➡ @roadfromroi ⬅
// ↗ ⬆ ⬆ ⬆ ⬆ ⬆ ↖
#include <iostream>
#include "vector"
#include "algorithm"
#include "numeric"
#include "climits"
#include "iomanip"
#include "bitset"
#include "cmath"
#include "map"
#include "deque"
#include "array"
#include "set"
#define all(x) x.begin(), x.end()
using namespace std;
struct dsu{
vector<int> nxt;
void init(int n) {
nxt.assign(n, {});
for (int i = 0; i < nxt.size(); ++i) {
nxt[i] = i;
}
}
void plsone() {
nxt.push_back(0);
nxt.back() = nxt.size() - 1;
}
int getv(int v) {
if (nxt[v] == v)
return v;
return nxt[v] = getv(nxt[v]);
}
void pop(int v) {
nxt[v] = v + 1;
}
};
void recstr(int v, vector<pair<pair<int, int>, int>> &a, string &s) {
if (v < 9) {
s.push_back('1' + v);
return;
}
recstr(a[v].first.first, a, s);
recstr(a[v].first.second, a, s);
}
void solve() {
int k;
cin >> k;
int sm = 0;
vector<pair<pair<int, int>, int>> a(9);
string s1, s2;
for (auto &i : a) {
cin >> i.second;
}
string sbk;
int peppa = k - 1;
for (int i = 8; i >= 0; --i) {
while (a[i].second and peppa) {
peppa--;
a[i].second--;
sbk.push_back(i + '1');
}
}
reverse(all(sbk));
int mx = -1;
int nmx = 0;
int nmxhelp = 0;
for (int i = 0; i < 9; ++i) {
if (a[i].second)
mx = i;
}
for (int i = 0; i < 9; ++i) {
//if (a[i].second and i != mx)
nmxhelp += a[i].second;
}
dsu nxt;
nxt.init(9);
while (nmxhelp != a[mx].second) {
nmx = nmxhelp - a[mx].second;
int c = a[mx].second;
int kt = (c + nmx - 1) / nmx;
int mx2cnt = mx % kt, mx2 = -1;
if (kt > 1) {
int rmx = mx;
for (int i = 0; i < kt - 1; ++i) {
if (i + 1 == kt - 1)
mx2 = rmx;
a.push_back({{rmx, mx}, 0});
nxt.plsone();
rmx = a.size() - 1;
}
a[rmx].second = (a[mx].second + kt - 1) / kt - mx2cnt;
nmxhelp += a[rmx].second;
nmxhelp -= a[mx].second;
a[mx].second = 0;
mx = rmx;
}
int v = nxt.getv(0);
vector<pair<pair<int, int>, int>> p1, p2;
while (a[mx].second) {
while (a[v].second == 0) {
nxt.pop(v);
v = nxt.getv(v);
}
int stepa = min(a[mx].second, a[v].second);
a[mx].second -= stepa;
a[v].second -= stepa;
p1.push_back({{mx, v}, stepa});
nmxhelp -= stepa;
}
if (mx2cnt) {
a[mx2].second = mx2cnt;
nmxhelp += mx2cnt;
while (a[mx2].second) {
while (a[v].second == 0) {
nxt.pop(v);
v = nxt.getv(v);
}
int stepa = min(a[mx2].second, a[v].second);
a[mx2].second -= stepa;
a[v].second -= stepa;
p2.push_back({{mx2, v}, stepa});
nmxhelp -= stepa;
}
}
for (auto i : p2) {
a.push_back(i);
nxt.plsone();
}
for (auto i : p1) {
a.push_back(i);
mx = a.size() - 1;
nxt.plsone();
}
}
string sres;
recstr(mx, a, sres);
string sres2;
for (int i = 0; i < a[mx].second; ++i) {
sres2 += sres;
}
sres2 += sbk;
cout << sres2 << '\n';
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
}
/*
1
7
2 4 2 0 0 6 2 2 2
*/
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Runtime Error
Test #1:
score: 0
Runtime Error
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
Runtime Error
Test #2:
score: 0
Runtime Error
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
Runtime Error
Test #11:
score: 0
Runtime Error
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%