이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
const int MAXN = 1005;
int bestLeft[MAXN], bestRight[MAXN];
int moveElement(int i, int j, vector <int> &s)
{
int cnt = 0;
while(j>i)
{
cnt++;
swap(s[j-1], s[j]);
j--;
}
return cnt;
}
int calcValue(int num, int index)
{
int sum = (bestLeft[num] - index) + (bestRight[num] - (index+1));
if(bestLeft[num]>bestRight[num]) sum++;
return sum;
}
long long count_swaps(vector <int> s)
{
int answer = 0;
for(int index = 0;index<s.size();index+=2)
{
memset(bestLeft, -1, sizeof(bestLeft));
for(int i = s.size()-1;i>=index;i--)
{
if(s[i]<0) bestLeft[ -s[i] ] = i;
else bestRight[ s[i] ] = i;
}
int bestIndex = -1;
for(int i = 1;i<=s.size()/2;i++)
{
if(bestLeft[i]==-1) continue;
if(bestIndex==-1) bestIndex = i;
else if(calcValue(bestIndex, index)>calcValue(i, index))
{
bestIndex = i;
}
}
if(bestLeft[bestIndex]>bestRight[bestIndex]) bestRight[bestIndex]++;
//cout << " " << bestIndex << '\n';
answer += moveElement(index, bestLeft[bestIndex], s);
answer += moveElement(index+1, bestRight[bestIndex], s);
}
//for(int i = 0;i<s.size();i++) cout << s[i] << " ";
//cout << '\n';
return answer;
}
컴파일 시 표준 에러 (stderr) 메시지
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:36:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int index = 0;index<s.size();index+=2)
~~~~~^~~~~~~~~
shoes.cpp:47:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 1;i<=s.size()/2;i++)
~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |