QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#740563 | #7940. Impossible Numbers | ucup-team3215 | WA | 238ms | 4424kb | C++23 | 4.9kb | 2024-11-13 10:31:58 | 2024-11-13 10:32:00 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
constexpr int K = 10;
constexpr int nax = 1 << K;
struct GenNumber {
int n;
vector<int> now;
array<int, K> cnt{};
GenNumber() = default;
GenNumber(const GenNumber &) = default;
GenNumber(int n, array<int, K> cnt) : n(n), now(n), cnt(cnt) {
now[0] = 1;
}
vector<int> get() {
vector<int> res;
gen(0, res);
return res;
}
bool gen(int pos, vector<int> &v) {
if (pos == n) {
return true;
}
for (; now[pos] < K; ++now[pos]) {
if (cnt[now[pos]]) {
--cnt[now[pos]];
v.push_back(now[pos]);
auto ans = gen(pos + 1, v);
++cnt[now[pos]];
if (pos + 1 == n)++now[pos];
if (ans)return true;
else v.pop_back();
}
}
now[pos] = 0;
return false;
}
};
vector<int> mn(array<int, K> cnt) {
int n = accumulate(cnt.begin(), cnt.end(), 0);
vector<int> res;
auto gen = [&](auto &&gen, int pos) -> void {
if (pos == n) {
return;
}
for (int i = pos == 0; i < K; ++i) {
if (cnt[i]) {
--cnt[i];
res.push_back(i);
gen(gen, pos + 1);
}
}
};
gen(gen, 0);
return res;
}
struct GenDistribution {
int n;
int last{-1};
array<int, K> now{};
array<char, K> ok;
array<char, K> uninit;
GenDistribution(int n, array<char, K> ok) : n(n), ok(ok) {
int first = find(ok.begin(), ok.end(), 1) - ok.begin();
now[first] = n;
for (int i = 0; i < K; ++i)if (ok[i])last = i;
}
array<int, K> get() {
fill(uninit.begin(), uninit.end(), 0);
array<int, K> res{};
gen(0, n, res);
return res;
}
bool gen(int pos, int s, array<int, K> &v) {
if (pos == K) {
return !s;
}
if (uninit[pos])now[pos] = ok[pos] ? s : 0;
for (; now[pos] >= (pos == last ? s : 0); --now[pos]) {
v[pos] = now[pos];
auto ans = gen(pos + 1, s - now[pos], v);
if (pos + 1 == K)--now[pos];
if (ans)return true;
v[pos] = 0;
}
uninit[pos] = 1;
return false;
}
};
int main() {
cin.tie(0)->sync_with_stdio(false);
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < 6; ++j) {
int x;
cin >> x;
a[i] |= 1 << x;
}
}
vector<GenDistribution> g;
for (int mask = 0; mask < nax; ++mask) {
int num = 0;
for (int i = 0; i < n; ++i) {
if (a[i] & mask)++num;
}
++num;
array<char, K> ok{};
for (int j = 0; j < K; ++j)ok[j] = (mask >> j) & 1;
g.emplace_back(num, ok);
}
auto cmp = [&](const vector<int> &lhs, const vector<int> &rhs) {
return lhs.size() < rhs.size() || lhs.size() == rhs.size() && lhs < rhs ? true : false;
};
auto cmpf = [&](const pair<array<int, K>, int> &lhs, const pair<array<int, K>, int> &rhs) {
return lhs.first != rhs.first ? cmp(mn(lhs.first), mn(rhs.first)) : lhs.second < rhs.second;
};
set<pair<array<int, K>, int>, decltype(cmpf)> s(cmpf);
for (int i = 0; i < nax; ++i) {
auto t = g[i].get();
if (mn(t).empty()) {
auto nxt = g[i].get();
if (mn(nxt).empty())continue;
t = nxt;
}
s.insert({t, i});
}
map<array<int, K>, GenNumber> gn;
auto cmpg = [&](const pair<vector<int>, array<int, K>> &lhs, const pair<vector<int>, array<int, K>> &rhs) {
return lhs.first != rhs.first ? cmp(lhs.first, rhs.first) : lhs.second < rhs.second;
};
set<pair<vector<int>, array<int, K>>, decltype(cmpg)> best(cmpg);
while (k--) {
while (1) {
auto it = *s.begin();
s.erase(it);
for (int i = 0; i < K; ++i) {
auto x = it.first;
x[i]++;
s.insert({x, -1});
}
if (~it.second) {
auto nxt = g[it.second].get();
if (accumulate(nxt.begin(), nxt.end(), 0))s.insert({nxt, it.second});
}
if (!gn.count(it.first)) {
int n = accumulate(it.first.begin(), it.first.end(), 0);
gn[it.first] = GenNumber(n, it.first);
best.insert({gn[it.first].get(), it.first});
break;
}
}
auto [val, who] = *best.begin();
best.erase(best.begin());
auto nxt = gn[who].get();
if (nxt.size())best.insert({nxt, who});
for (auto x: val)cout << x;
cout << " ";
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3780kb
input:
2 3 1 8 7 0 6 2 1 2 5 4 9 3
output:
33 34 35
result:
ok single line: '33 34 35 '
Test #2:
score: 0
Accepted
time: 19ms
memory: 3708kb
input:
1 10 1 5 2 2 6 4
output:
3 7 8 9 10 11 12 13 14 15
result:
ok single line: '3 7 8 9 10 11 12 13 14 15 '
Test #3:
score: 0
Accepted
time: 0ms
memory: 3784kb
input:
4 10 1 5 7 1 2 4 0 1 5 8 9 4 3 5 2 2 7 8 6 1 7 0 2 2
output:
33 66 99 133 166 199 233 266 299 303
result:
ok single line: '33 66 99 133 166 199 233 266 299 303 '
Test #4:
score: 0
Accepted
time: 2ms
memory: 3792kb
input:
5 10 5 9 4 8 3 3 1 1 9 2 8 9 6 3 3 0 2 1 2 6 0 3 6 4 3 6 4 2 9 4
output:
7 17 27 37 47 55 57 67 70 71
result:
ok single line: '7 17 27 37 47 55 57 67 70 71 '
Test #5:
score: 0
Accepted
time: 2ms
memory: 3804kb
input:
5 10 8 7 1 4 8 9 2 5 0 1 0 1 9 5 5 3 9 7 6 0 0 2 3 1 1 0 0 4 9 3
output:
66 88 166 188 222 226 262 266 288 366
result:
ok single line: '66 88 166 188 222 226 262 266 288 366 '
Test #6:
score: 0
Accepted
time: 2ms
memory: 3928kb
input:
5 10 6 8 7 7 0 0 0 5 1 9 4 1 5 9 6 9 5 4 0 4 6 9 1 6 2 8 7 4 4 0
output:
3 13 22 23 30 31 32 33 34 35
result:
ok single line: '3 13 22 23 30 31 32 33 34 35 '
Test #7:
score: -100
Wrong Answer
time: 238ms
memory: 4424kb
input:
5 1000 0 4 1 3 9 6 9 6 2 1 8 6 5 3 0 7 7 3 0 2 8 0 8 4 2 4 1 2 9 7
output:
55 155 255 333 335 353 355 455 505 515 525 533 535 545 550 551 552 553 554 555 556 557 558 559 565 575 577 585 595 655 666 755 757 775 777 855 888 955 1055 1111 1116 1119 1155 1161 1166 1169 1191 1196 1199 1255 1333 1335 1353 1355 1455 1505 1515 1525 1533 1535 1545 1550 1551 1552 1553 1554 1555 1556...
result:
wrong answer 1st lines differ - expected: '55 155 255 333 335 353 355 455...0 10053 10055 10111 10116 10119', found: '55 155 255 333 335 353 355 455... 10055 10111 10116 10119 10155 '