제출 #165392

#제출 시각아이디문제언어결과실행 시간메모리
165392andreiomdArranging Shoes (IOI19_shoes)C++17
30 / 100
1095 ms279032 KiB
#include "shoes.h"
#include <bits/stdc++.h>

using namespace std;

const int NMAX = 1e5 + 5;

int N, A[NMAX], B[NMAX], V[NMAX];

map < int, int > mp;

deque < int > G[2 * NMAX];

///
int AIB[2 * NMAX];

static inline int UB (int X)
{
    return (X & (-X));
}

static inline void Update (int pos, int val)
{
    for(int i = pos; i <= 2 * N; i += UB(i))
        AIB[i] += val;

    return;
}

static inline int Query (int pos)
{
    int r = 0;

    for(int i = pos; i >= 1; i -= UB(i))
        r += AIB[i];

    return r;
}
///

static inline long long Task1 (vector < int > S)
{
    long long ans = 0;

    for(int i = 0; i < N; i += 2)
    {
        int pos = 0;

        for(int j = i + 1; j < N; ++j)
            if(S[j] == -S[i])
            {
                pos = j;

                break;
            }

        ans += (pos - i - 1);

        for(int j = pos; j > i + 1; --j)
            swap(S[j], S[j - 1]);

        if(S[i] > 0)
            ++ans, swap(S[i], S[i + 1]);
    }

    return ans;
}

static inline void Normalize ()
{
    for(int i = 1; i <= N; ++i)
        B[i] = A[i];

    sort(B + 1, B + N + 1);

    V[++V[0]] = B[1];

    for(int i = 2; i <= N; ++i)
        if(B[i] != B[i - 1])
            V[++V[0]] = B[i];

    for(int i = 1; i <= N; ++i)
    {
        int Keep = lower_bound(V + 1, V + V[0] + 1, A[i]) - V;

        if(!mp[A[i]])
            mp[A[i]] = Keep;
    }

    return;
}

static inline long long Task2 (vector < int > S)
{
    long long ans = 0;

    for(int i = 0; i < N; ++i)
        A[i + 1] = S[i];

    Normalize();

    for(int i = 1; i <= N; ++i)
        G[mp[A[i]]].push_back(i);

    for(int i = 1; i <= N; i += 2)
    {
        int Now = mp[A[i]];
        int Search = mp[-A[i]];

        int pos = G[Search].front();
        G[Search].pop_front();

        ///

        ans += 1LL * (pos - Query(pos));

        Update(pos, +1);

        ///


        if(S[i] > 0)
            ++ans;

        G[Now].pop_front();
    }

    return ans;
}

long long count_swaps (vector < int > S)
{
    N = (int)S.size();

    if(N <= 1e3)
        return Task1(S);

    return Task2(S);
}
#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...