This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#ifndef LOCAL
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define dbg(...)
#define STRUCT_DBG(...)
#else
#include "lib/debug.h"
#endif
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
template <class T>
using vec = vector<T>;
using vll = vector<ll>;
using vpll = vector<pair<ll, ll>>;
using vvll = vector<vector<ll>>;
using vvpll = vector<vector<pair<ll, ll>>>;
template <class T>
using indexed_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define FOR(i, s, e) for (ll i = (ll)s; i < (ll)e; i++)
#define CFOR(i, s, e) for (ll i = (ll)s; i <= (ll)e; i++)
#define RFOR(i, e, s) for (ll i = (ll)e - 1; i >= (ll)s; i--)
#define TRAV(a, c) for (const auto &a : c)
#define all(x) x.begin(), x.end()
#define MOD 1000000007
// #define MOD 998244353
#define FASTIO
#define PRECISION
// #define FILE "file"
#define SINGLE
// #define MULTIPLE
// #define GOOGLE
void solve() {
ll n;
cin >> n;
vll a(n);
FOR(i, 0, n) cin >> a[i];
ll m;
cin >> m;
vll b(m);
FOR(i, 0, m) cin >> b[i];
bool st1 = true, st2 = (m == 1);
FOR(i, 0, n) st1 &= (a[i] == 1);
if (st1) {
vll dp(n + 1, INT_MAX), from(n + 1, -1);
dp[0] = 0;
FOR(i, 0, n) {
if (dp[i] == INT_MAX) continue;
TRAV(e, b) {
if (i + e <= n && dp[i] + 1 < dp[i + e]) {
dp[i + e] = dp[i] + 1;
from[i + e] = i;
}
}
}
ll ind = 1, curr = n;
if (dp[curr] == INT_MAX) {
cout << "-1\n";
return;
}
cout << dp[curr] << "\n";
while (curr != 0) {
cout << curr - from[curr] << " ";
FOR(i, 0, curr - from[curr]) {
cout << ind++ << " ";
}
cout << "\n";
curr = from[curr];
}
} else if (st2) {
vpll aa(n);
FOR(i, 0, n) aa[i] = {a[i], i + 1};
sort(all(aa));
reverse(all(aa));
vvll ans;
while (aa.size()) {
if (aa.size() < b[0]) {
cout << "-1\n";
return;
}
ans.push_back({});
FOR(i, 0, b[0]) {
ans.back().push_back(aa[i].second);
aa[i].first--;
}
sort(all(aa));
reverse(all(aa));
while (aa.size() && aa.back().first == 0) {
aa.pop_back();
}
}
cout << ans.size() << "\n";
TRAV(e, ans) {
cout << e.size() << " ";
TRAV(e1, e) {
cout << e1 << " ";
}
cout << "\n";
}
} else {
map<vpll, vvll> mm;
function<vvll(vpll)> recurse = [&](vpll state) -> vvll {
if (mm.count(state)) return mm[state];
if (state == vpll{}) return mm[state] = {};
mm[state].resize(100);
TRAV(e, b) {
if (state.size() < e) continue;
vpll tmp(state);
vll removed;
FOR(i, 0, e) {
tmp[i].first--;
removed.push_back(tmp[i].second);
}
sort(all(tmp));
reverse(all(tmp));
while (tmp.size() && tmp.back().first == 0) tmp.pop_back();
vvll obj = recurse(tmp);
obj.push_back(removed);
if (!mm.count(state)) mm[state] = obj;
if (obj.size() < mm[state].size()) {
mm[state] = obj;
}
}
return mm[state];
};
vpll initial(n);
FOR(i, 0, n) initial[i] = {a[i], i + 1};
sort(all(initial));
reverse(all(initial));
vvll ans = recurse(initial);
if (ans.size() == 100) {
cout << "-1\n";
return;
}
cout << ans.size() << "\n";
TRAV(e, ans) {
cout << e.size() << " ";
TRAV(e1, e) {
cout << e1 << " ";
}
cout << "\n";
}
}
}
int main() {
#ifdef FASTIO
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#endif
#ifdef PRECISION
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
#endif
#ifdef FILE
freopen((FILE + string(".in")).c_str(), "r", stdin);
freopen((FILE + string(".out")).c_str(), "w", stdout);
#endif
#ifdef SINGLE
solve();
#endif
#ifdef MULTIPLE
ll t;
cin >> t;
for (ll i = 1; i <= t; i++) {
solve();
}
#endif
#ifdef GOOGLE
ll t;
cin >> t;
for (ll i = 1; i <= t; i++) {
cout << "Case #" << i << ": ";
solve();
}
#endif
}
Compilation message (stderr)
cookies.cpp: In function 'void solve()':
cookies.cpp:91:27: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} and '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} [-Wsign-compare]
91 | if (aa.size() < b[0]) {
cookies.cpp: In lambda function:
cookies.cpp:121:34: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} and 'const long long int' [-Wsign-compare]
121 | if (state.size() < e) continue;
| ~~~~~~~~~~~~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |