Submission #239077

# Submission time Handle Problem Language Result Execution time Memory
239077 2020-06-14T10:18:55 Z VEGAnn Vrtić (COCI18_vrtic) C++14
80 / 160
6 ms 384 KB
#include <bits/stdc++.h>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")
#define i2 array<int,2>
#define PB push_back
#define all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
using namespace std;
typedef long long ll;
const int N = 510;
const int M = 20;
const ll OO = 1e18;
const int oo = 2e9;
const int md = int(1e9) + 7;
int n, nt[N], pre[N], a[N], ans[N], cnt, siz[M];
int comp[M], f[(1 << M)], pr[(1 << M)], res[M], fr[M];

void upd(int &x, int y){
    x = min(x, y);
}

int get(int lf, int rt){
    ans[0] = a[lf];

    int fi = 0, se = 0, it = 1, mx = rt - lf;

    while (it + lf < rt){
        if (it & 1){
            fi++;

            if (fi == mx) fi -= mx;

            if (fi == se) break;

            ans[fi] = a[lf + it];
        } else {
            se--;

            if (se < 0) se += mx;

            if (fi == se) break;

            ans[se] = a[lf + it];
        }

        it++;
    }

    int sum = 0;

    for (int i = 0; i < mx; i++)
        sum = max(sum, abs(ans[i] - ans[(i + 1) % mx]));

    return sum;
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);

#ifdef _LOCAL
    freopen("in.txt","r",stdin);
#endif // _LOCAL

    cin >> n;

    for (int i = 0; i < n; i++) {
        cin >> nt[i];
        nt[i]--;
        pre[nt[i]] = i;
    }

    for (int i = 0; i < n; i++)
        cin >> a[i];

    sort(a, a + n);

    if (n < 21){
        fill(comp, comp + n, -1);

        for (int i = 0; i < n; i++){
            if (comp[i] >= 0) continue;

            comp[i] = cnt;
            siz[cnt] = 1;
            fr[cnt] = i;

            int loc = nt[i];

            while (comp[loc] < 0){
                comp[loc] = cnt;
                siz[cnt]++;
                loc = nt[loc];
            }

            cnt++;
        }

        fill(f + 1, f + (1 << cnt), oo);

        f[0] = 0;

        for (int msk = 0; msk < (1 << cnt); msk++){
            int lf = 0;

            for (int i = 0; i < cnt; i++)
                if (msk & (1 << i))
                    lf += siz[i];

            for (int i = 0; i < cnt; i++)
                if (!(msk & (1 << i))){
                    int nw = max(f[msk], get(lf, lf + siz[i]));

                    if (f[msk + (1 << i)] > nw){
                        f[msk + (1 << i)] = nw;
                        pr[msk + (1 << i)] = i;
                    }
                }
        }

        cout << f[(1 << cnt) - 1] << '\n';

        int msk = (1 << cnt) - 1, sum = n;

        while (msk > 0){
            int cur = pr[msk];

            int lf = sum - siz[cur];

            get(lf, sum);

            int loc = fr[cur];

            for (int it = 0; it < siz[cur]; it++){
                res[loc] = ans[it];
                loc = nt[loc];
            }

            sum -= siz[cur];
            msk ^= (1 << cur);
        }

        for (int i = 0; i < n; i++)
            cout << res[i] << " ";

        return 0;
    }

    cout << get(0, n) << '\n';

    for (int i = 0; i < n; i++)
        cout << ans[i] << " ";

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 384 KB the dissatisfaction value doesn't match with your answer
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 384 KB the dissatisfaction value doesn't match with your answer
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 384 KB the dissatisfaction value doesn't match with your answer
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 384 KB the dissatisfaction value doesn't match with your answer
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 384 KB the dissatisfaction value doesn't match with your answer
2 Halted 0 ms 0 KB -