Submission #164577

#TimeUsernameProblemLanguageResultExecution timeMemory
164577qwerty234Arranging Shoes (IOI19_shoes)C++14
10 / 100
13 ms6652 KiB
#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define f first
#define se second
#define pb push_back
 
 
using namespace std;
 
 
const int N = 4e5 + 219;
const ll M = 3e5 + 19;
const ll inf = 1e15 + 10;
const ll mod = 1e9 + 7;


ll n, a[N], Last[N], Lastt[N], nx[N], prv[N], t[4 * N];
bool vis[N];


void upd(int v, int tl, int tr, int pos) {
    if (tl == tr) {
        t[v] = 1;
        return;
    }
    int tm = (tl + tr) / 2;
    if (pos <= tm)
        upd(v * 2, tl, tm, pos);
    else
        upd(v * 2 + 1, tm + 1, tr, pos);
    t[v] = t[v * 2] + t[v * 2 + 1];
}


ll get(int v, int tl, int tr, int l, int r) {
    if (r < tl || tr < l)
        return 0;
    int tm = (tl + tr) / 2;
    if (l <= tl && tr <= r)
        return t[v];
    return get(v * 2, tl, tm, l, r) + get(v * 2 + 1, tm + 1, tr, l, r);
}


ll count_swaps(vector <int> b) {
    n = b.size();
    for (int i = 0; i < b.size(); i++)
        a[i + 1] = b[i];
    for (int i = 1; i <= n; i++) {
        if (a[i] < 0) {
            Lastt[-a[i]] = i;
            prv[i] = Last[-a[i]];
        } else {
            Last[a[i]] = i;
            prv[i] = Lastt[a[i]];
        }
    }
    memset(Last, 0, sizeof(Last));
    memset(Lastt, 0, sizeof(Lastt));
    for (int i = n; i >= 1; i--) {
        if (a[i] < 0) {
            Lastt[-a[i]] = i;
            nx[i] = Last[-a[i]];
        } else {
            Last[a[i]] = i;
            nx[i] = Lastt[a[i]];
        }
    }
    ll ans = 0, t1, t2;
    ll L = 1, R = n;
    while (L <= R) {
        t1 = nx[L] - L - get(1, 1, n, L, nx[L]) - 1;
        if (a[L] > 0)
            t1++;
        t2 = R - prv[R] - get(1, 1, n, R, prv[R]) - 1;
        if (a[R] < 0)
            t2++;
        if (t1 < t2) {
            ans += t1;
            vis[L] = vis[nx[L]] = 1;
            upd(1, 1, n, L);
            upd(1, 1, n, nx[L]);
        } else {
            ans += t2;
            upd(1, 1, n, R);
            upd(1, 1, n, prv[R]);
            vis[R] = vis[prv[R]] = 1;
        }
        while (vis[L])
            L++;
        while (vis[R])
            R--;
        if (R < L || L > n || R < 0)
            break;
    }
    return ans;
}

Compilation message (stderr)

shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:49:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < b.size(); i++)
                     ~~^~~~~~~~~~
#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...