제출 #1180544

#제출 시각아이디문제언어결과실행 시간메모리
1180544patgraRope (JOI17_rope)C++20
45 / 100
27 ms5372 KiB
#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>

#define rep(a,b,c) for(auto a = (b); a != (c); a++)
#define repD(a,b,c) for(auto a = (b); a != (c); a--)
#define repIn(a, b) for(auto a : (b))
#define repIn2(a, b, c) for(auto [a, b] : (c))

constexpr bool dbg = 1;
#define DEBUG if constexpr(dbg)
#define DC DEBUG std::cerr
#define eol std::endl

#define ll long long
#define pb push_back

using namespace std;

struct hsh {
    ll operator() (pair<int, int> x) const {
        ll a = x.first, b = x.second;
        return (a << 32) | b;
    }
};

constexpr int maxm = 1e6 + 7, maxTb = 1 << 20, maxLog = 20;
int n, m;
int iniCol[maxm];
int ans[maxm];
unordered_map<pair<int, int>, int, hsh> ileTakich;
int ilePojedynczych[maxm], ilePodwojnych[maxm];
unordered_map<pair<int, int>, int, hsh> ileTakich2;
int ilePojedynczych2[maxm], ilePodwojnych2[maxm];
pair<int, int> maxOnes[maxm];

void checkCols(int c1, int c2) {
    if(c1 > c2) swap(c1, c2);
    int ileWsumie = n / 2;
    auto ilePoj = ilePojedynczych[c1] + (c1 == c2 ? 0 : ilePojedynczych[c2] - (ileTakich.contains(pair{c1, c2}) ? ileTakich[pair{c1, c2}] : 0));
    auto ilePod = ilePodwojnych[c1] + (c1 == c2 ? 0 : ilePodwojnych[c2]);
    auto cost1 = (ileWsumie - ilePod) * 2 - ilePoj;
    if(n % 2 == 1) cost1 += iniCol[n - 1] != c1 && iniCol[n - 1] != c2;

    if(n % 2 == 0) ileWsumie--;
    ilePoj = ilePojedynczych2[c1] + (c1 == c2 ? 0 : ilePojedynczych2[c2] - (ileTakich2.contains(pair{c1, c2}) ? ileTakich2[pair{c1, c2}] : 0));
    ilePod = ilePodwojnych2[c1] + (c1 == c2 ? 0 : ilePodwojnych2[c2]);
    auto cost2 = (ileWsumie - ilePod) * 2 - ilePoj;
    cost2 += iniCol[0] != c1 && iniCol[0] != c2;
    if(n % 2 == 0) cost2 += iniCol[n - 1] != c1 && iniCol[n - 1] != c2;

    auto cost = min(cost1, cost2);
    ans[c1] = min(ans[c1], cost);
    ans[c2] = min(ans[c2], cost);
}


int32_t main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    cin >> n >> m;
    rep(i, 0, n) cin >> iniCol[i];
    //scanf("%d%d", &n, &m);
    //rep(i, 0, n) scanf("%d", iniCol + i);
    rep(i, 1, m + 1) ans[i] = 1e9;
    rep(i, 0, n - 1) {
        auto c1 = iniCol[i], c2 = iniCol[i + 1];
        if(i % 2 == 0) {
            if(c1 == c2) ilePodwojnych[c1]++;
            else ilePojedynczych[c1]++, ilePojedynczych[c2]++, ileTakich[pair{min(c1, c2), max(c1, c2)}]++;
        }
        else {
            if(c1 == c2) ilePodwojnych2[c1]++;
            else ilePojedynczych2[c1]++, ilePojedynczych2[c2]++, ileTakich2[pair{min(c1, c2), max(c1, c2)}]++;
        } 
    }

    rep(i, 1, m + 1) maxOnes[i - 1] = {ilePodwojnych[i] * 2 + ilePojedynczych[i], i};
    sort(maxOnes, maxOnes + m);
    reverse(maxOnes, maxOnes + m);
    rep(i, 1, m + 1) {
        repIn2(j1, j2, maxOnes) {
            checkCols(i, j2);
            if(!ileTakich.contains(pair{min(i, j2), max(i, j2)}) && !ileTakich2.contains(pair{min(i, j2), max(i, j2)})) break;
        }
    }

    rep(i, 1, m + 1) maxOnes[i - 1] = {ilePodwojnych2[i] * 2 + ilePojedynczych2[i], i};
    sort(maxOnes, maxOnes + m);
    reverse(maxOnes, maxOnes + m);
    rep(i, 1, m + 1) {
        repIn2(j1, j2, maxOnes) {
            checkCols(i, j2);
            if(!ileTakich.contains(pair{min(i, j2), max(i, j2)}) && !ileTakich2.contains(pair{min(i, j2), max(i, j2)})) break;
        }
    }

    //rep(i, 0, m) printf("%d\n", ans[i + 1]);
    rep(i, 0, m) cout << ans[i + 1] << '\n';
}

#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...