Submission #164574

#TimeUsernameProblemLanguageResultExecution timeMemory
164574qwerty234Arranging Shoes (IOI19_shoes)C++14
10 / 100
3 ms504 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], pos[N], nx[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 (pos[abs(a[i])] != 0) {
            nx[pos[abs(a[i])]] = i;
            nx[i] = pos[abs(a[i])];
        }
        pos[abs(a[i])] = 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 - nx[R] - get(1, 1, n, R, nx[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, nx[R]);
            vis[R] = vis[nx[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...