Submission #1173668

#TimeUsernameProblemLanguageResultExecution timeMemory
1173668LucaIlieRope (JOI17_rope)C++20
0 / 100
10 ms23884 KiB
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1e6;
const int MAX_M = 1e6;
int c[MAX_N], ans[MAX_M + 1], frecv[MAX_M + 1];
vector<int> pos[MAX_M + 1];
set<pair<int, int>> maxFrecv;

void erase( int col ) {
    maxFrecv.erase( { frecv[col], col } );
}

void insert( int col ) {
    maxFrecv.insert( { frecv[col], col } );
}

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

    int n, m;

    cin >> n >> m;
    for ( int i = 0; i < n; i++ ) {
        cin >> c[i];
        frecv[c[i]]++;
        pos[c[i]].push_back( i );
    }

    for ( int i = 1; i <= m; i++ )
        maxFrecv.insert( { frecv[i], i } );

    for ( int x = 1; x <= m; x++ ) {
        int minn = n;
        for ( int f = 0; f < 2; f++ ) {
            int changes = 0, sumFrecv = n - frecv[x];
            for ( int p: pos[x] ) {
                int q = ((p - f) % 2 == 0 ? p + 1 : p - 1);
                if ( f <= q && q < n && c[q] != x ) {
                    erase( c[q] );
                    frecv[c[q]]--;
                    sumFrecv--;
                    insert( c[q] );
                    changes++;
                }
            }

            auto p = maxFrecv.end();
            p--;
            if ( p->second == x )
                p--;

            changes += sumFrecv - p->first;

            minn = min( minn, changes );

            for ( int p: pos[x] ) {
                int q = ((p - f) % 2 == 0 ? p + 1 : p - 1);
                if ( f <= q && q < n && c[q] != x ) {
                    erase( c[q] );
                    frecv[c[q]]++;
                    insert( c[q] );
                }
            }
       }

        cout << minn << "\n";
    }

    return 0;
}
#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...