이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "books.h"
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
typedef long long ll;
const int N = 1003;
int n;
int bfs(vector<int> p, int s)
{
for (int i = 0; i < n; ++i)
++p[i];
map<pair<vector<int>, pair<int, int> >, int> mp;
deque<pair<vector<int>, pair<int, int> > > q;
q.push_back(m_p(p, m_p(s, 0)));
mp[m_p(p, m_p(s, 0))] = 0;
while (1)
{
pair<vector<int>, pair<int, int> > t = q.front();
q.pop_front();
bool z = true;
if (t.se.fi != s)
z = false;
for (int i = 0; i < n; ++i)
{
if (t.fi[i] != i + 1)
{
z = false;
break;
}
}
if (z)
return mp[t];
pair<vector<int>, pair<int, int> > h;
h = t;
if (h.se.fi != n - 1)
{
++h.se.fi;
if (mp.find(h) == mp.end())
{
mp[h] = mp[t] + 1;
q.push_back(h);
}
}
h = t;
if (h.se.fi != 0)
{
--h.se.fi;
if (mp.find(h) == mp.end())
{
mp[h] = mp[t] + 1;
q.push_back(h);
}
}
h = t;
swap(h.se.se, h.fi[h.se.fi]);
if (mp.find(h) == mp.end())
{
mp[h] = mp[t];
q.push_front(h);
}
}
}
int q[N];
long long minimum_walk(std::vector<int> p, int s)
{
n = sz(p);
ll ans = 0;
for (int i = 0; i < n; ++i)
{
ans += abs(i - p[i]);
}
for (int i = 0; i < n; ++i)
{
q[min(i, p[i])]++;
q[max(i, p[i])]--;
}
for (int i = 0; i < n; ++i)
{
q[i] += q[i - 1];
if (!q[i])
{
bool zl = false;
bool zr = false;
for (int j = 0; j <= i; ++j)
{
if (j != p[j])
{
zl = true;
break;
}
}
for (int j = i + 1; j < n; ++j)
{
if (j != p[j])
{
zr = true;
break;
}
}
if (zr)
ans += 2;
}
}
//printf("%d\n", bfs(p, s));
return ans;
}
컴파일 시 표준 에러 (stderr) 메시지
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:93:18: warning: variable 'zl' set but not used [-Wunused-but-set-variable]
93 | bool zl = false;
| ^~
# | 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... |