제출 #1032542

#제출 시각아이디문제언어결과실행 시간메모리
1032542omarpaladines95Arranging Shoes (IOI19_shoes)C++14
10 / 100
1095 ms3908 KiB
#include "shoes.h"

#include <bits/stdc++.h>

using namespace std;

void FindFirstSwap(std::vector<int>& original_copy, const std::vector<int>& target, int& startingIndex, long long& numSwaps)
{
    int i = 0;
    int j = 0;
    startingIndex = -1;
    while (original_copy[i] == target[j])
    {
        ++i;
        ++j;

        // If we reached the end and they match, we can just return.
        if ((i == target.size()) && (j == target.size()))
        {
            return;
        }
    }

    // Now we need to advance j until it matches.
    for (int start = j; start < target.size(); ++start)
    {
        if(original_copy[i] == target[start])
        {
            j=start;
            break;
        }
    }

    startingIndex = i;
    numSwaps = j-i;
}

long long FindNumberOfSwaps(const std::vector<int>& original, const std::vector<int>& target)
{
    long long totalSwaps = 0;
    std::vector<int> original_copy = original;

    // We will keep modifying original_copy until we swap everything.
    while (true)
    {
        int startingIndex = 0;
        long long numSwaps = 0;
        FindFirstSwap(original_copy, target, startingIndex, numSwaps);
        totalSwaps += numSwaps;
        if (startingIndex == -1)
        {
            break;
        }
        else
        {
            // Apply the swaps.
            for (int i = 0; i < numSwaps; ++i)
            {
                int localCopy = original_copy[startingIndex];
                original_copy[startingIndex] = original_copy[startingIndex+1];
                original_copy[startingIndex + 1] = localCopy;
                startingIndex++;
            }
        }
    }
    return totalSwaps;
}

bool IsValid(const std::vector<int>& curPermutation)
{
    for (int i = 0; i < curPermutation.size(); i+=2)
    {
        if(!((curPermutation[i] < 0) && (curPermutation[i+1] > 0) && ((curPermutation[i] + curPermutation[i+1]) == 0)))
        {
            return false;
        }
    }
    return true;
}

long long count_swaps(std::vector<int> S)
{
    long long minSwaps = LLONG_MAX;
    vector<int> curPermutation = S;
    sort(curPermutation.begin(), curPermutation.end());
    do
    {
        if (IsValid(curPermutation))
        {
            minSwaps = min(minSwaps, FindNumberOfSwaps(S, curPermutation));
        }
    } while(next_permutation(curPermutation.begin(), curPermutation.end()));
    return minSwaps;
}

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

shoes.cpp: In function 'void FindFirstSwap(std::vector<int>&, const std::vector<int>&, int&, long long int&)':
shoes.cpp:18:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   18 |         if ((i == target.size()) && (j == target.size()))
      |              ~~^~~~~~~~~~~~~~~~
shoes.cpp:18:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   18 |         if ((i == target.size()) && (j == target.size()))
      |                                      ~~^~~~~~~~~~~~~~~~
shoes.cpp:25:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |     for (int start = j; start < target.size(); ++start)
      |                         ~~~~~~^~~~~~~~~~~~~~~
shoes.cpp: In function 'bool IsValid(const std::vector<int>&)':
shoes.cpp:71:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |     for (int i = 0; i < curPermutation.size(); i+=2)
      |                     ~~^~~~~~~~~~~~~~~~~~~~~~~
#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...