Submission #142635

#TimeUsernameProblemLanguageResultExecution timeMemory
142635KCSCVrtić (COCI18_vrtic)C++14
0 / 160
9 ms404 KiB
#include <bits/stdc++.h> using namespace std; /* #ifdef HOME const int DIM = ; const int INF = 1000000005; const int MXM = 50; #else */ const int DIM = 155; const int INF = 1000000005; const int MXM = 10000005; //#endif bool oki[DIM]; int nxt[DIM], cnt[DIM], val[DIM], dp[MXM]; int cst[DIM][DIM], ans[DIM], fth[MXM]; vector<int> tot, aux; vector<int> decrypt(int v, int n) { vector<int> msk(n + 1); for (int i = 1; i <= n; ++i) { if (!val[i]) continue; while (v >= val[i]) { v -= val[i]; ++msk[i]; } } return msk; } int encrypt(vector<int> &msk, int n) { int v = 0; for (int i = 1; i <= n; ++i) for (int j = 1; j <= msk[i]; ++j) v += val[i]; return v; } int main(void) { #ifdef HOME freopen("vrtic.in", "r", stdin); freopen("vrtic.out", "w", stdout); #endif int n; cin >> n; for (int i = 1; i <= n; ++i) cin >> nxt[i]; for (int i = 1; i <= n; ++i) cin >> cnt[i]; sort(cnt + 1, cnt + n + 1); for (int i = 1; i <= n; ++i) { for (int j = i; j <= n; ++j) { vector<int> ord; for (int k = i; k <= j; k += 2) ord.push_back(k); for (int k = j - (j - i + 1) % 2; k >= i; k -= 2) ord.push_back(k); ord.push_back(ord[0]); for (int k = 1; k < ord.size(); ++k) cst[i][j] = max(cst[i][j], (int) abs(cnt[ord[k]] - cnt[ord[k - 1]])); } } return 0; tot.resize(n + 1); for (int i = 1; i <= n; ++i) { if (oki[i]) continue; int nr = 0; for (int j = i; ;) { if (oki[j]) break; oki[j] = true; ++nr; j = nxt[j]; } ++tot[nr]; } val[n + 1] = 1; for (int i = n; i >= 1; --i) val[i] = val[i + 1] * (tot[i + 1] + 1); for (int i = 1; i <= n; ++i) if (!tot[i]) val[i] = 0; int mxm = encrypt(tot, n); for (int i = 1; i <= mxm; ++i) dp[i] = INF; for (int v = 0; v < mxm; ++v) { aux = decrypt(v, n); int nr = 0; for (int i = 1; i <= n; ++i) nr += aux[i] * i; for (int i = 1; i <= n; ++i) { if (tot[i] == aux[i]) continue; if (dp[v + val[i]] > max(dp[v], cst[nr + 1][nr + i])) { dp[v + val[i]] = max(dp[v], cst[nr + 1][nr + i]); fth[v + val[i]] = i; } } } for (int v = mxm, nr = n; v; v -= val[fth[v]]) { for (int i = 1; i <= n; ++i) oki[i] = false; for (int i = 1; i <= n; ++i) { if (oki[i] or ans[i]) continue; vector<int> cyc; for (int j = i; ;) { if (oki[j]) break; cyc.push_back(j); oki[j] = true; j = nxt[j]; } if (cyc.size() == fth[v]) { for (int x : cyc) ans[x] = cnt[nr--]; break; } } } cout << dp[mxm] << endl; for (int i = 1; i <= n; ++i) cout << ans[i] << " "; return 0; }

Compilation message (stderr)

vrtic.cpp: In function 'int main()':
vrtic.cpp:63:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int k = 1; k < ord.size(); ++k)
                    ~~^~~~~~~~~~~~
vrtic.cpp:117:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    if (cyc.size() == fth[v]) {
        ~~~~~~~~~~~^~~~~~~~~
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...