제출 #222864

#제출 시각아이디문제언어결과실행 시간메모리
222864MrDominoArranging Shoes (IOI19_shoes)C++14
65 / 100
1022 ms3700 KiB
#include <bits/stdc++.h>
#include "shoes.h"

using namespace std;

typedef long long ll;

ll get_dist(vector<int> ord)
{
    ll sol = 0;
    for (int i = 0; i < (int) ord.size(); i++)
    {
        sol += abs(ord[i] - i);
    }
    return sol / 2;
}

const int N = (int) 1e5 + 7;
int pl[N];
int mn[N];

long long count_swaps(vector<int> a)
{
    int n = (int) a.size() / 2;
    bool sim_sub = 1;
    for (int i = 0; i < n; i++)
    {
        sim_sub &= (a[i] < 0 && a[i + n] > 0 && a[i] == -a[i + n]);
    }
    if (sim_sub)
    {
        vector<int> ord;
        for (int i = 0; i < n; i++)
        {
            ord.push_back(i);
            ord.push_back(i + n);
        }
        return get_dist(ord);
    }
    ll sol = 0;
    for (int i = 0; i < n; i++)
    {
        int pos = 2 * i;
        for (int j = 1; j <= n; j++)
        {
            pl[j] = mn[j] = (int) 1e9;
        }
        for (int j = pos; j < 2 * n; j++)
        {
            if (a[j] < 0)
            {
                mn[-a[j]] = min(mn[-a[j]], j);
            }
            if (a[j] > 0)
            {
                pl[a[j]] = min(pl[a[j]], j);
            }
        }
        int smin = (int) 1e9;
        for (int j = 1; j <= n; j++)
        {
            smin = min(smin, pl[j] + mn[j]);
        }
        int init = sol;
        for (int j = 1; j <= n; j++)
        {
            if (pl[j] + mn[j] == smin)
            {
                int val = mn[j];
                while (val > pos)
                {
                    swap(a[val], a[val - 1]);
                    val--;
                    sol++;
                }
                pos++;
                if (a[pl[j]] != j)
                {
                    pl[j]++;
                }
                val = pl[j];
                while (val > pos)
                {
                    swap(a[val], a[val - 1]);
                    val--;
                    sol++;
                }
                break;
            }
        }
    }
    return sol;
}


컴파일 시 표준 에러 (stderr) 메시지

shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:64:13: warning: unused variable 'init' [-Wunused-variable]
         int init = sol;
             ^~~~
#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...