#define NDEBUG
#ifdef NDEBUG
#define dbg(TXTMSG) if constexpr (false) cerr << "lol"
#define dbgv(VARN) ((void)0)
#define dbgfor(COND) if constexpr (false) for (COND)
#else
#define _GLIBCXX_DEBUG 1
#define _GLIBCXX_DEBUG_PEDANTIC 1
#pragma GCC optimize("trapv")
#define dbg(TXTMSG) cerr << "\n" << TXTMSG
#define dbgv(VARN) cerr << "\n" << #VARN << " = "<< VARN << ", line: " << __LINE__ << "\n"
#define dbgfor(COND) for (COND)
#endif
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll,ll>;
#define e0 first
#define e1 second
constexpr ll INFTY = 2e18;
ll N;
ll find_remove(ll to_rem, deque<ll>& find_in, deque<ll>& temp)
{
swap(find_in,temp);
find_in.resize(temp.size()-1);
ll i;
for (i = 0; temp[i]!=to_rem; ++i)
{
find_in[i] = temp[i];
}
for (ll j = i; j < ll(find_in.size()); ++j)
{
find_in[j] = temp[j+1];
}
return i;
}
int main()
{
cin >> N;
vector<ll> P(N);
for (ll i = 0; i < N; ++i)
{
cin >> P[i];
}
if (N==1) {
cout << "0";
return 0;
}
vector<pair<ll,char>> outp;
deque<ll> remain(P.cbegin(),P.cend());
deque<ll> temp;
ll curr=1;
while (remain.size()>2)
{
ll i = find_remove(curr, remain, temp);
outp.push_back({-i,'a'});
for (; i > 1; i-=2)
{
outp.push_back({2,'a'});
outp.push_back({1,'b'});
}
if (i==1)
{
outp.push_back({1,'a'});
outp.push_back({2,'b'});
swap(remain[0],remain[1]);
i--;
}
assert(i==0);
outp.push_back({-1,'a'});
curr++;
}
if (remain[0]==N-1)
{
outp.push_back({-2,'a'});
}
else
{
assert(remain[0]==N);
dbgv("g");
if (N%2==1)
{
cout << "NIE DA SIE";
return 0;
}
for (ll i = 0; i < (N/2)-1; ++i)
{
outp.push_back({2,'a'});
outp.push_back({1,'b'});
}
outp.push_back({-1,'a'});
}
for (ll fff=0;fff<2;fff++)
{
vector<pair<ll,char>> newoutp;
pair<ll,char> currcmd={0,'z'};
for (pair<ll,char> cmd : outp)
{
dbgv(cmd.e0 << cmd.e1);
if (cmd.e1==currcmd.e1) currcmd.e0+=cmd.e0;
else
{
if (currcmd.e0!=0){
newoutp.push_back({currcmd.e0, currcmd.e1});
}
currcmd=cmd;
}
if (currcmd.e1=='a') {
currcmd.e0+=2*N;
currcmd.e0%=N;
}
else
{
currcmd.e0+=6;
currcmd.e0%=3;
}
}
if (currcmd.e0!=0){
newoutp.push_back({currcmd.e0, currcmd.e1});
}
swap(outp,newoutp);
}
stringstream stroutp;
ll osize = 0;
{
pair<ll,char> currcmd={0,'z'};
for (pair<ll,char> cmd : outp)
{
dbgv(cmd.e0 << cmd.e1);
if (cmd.e1==currcmd.e1) currcmd.e0+=cmd.e0;
else
{
if (currcmd.e0!=0){
osize+=1;
stroutp << currcmd.e0 << currcmd.e1 << " ";
}
currcmd=cmd;
}
if (currcmd.e1=='a') {
currcmd.e0+=2*N;
currcmd.e0%=N;
}
else
{
currcmd.e0+=6;
currcmd.e0%=3;
}
}
if (currcmd.e0!=0){
osize+=1;
stroutp << currcmd.e0 << currcmd.e1 << " ";
}
}
cout << osize << "\n";
cout << stroutp.str();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1236 KB |
Output is correct |
2 |
Correct |
1 ms |
984 KB |
Output is correct |
3 |
Correct |
2 ms |
984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
3204 KB |
Output is correct |
2 |
Correct |
7 ms |
3212 KB |
Output is correct |
3 |
Correct |
8 ms |
3424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
38580 KB |
Output is correct |
2 |
Correct |
74 ms |
38812 KB |
Output is correct |
3 |
Correct |
69 ms |
37636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
44088 KB |
Output is correct |
2 |
Correct |
99 ms |
44168 KB |
Output is correct |
3 |
Correct |
85 ms |
43480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
47600 KB |
Output is correct |
2 |
Correct |
100 ms |
48084 KB |
Output is correct |
3 |
Runtime error |
57 ms |
65536 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |