Submission #532099

#TimeUsernameProblemLanguageResultExecution timeMemory
532099fhvirusNaan (JOI19_naan)C++17
29 / 100
4072 ms9400 KiB
// Knapsack DP is harder than FFT. #include <bits/stdc++.h> using namespace std; typedef int64_t ll; typedef pair<int, int> pii; #define pb emplace_back #define AI(x) begin(x),end(x) #define ff first #define ss second #ifdef OWO #define debug(args...) LKJ("\033[1;32m[ " + string(#args) + " ]\033[0m", args) template <class I> void LKJ(I&&x) { cerr << x << endl; } template <class I, class...T> void LKJ(I&&x, T&&...t) { cerr << x << ", "; LKJ(t...); } template <class I> void OI(I a, I b) { while (a != b) cerr << *a << " \n"[(a = next(a)) == b]; } #else #define debug(...) 0 #define OI(...) 0 #endif struct Frac { ll a, b; Frac () = default; Frac (ll _a, ll _b): a(_a), b(_b) { shrink(); } void shrink() { ll g = gcd(a, b); a /= g; b /= g; } const ll toi() const { return a / b; } const Frac operator + (const Frac& o) const { return Frac(a * o.b + o.a * b, b * o.b); } const Frac operator - (const Frac& o) const { return Frac(a * o.b - o.a * b, b * o.b); } const Frac operator * (const ll& val) const { return Frac(a * val, b); } const bool operator < (const Frac& o) const { return a * o.b < o.a * b; } }; const int kN = 2002; int N, L, V[kN][kN]; ll pre[kN][kN]; Frac ori[kN]; Frac f(int i, Frac j) { ll k = j.toi(); ll sum1 = pre[i][k]; j.a -= k * j.b; Frac sum2 = j * V[i][k + 1]; sum2.a += sum1 * sum2.b; return sum2; } bool solve(const vector<int>& perm) { vector<Frac> x(1, Frac(0, 1)); OI(AI(perm)); for (const int& i: perm) { Frac need = ori[i] + f(i, x.back()); Frac tmp = f(i, x.back()); debug(i, need.a, need.b); debug(ori[i].a, ori[i].b); debug(x.back().a, x.back().b, tmp.a, tmp.b); if (f(i, Frac(L, 1)) < need) return false; int k = x.back().toi(); while (f(i, Frac(k + 1, 1)) < need) ++k; need = need - f(i, Frac(k, 1)); Frac nxt = need; nxt.b *= V[i][k + 1]; nxt.shrink(); x.emplace_back(nxt.a + k * nxt.b, nxt.b); } for (int i = 1; i < N; ++i) cout << x[i].a << ' ' << x[i].b << '\n'; for (const int& i: perm) cout << i << " \n"[i == perm.back()]; return true; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> N >> L; for (int i = 1; i <= N; ++i) { ori[i] = Frac(0, 1); for (int j = 1; j <= L; ++j) { cin >> V[i][j]; pre[i][j] = pre[i][j - 1] + V[i][j]; ori[i].a += V[i][j]; } ori[i].b = N; ori[i].shrink(); } vector<int> perm(N); iota(AI(perm), 1); do if (solve(perm)) exit(0); while (next_permutation(AI(perm))); cout << -1 << '\n'; return 0; }

Compilation message (stderr)

naan.cpp: In function 'bool solve(const std::vector<int>&)':
naan.cpp:16:17: warning: statement has no effect [-Wunused-value]
   16 | #define OI(...) 0
      |                 ^
naan.cpp:51:2: note: in expansion of macro 'OI'
   51 |  OI(AI(perm));
      |  ^~
naan.cpp:15:20: warning: statement has no effect [-Wunused-value]
   15 | #define debug(...) 0
      |                    ^
naan.cpp:56:3: note: in expansion of macro 'debug'
   56 |   debug(i, need.a, need.b);
      |   ^~~~~
naan.cpp:15:20: warning: statement has no effect [-Wunused-value]
   15 | #define debug(...) 0
      |                    ^
naan.cpp:57:3: note: in expansion of macro 'debug'
   57 |   debug(ori[i].a, ori[i].b);
      |   ^~~~~
naan.cpp:15:20: warning: statement has no effect [-Wunused-value]
   15 | #define debug(...) 0
      |                    ^
naan.cpp:58:3: note: in expansion of macro 'debug'
   58 |   debug(x.back().a, x.back().b, tmp.a, tmp.b);
      |   ^~~~~
naan.cpp:55:8: warning: variable 'tmp' set but not used [-Wunused-but-set-variable]
   55 |   Frac tmp = f(i, x.back());
      |        ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...