Submission #947626

#TimeUsernameProblemLanguageResultExecution timeMemory
947626marvinthangCookies (JOI23_cookies)C++17
100 / 100
196 ms207468 KiB
/************************************* * author: marvinthang * * created: 16.03.2024 22:48:39 * *************************************/ #include <bits/stdc++.h> using namespace std; #define fi first #define se second #define left ___left #define right ___right #define TIME (1.0 * clock() / CLOCK_PERS_SEC) #define MASK(i) (1LL << (i)) #define BIT(x, i) ((x) >> (i) & 1) #define __builtin_popcount __builtin_popcountll #define ALL(v) (v).begin(), (v).end() #define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i) #define REPD(i, n) for (int i = (n); i-- > 0; ) #define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i) #define FORD(i, b, a) for (int i = (b), _a = (a); i-- > _a; ) #define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i) #define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i) #define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u) #define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u) #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); } template <class U, class V> scan_op(pair <U, V>) { return in >> u.fi >> u.se; }; template <class T> scan_op(vector <T>) { for (T &x: u) in >> x; return in; } template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.fi << ", " << u.se << ')'; } template <size_t i, class T> ostream &print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ')'; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); } template <class ...U> print_op(tuple <U...>) { return print_tuple_utils<0, tuple <U...>>(out, u); } template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream &>::type operator << (ostream &out, const Con &con) { out << '{'; for (auto it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; } const int MAX = 15005; #define Bitset bitset <MAX> void process(void) { int n; cin >> n; vector <int> a(n); cin >> a; int m; cin >> m; vector <int> b(m); cin >> b; reverse(ALL(b)); vector <int> c(a); sort(ALL(c)); int sum = accumulate(ALL(a), 0); vector <int> max_cap(sum + 1); int j = 0, cur = 0; max_cap[0] = n; FORE(i, 1, sum) { while (j < n && c[j] < i) cur += c[j++]; max_cap[i] = cur + (n - j) * i; } vector <vector <Bitset>> dp(m); Bitset all; all.set(); REP(i, m) dp[i].resize(sum / b[i] + 1); dp[0][0][0] = 1; REP(i, m) { int sz = sum / b[i] + 1; REP(j, sz) { dp[i][j] &= all >> (MAX - max_cap[j] - 1); if (i + 1 < m) dp[i + 1][j] |= dp[i][j]; if (j + 1 < sz) dp[i][j + 1] |= dp[i][j] << b[i]; } } int mi = -1; REP(j, dp[m - 1].size()) if (dp[m - 1][j][sum]) { mi = j; break; } cout << mi << '\n'; if (mi == -1) return; int k = sum; vector <int> boxes; REPD(i, m) { while (k >= b[i] && mi && dp[i][mi - 1][k - b[i]]) { boxes.push_back(b[i]); --mi; k -= b[i]; } } priority_queue <pair <int, int>> pq; REP(i, n) pq.emplace(a[i], i); for (int sz: boxes) { vector <int> v; REP(love, sz) { v.push_back(pq.top().se); pq.pop(); } cout << sz; for (int i: v) { cout << ' ' << i + 1; pq.emplace(--a[i], i); } cout << '\n'; } } int main(void) { ios_base::sync_with_stdio(false); cin.tie(nullptr); file("cookies"); process(); return (0 ^ 0); }

Compilation message (stderr)

cookies.cpp: In function 'int main()':
cookies.cpp:27:57: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
cookies.cpp:105:2: note: in expansion of macro 'file'
  105 |  file("cookies");
      |  ^~~~
cookies.cpp:27:90: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
cookies.cpp:105:2: note: in expansion of macro 'file'
  105 |  file("cookies");
      |  ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...