Problem:
Problem B
Amidakuji
Amidakuji is a method of lottery popular in East Asia.
It is often used when a number of gifts are distributed to people
or when a number of tasks are assigned.
Figure B-1: An example of amidakuji
Figure B-1 shows an example of amidakuji, which corresponds to the first dataset of Sample Input.
A number of vertical lines are drawn.
The number of lines is the same as the number of participants.
Some number of horizontal bars are drawn between some pairs of adjacent vertical lines.
The number and positions of these bars are arbitrary,
but no two bars should share their ends.
Usually, many horizontal bars are drawn at random.
To use an amidakuji, first, the names of an item, a gift or a task, are written down at the bottom end of vertical lines.
Then, each participant chooses the top end of a vertical line, different from one another.
Participants trace down the chosen line from the top.
When reaching a T-junction with a horizontal bar, that bar is followed, switching to the adjacent vertical line it is connected, and the trace continues downward.
When the bottom end of a vertical line is reached, the item written there is the awarded gift or the assigned task.
Figure B-2 shows the case when the top end P is chosen.
In this case, the item at R is obtained.
Figure B-2: The trace from P
Assume that the participant choosing P of Figure B-2 wants the item at Q, which is not fulfilled.
But wait, a special rule just introduced may grant that wish.
The new rule allows adding a single horizontal bar at an arbitrary position.
Actually, adding a bar marked X in Figure B-3 will make the trace reach Q.
Figure B-3: The new trace after adding a horizontal bar
Your task in this problem is to write a program that finds the position of a horizontal bar to add to the given amidakuji for making the trace starting from the top end of the specified vertical line reach the bottom end of the specified vertical line.
Input
The input consists of multiple datasets, each in the following format.
n
m
p
q
x
1
⋯
x
m
All the input items in a dataset are positive integers.
n
is the number of vertical lines.
m
is the number of horizontal bars in the original amidakuji.
The participant choosing the top end of the
p
-th line from the left wants the trace to reach the bottom end of the
q
-th line from the left.
You can assume 2 ≤
n
≤ 50,
m
≤ 500,
p
≤
n
, and
q
≤
n
hold.
x
1
, ..., and
x
m
give the positions of horizontal bars in the original amidakuji.
x
k
is the position of the
k
-th bar from the top.
Thus, vertical positions of all bars are different.
x
k
≤
n
− 1 holds.
The bar connects the
x
k
-th line from the left and the (
x
k
+ 1)-th line.
The end of the input is indicated by a line consisting of four zeros.
The number of datasets in the input does not exceed 300.
Output
For each dataset, output a single line, which is one of the following.
If
q
can be reached from
p
without adding a bar,
OK
.
If not, and adding a single bar cannot make
q
reachable from
p
,
N
...(truncated)

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
  while(true){
    int n, m, p, q;
    cin >> n >> m >> p >> q;
    if(!n) break;

    vector<int> amida(m);
    for(auto& i:amida) cin >> i;

    int save_p = p; //追加あり用にpを保存

    // 追加なしでの成立判定
    for(auto i:amida){ 
      if(p == i) p = i+1;
      else if(p == i+1) p = i;
    }
    if(p == q){
      cout << "OK" << endl;
      continue;
    } 

    // 追加ありでの成立判定
    p = save_p;
    int ans_x;
    bool end = false; // 終了判定
    bool pushed = false; //　挿入判定

    for(int i=0;i<=m;i++){
      for(int j=0;j<2;j++){ // 該当の縦線から左右に線を引くのをjで管理
        for(int k=0;k<amida.size();k++){
          int line;
          if(i==k && !pushed){ // 挿入処理
            line = p-1+j;
            ans_x = line;
            k--;
            pushed = true;
          }
          else if(i==m && k==m-1){ //　一番最後に横線を挿入
            line = amida[k];

            if(p == line) p = line+1;
            else if(p == line+1) p = line;
            
            line = p-1+j;
            ans_x = line;
            pushed = true;
          }
          else{
            line = amida[k];
          }
          if(p == line) p = line+1;
          else if(p == line+1) p = line;
        }
        if(p==q){
          cout << ans_x << ' ' << i << endl;
          end = true;
        }
        if(end) break;
        p = save_p; //値リセット
        pushed = false;
      }
      if(end) break;
    }
    if(end) continue;

    cout << "NG" << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3460
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
  while(true){
    int n, m, p, q;
    cin >> n >> m >> p >> q;
    if(!n) break;

    vector<int> amida(m);
    for(auto& i:amida) cin >> i;

    int save_p = p; //追加あり用にpを保存

    // 追加なしでの成立判定
    for(auto i:amida){ 
      if(p == i) p = i+1;
      else if(p == i+1) p = i;
    }
    if(p == q){
      cout << "OK" << endl;
      continue;
    } 

    // 追加ありでの成立判定
    p = save_p;
    int ans_x;
    bool end = false; // 終了判定
    bool pushed = false; //　挿入判定

    for(int i=0;i<=m;i++){
      for(int j=0;j<2;j++){ // 該当の縦線から左右に線を引くのをjで管理
        for(int k=0;k<amida.size();k++){
          int line;
          if(i==k && !pushed){ // 挿入処理
            line = p-1+j;
            ans_x = line;
            k--;
            pushed = true;
          }else if(i==m && k==m-1){
            line = amida[k];
            if(p == line) p = line+1;
            else if(p == line+1) p = line;
            line = p-1+j;
            ans_x = line;
            pushed = true;
          }else{
            line = amida[k];
          }
          if(p == line) p = line+1;
          else if(p == line+1) p = line;
        }
        if(p==q){
          cout << ans_x << ' ' << i << endl;
          end = true;
        }
        if(end) break;
        p = save_p; //値リセット
        pushed = false;
      }
      if(end) break;
    }
    if(end) continue;

    cout << "NG" << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3368
final_rank: 3

code_snippet: // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define rep(i,n) for(ll i=0;i<n;i++)

void solve(int n,int m,int p,int q){
    p--;
    q--;
    vector<int> a(m);
    rep(i,m) cin>>a[i];
    rep(i,m) a[i]--;

    int INF=1e9;

    int now=p;
    rep(j,m){
        if(a[j]==now) now++;
        else if(a[j]+1==now) now--;
    }

    if(now==q){
        cout << "OK" << endl;
        return;
    }

    for(int i=0;i<=m;i++){
        rep(k,2){
            int now=p,x=-1;
            rep(j,m){
                if(j==i){
                    x=now;
                    if(k==0) now--;
                    else now++;
                    if(now<0 && now>=n) now=-INF;
                }
                if(a[j]==now) now++;
                else if(a[j]+1==now) now--;
            }

            if(m==i){
                x=now;
                if(k==0) now--;
                else now++;
                if(now<0 && now>=n) now=-INF;
            }

            if(now==q){
                if(k==0) x--;
                cout << x+1 << " " << i << endl;
                return;
            }
        }
    }

    cout << "NG" << endl;
    return;
}

int main(){
    while(1){
        int n,m,p,q; cin>>n>>m>>p>>q;
        if(n==0) break;
        solve(n,m,p,q);
    }

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3456
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)
#define rrep(i, s, t) for(ll i = (ll)(t) - 1; i >= (ll)(s); i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)

#define TT template<typename T>
TT using vec = vector<T>;
template<class T1, class T2> bool chmin(T1 &x, T2 y) { return x > y ? (x = y, true) : false; }
template<class T1, class T2> bool chmax(T1 &x, T2 y) { return x < y ? (x = y, true) : false; }

struct io_setup {
    io_setup() {
        ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        cout << fixed << setprecision(15);
    }
} io_setup;

int main() {
    while(1){
        int N,M,P,Q;
        cin>>N>>M>>P>>Q;
        if(N==0)return 0;
        P--;
        Q--;
        vector<int>X(M);
        rep(i,0,M){
            cin>>X[i];
            X[i]--;
        }
        {
            int now=P;
            rep(i,0,M){
                if(now==X[i])now++;
                else if(now==X[i]+1)now--;
            }
            if(now==Q){
                cout<<"OK"<<endl;
                continue;
            }
        }
        bool flag=false;
        for(int y=0;y<=M;y++){
            rep(x,0,N-1){
                int now=P;
                vector<int>Z;
                rep(i,0,M){
                    if(y==i)Z.push_back(x);
                    Z.push_back(X[i]);
                }
                if(y==M)Z.push_back(x);
                rep(i,0,M+1){
                    if(now==Z[i])now++;
                    else if(now==Z[i]+1)now--;
                }
                if(now==Q){
                    cout<<x+1<<" "<<y<<endl;
                    flag=true;
                    break;
                }
            }
            if(flag)break;
        }
        if(!flag){
            cout<<"NG"<<endl;
        }
    }
}
accuracy: 1
time_ms: 230
memory_kb: 3584
final_rank: 19

code_snippet: #include <bits/stdc++.h>

using namespace std;

int main() {
    while (true) {
        int n, m, p, q;
        cin >> n >> m >> p >> q;
        if (n == 0 && m == 0 && p == 0 && q == 0) {
            break;
        }
        vector<pair<int, int>> xs;
        for (int i = 0; m > i; i++) {
            int x;
            cin >> x;
            xs.push_back(make_pair(x, x + 1));
        }
        xs.push_back(make_pair(0, 0));

        int cur = p;
        for (int i = 0; xs.size() > i; i++) {
            auto [a, b] = xs[i];
            if (cur == a) {
                cur = b;
            }
            else if (cur == b) {
                cur = a;
            }
        }
        // cout << cur << endl;
        if (cur == q) {
            cout << "OK" << endl;
            continue;
        }

        auto flag = false;
        for (auto r = 0; xs.size() > r; r++) {
            for (int d = -1; 1 >= d; d++) {
                int cur = p;
                int pos = -1;
                for (int i = 0; xs.size() > i; i++) {
                    auto [a, b] = xs[i];
                    if (i == r) {
                        if (1 <= cur + d && cur + d <= n) {
                            pos = min(cur + d, cur);
                            cur += d;
                        }
                    }
                    if (cur == a) {
                        cur = b;
                    }
                    else if (cur == b) {
                        cur = a;
                    }
                }
                if (cur == q && pos != -1) {
                    cout << pos << ' '
                         << r << endl;
                    flag = true;
                    break;
                }
            }
            if (flag) {
                break;
            }
        }
        if (!flag) {
            cout << "NG" << endl;
        }
    }
    return EXIT_SUCCESS;
}
accuracy: 1
time_ms: 10
memory_kb: 3584
final_rank: 11

code_snippet: #include <cstdio>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
int main()
{
    while (true) 
    {
        int n, m, p, q;
        cin >> n >> m >> p >> q;
        if (n + m + p + q == 0) break;
        vector<int> x(m);
        rep(i, m) cin >> x[i];

        int pos = p;

        rep(i, m)
        {
            if (x[i] == pos)
            {
                pos++;
            }
            else if (x[i] + 1 == pos)
            {
                pos--;
            }
        }

        if (pos == q)
        {
            cout << "OK" << endl;
        } 
        else
        {
            pair<int, int> ans;
            ans.first = 1e9;
            ans.second = 1e9;

            for(int i = 0; i <= m; i++)
            {
                int pos = p;
                int X = 1e9, Y = 1e9;
                // i回目に横線を追加するとしてシミュレーション
                for (int j = 0; j < m; j++)
                {
                    if (i == j)
                    {
                        X = pos;
                        Y = j;
                        pos++;
                    }

                    if (x[j] == pos)
                    {
                        pos++;
                    }
                    else if (x[j] + 1 == pos)
                    {
                        pos--;
                    }

                    if (j == m - 1 && i == m)
                    {
                        X = pos;
                        Y = i;
                        pos++;
                    }

                }

                if (pos == q)
                {
                    if (ans.second > Y)
                    {
                        ans = {X, Y};
                    }
                }
            }

            for (int i = 0; i <= m; i++)
            {
                int X = 1e9, Y = 1e9;
                int pos = p;
                for (int j = 0; j < m; j++)
                {
                    if (i == j)
                    {
                        X = pos;
                        Y = i;
                        pos--;
                    }

                    if (x[j] == pos)
                    {
                        pos++;
                    }
                    else if (x[j] + 1 == pos)
                    {
                        pos--;
                    }

                    if (i == m && j == m - 1)
                    {
                        X = pos;
                        pos--;
                        Y = i;
                    }
                }

                if (pos == q)
                {
                    if (ans.second > Y)
                    {
                        ans = {X - 1, Y};
                    }
                }
            }

            if (ans.first != 1e9)
            {
                auto [i, j] = ans;
                cout << i << " " << j << endl;
            }
            else 
            {
                cout << "NG" << endl;
            }
        }
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3220
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
  #include "settings/debug.cpp"
#else
  #define Debug(...) void(0)
#endif
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using ull = unsigned long long;

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  while (true) {
    int w, h, p, q;
    cin >> w >> h >> p >> q;
    if (w + h + p + q == 0) break;
    vector<int> x(h);
    rep(i, h) cin >> x[i];
    auto simulate = [&]() {
      int now = p;
      rep(i, x.size()) {
        if (x[i] == now - 1)
          now -= 1;
        else if (x[i] == now)
          now += 1;
      }
      return now == q;
    };
    if (simulate()) {
      cout << "OK" << '\n';
      continue;
    }
    pair<int, int> ans = { -1, -1 };
    rep(i, h + 1) {
      rep(j, w - 1) {
        x.insert(x.begin() + i, j + 1);
        if (simulate()) {
          ans = { i, j + 1 };
          break;
        }
        x.erase(x.begin() + i);
      }
      if (ans.first != -1) break;
    }
    if (ans.first == -1) {
      cout << "NG" << '\n';
    }
    else {
      cout << ans.second << ' ' << ans.first << '\n';
    }
  }
  return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 3584
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll  = long long;
using ull = unsigned long long;
#define rep(i, n) for(ll i = 0; i < n; i++)
constexpr ll INF = ((1LL << 61) + (1LL << 30) - 1);

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

  while(true) {
    ll n, m, p, q;
    cin >> n >> m >> p >> q;
    if(n == 0) break;
    p--;
    q--;

    vector<ll> x(m);
    rep(i, m) {
      cin >> x[i];
      x[i]--;
    }
    auto check = [&](const vector<ll>& xx) -> bool {
      ll now = p;
      rep(i, xx.size()) {
        if(now == xx[i]) now = xx[i] + 1;
        else if(now == xx[i] + 1) now = xx[i];
      }
      return now == q;
    };
    if(check(x)) {
      cout << "OK" << endl;
      continue;
    }

    ll ans_x = -1, ans_y = -1;
    rep(y, m + 1) {
      rep(nx, n - 1) {
        vector<ll> next_x;
        rep(i, m) {
          if(i == y) next_x.emplace_back(nx);
          next_x.emplace_back(x[i]);
        }
        if(y == m) next_x.emplace_back(nx);
        if(check(next_x)) {
          ans_x = nx + 1;
          ans_y = y;
          break;
        }
      }
      if(ans_x != -1) break;
    }
    if(ans_x == -1) cout << "NG" << endl;
    else cout << ans_x << " " << ans_y << endl;
  }

  return 0;
}
accuracy: 1
time_ms: 380
memory_kb: 3492
final_rank: 20

code_snippet: #include<bits/stdc++.h>
using namespace std;

#define rep(i,l,r) for(int i=(l);(i)<(r);i++)
#define rrep(i,l,r) for(int i=(r-1);(l)<=(i);i--)

int main(){
    while(true){
        int N,M,p,q; cin>>N>>M>>p>>q; p--,q--;
        if(N==0)return 0;
        vector<int> X(M); rep(i,0,M)(cin>>X[i]),X[i]--;
        vector<int> P1(N); rep(i,0,N)P1[i]=i;
        vector<vector<int>> p1,p2;
        vector<int> P2(N); rep(i,0,N)P2[i]=i;
        p1.push_back(P1);
        rep(i,0,M){
            swap(P1[X[i]],P1[X[i]+1]);
            p1.push_back(P1);
        }
        p2.push_back(P2);
        rrep(i,0,M){
            swap(P2[X[i]],P2[X[i]+1]);
            p2.push_back(P2);
        }
        reverse(p2.begin(),p2.end());
        // rep(i,0,M+1){
        //     rep(j,0,N)cout<<p1[i][j]<<' ';
        //     cout<<endl;
        // }
        // cout<<endl<<endl;

        // rep(i,0,M+1){
        //     rep(j,0,N)cout<<p2[i][j]<<' ';
        //     cout<<endl;
        // }
        // cout<<endl;
        vector<pair<int,int>> ans;
        rep(j,0,N){
            if(p1[0][j]==p&&p2[0][j]==q){
                cout<<"OK"<<endl;
                goto g;
            }
        }
        rep(i,0,M+1){
            rep(j,0,N-1){
                // cout<<i<<' '<<j<<' '<<p1[i][j]<<' '<<p2[i][j+1]<<' ';
                // cout<<(p1[i][j]==p)<<' '<<(p2[i][j+1]==q)<<endl;
                if((p1[i][j]==p&&p2[i][j+1]==q)||(p1[i][j+1]==p&&p2[i][j]==q)){
                    ans.push_back({i,j+1});
                }
            }
        }
        if(ans.size()==0)cout<<"NG"<<endl;
        else{
            sort(ans.begin(),ans.end());
            cout<<ans[0].second<<' '<<ans[0].first<<endl;
        }
        // cout<<p<<' '<<q<<endl;
        g:;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3300
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;

int nextcp(int cp, int cx) {
  if (cp == cx) {
    return cp + 1;
  } else if (cp == cx + 1) {
    return cp - 1;
  } else {
    return cp;
  }
}

bool check(int n, int m, int p, int q, vector<int>& x) {
  int cp = p;
  for (int i = 0; i < m; i++) {
    int cx = x[i]-1;
    cp = nextcp(cp, cx);
  }
  return cp == q;
}

bool check(int ax, int ay, int n, int m, int p, int q, vector<int>& x) {
  int cp = p;
  for (int i = 0; i <= m; i++) {
    if (ay == i) {
      int cx = ax;
      cp = nextcp(cp, cx);
    }
    if (i != m) {
      int cx = x[i]-1;
      cp = nextcp(cp, cx);
    }
  }
  return cp == q;
}

int main() {
  while (1) {
    int n, m, p, q;
    cin >> n >> m >> p >> q;
    if (n == 0 and m == 0 and p == 0 and q == 0) break;
    p--;q--;
    vector<int> x(m);
    for (int i = 0; i < m; i++) {
      cin >> x.at(i);
    }
    if (check(n, m, p, q, x)) {
      cout << "OK" << endl;
    } else {
      bool f = true;
      for (int ay = 0; ay <= m; ay++) {
        for (int ax = 0; ax < n - 1; ax++) {
          if (check(ax, ay, n, m, p, q, x)) {
            cout << ax + 1 << " " << ay << endl;
            f = false;
            break;
          }
        }
        if (!f) break;
      }
      if (f) {
        cout << "NG" << endl;
      }
    }
  }
}
accuracy: 1
time_ms: 100
memory_kb: 3456
final_rank: 10

code_snippet: #pragma region Macros
#include <bits/stdc++.h>

#include <atcoder/all>
using namespace std;
using namespace atcoder;
using lint = long long;
using mint = modint998244353;
using ull = unsigned long long;
using ld = long double;
using int128 = __int128_t;
#define all(x) (x).begin(), (x).end()
#define EPS 1e-8
#define uniqv(v) v.erase(unique(all(v)), v.end())
#define OVERLOAD_REP(_1, _2, _3, name, ...) name
#define REP1(i, n) for (auto i = std::decay_t<decltype(n)>{}; (i) != (n); ++(i))
#define REP2(i, l, r) for (auto i = (l); (i) != (r); ++(i))
#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
#define log(x) cout << x << endl
#define logfixed(x) cout << fixed << setprecision(10) << x << endl;
#define logy(bool)         \
  if (bool) {              \
    cout << "Yes" << endl; \
  } else {                 \
    cout << "No" << endl;  \
  }

ostream &operator<<(ostream &dest, __int128_t value) {
  ostream::sentry s(dest);
  if (s) {
    __uint128_t tmp = value < 0 ? -value : value;
    char buffer[128];
    char *d = end(buffer);
    do {
      --d;
      *d = "0123456789"[tmp % 10];
      tmp /= 10;
    } while (tmp != 0);
    if (value < 0) {
      --d;
      *d = '-';
    }
    int len = end(buffer) - d;
    if (dest.rdbuf()->sputn(d, len) != len) {
      dest.setstate(ios_base::badbit);
    }
  }
  return dest;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++) {
    os << v[i] << (i + 1 != (int)v.size() ? " " : "");
  }
  return os;
}

template <typename T>
ostream &operator<<(ostream &os, const set<T> &set_var) {
  for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
    os << *itr;
    ++itr;
    if (itr != set_var.end()) os << " ";
    itr--;
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
  for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
    os << itr->first << " -> " << itr->second << "\n";
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &pair_var) {
  os << "(" << pair_var.first << ", " << pair_var.second << ")";
  return os;
}

void out() { cout << '\n'; }
template <class T, class... Ts>
void out(const T &a, const Ts &...b) {
  cout << a;
  (cout << ... << (cout << ' ', b));
  cout << '\n';
}

template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (T &in : v) is >> in;
  return is;
}

inline void in(void) { return; }
template <typename First, typename... Rest>
void in(First &first, Rest &...rest) {
  cin >> first;
  in(rest...);
  return;
}

template <typename T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

vector<lint> dx8 = {1, 1, 0, -1, -1, -1, 0, 1};
vector<lint> dy8 = {0, 1, 1, 1, 0, -1, -1, -1};
vector<lint> dx4 = {1, 0, -1, 0};
vector<lint> dy4 = {0, 1, 0, -1};

#pragma endregion

bool check(const vector<int> &x, int p, int q) {
  int cur = p;
  int n = x.size();
  rep(i, n) {
    if (cur == x[i]) {
      cur++;
    } else if (cur - 1 == x[i]) {
      cur--;
    }
  }

  if (cur == q) {
    return true;
  } else {
    return false;
  }
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n, m, p, q;
  while (1) {
    in(n, m, p, q);
    if (n == 0 and m == 0 and p == 0 and q == 0) break;

    vector<int> l(m);
    in(l);
    bool fin = false;
    if (check(l, p, q)) {
      out("OK");
      continue;
    }
    rep(y, m + 1) {
      rep(x, 1, n) {
        vector<int> tmp;

        rep(i, y) {
          tmp.emplace_back(l[i]);
        }
        tmp.emplace_back(x);
        rep(i, y, m) {
          tmp.emplace_back(l[i]);
        }

        bool res = check(tmp, p, q);
        if (res) {
          out(x, y);
          fin = 1;
          break;
        }
      }
      if (fin) break;
    }

    if (!fin) {
      out("NG");
    }
  }
}
accuracy: 1
time_ms: 210
memory_kb: 3584
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
ld pie = 3.14159265393;
ll mod = 998244353;
ll inf = 100900000;
int main(){
    vector<string>ans;
    while (1)
    {
        ll n,m,p,q;
        cin >> n >> m >> p >> q;
        if (n==0)
        {
            break;
        }
        
        p--,q--;
        vector<ll>x(m);
        for (ll i = 0; i < m; i++)
        {
            cin >> x[i];
            x[i]--;
        }
        ll now=p;
        for (ll i = 0; i < m; i++)
        {
            if (x[i]==now)
            {
                now++;
            }else if (x[i]==now-1)
            {
                now--;
            }
        }
        if (now==q)
        {
            ans.push_back("OK");
            continue;
        }
        ll xx=-1,yy=-1;
        for (ll i = 0; i < m+1; i++)
        {
            for (ll j = 0; j < n-1; j++)
            {
                ll now=p;
                for (ll k = 0; k < m+1; k++)
                {
                    if (i==k)
                    {
                        if (j==now)
                        {
                            now++;
                        }else if (j==now-1)
                        {
                            now--;
                        }
                    }else if (k>i)
                    {
                        if (x[k-1]==now)
                        {
                            now++;
                        }else if (x[k-1]==now-1)
                        {
                            now--;
                        }
                    }else{
                        if (x[k]==now)
                        {
                            now++;
                        }else if (x[k]==now-1)
                        {
                            now--;
                        }
                    }
                }
                if (now==q)
                {
                    xx=i,yy=j;
                    break;
                }
            }
            if (xx!=-1)
            {
                break;
            }
            
        }
        if (xx!=-1)
        {
            string s=to_string(yy+1);
            s.push_back(' ');
            s+=to_string(xx);
            ans.push_back(s);
        }else{
            ans.push_back("NG");
        }
        
    }
    
    for (ll i = 0; i < ans.size(); i++)
    {
        cout << ans[i] << endl;
    }
    
}
accuracy: 1
time_ms: 140
memory_kb: 3520
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using a2 = array<ll, 2>;
using a3 = array<ll, 3>;

template <typename A>
void chmin(A &l, const A &r) {
    if (r < l)
        l = r;
}
template <typename A>
void chmax(A &l, const A &r) {
    if (l < r)
        l = r;
}

ll mod = 998244353;

ll n, m, p, q;
vector<ll> v;
void input() {
    cin >> n >> m >> p >> q;
    v.resize(m);
    for (auto &x : v) cin >> x;
}

void solve() {
    p--;
    q--;
    for (auto &x : v)
        x--;

    ll now = p;
    for (int i = 0; i < m; i++) {
        if (v[i] == now) {
            now = v[i] + 1;
        } else if (v[i] + 1 == now) {
            now = v[i];
        }
    }
    if (now == q) {
        cout << "OK" << endl;
        return;
    }

    for (int j = 0; j < m + 1; j++) {
        for (int k = 0; k < n - 1; k++) {
            ll now = p;
            for (int i = 0; i < m; i++) {
                if (i == j) {
                    if (k == now) {
                        now = k + 1;
                    } else if (k + 1 == now) {
                        now = k;
                    }
                }
                if (v[i] == now) {
                    now = v[i] + 1;
                } else if (v[i] + 1 == now) {
                    now = v[i];
                }
            }
            if (j == m) {
                if (k == now) {
                    now = k + 1;
                } else if (k + 1 == now) {
                    now = k;
                }
            }
            if (now == q) {
                cout << k+1 << ' ' << j << endl;
                return;
            }
        }
    }
    cout << "NG" << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    while (1) {
        input();
        if (n == 0)
            break;
        solve();
    }

    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 3380
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
using namespace std;
using ll = long long;//10^18まで
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll,ll>;

#define each(variable, container) for(auto &variable : container) 
#define time(variable,end) for(ll variable = 0; variable < end; variable++)
#define rep(variable, start, end) for(ll variable = start; variable < end; variable++)
#define INF 1000390039

template <typename T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    for (int i = 0; i < (int)v.size(); i++)
        os << v[i] << (i + 1 != (int)v.size() ? " " : "");
    return os;
}
template <typename T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
    for (T &in : v)
        is >> in;
    return is;
}

int main(){
    //ofstream cout("./output.txt");

    while(1){
        bool ok = 0;
        int n,m,p,q;
        cin >> n >> m >> p >> q;
        if(n == 0 && m == 0){
            return 0;
        }

        vector<int> A(m);
        cin >> A;

        //initial state
        int pos = p;
        rep(i,0,m){
            if(A[i] == pos)pos++;
            else if(A[i] + 1 == pos)pos--;

        }
        if(pos == q){
            cout << "OK" << endl;
            ok = 1;
        }
        A.push_back({INF});
        m++;

        rep(i,0,m){
            rep(j,0,n){
                pos = p;
                rep(idx,0,m){
                    if(i == idx){
                        if(j == pos)pos++;
                        else if(j+1 == pos)pos--;
                    }
                    int e = A[idx];
                    if(e == pos)pos++;
                    else if(e + 1 == pos)pos--;
                }
                if(pos == q && !ok){
                    cout << j << ' ' << i << endl;
                    ok = 1;
                }
            }
        }
        if(!ok)cout << "NG" << endl;
    }
    

}
accuracy: 1
time_ms: 150
memory_kb: 3476
final_rank: 13

code_snippet: #include<bits/stdc++.h>
using namespace std;

void solve(int n, int m, int p, int q){
    vector<int> x(m);
    for (int i = 0; i < m; i++)cin >> x[i];
    
    int now = p;

    for (int i = 0; i < m; i++){
        if (now == x[i])now++;
        else if (now == x[i]+1)now--;
    }
    if (now == q){
        cout << "OK" << endl;
        return;
    }


    for (int i = 0; i <= m; i++){
        for (int j = 1; j < n; j++){
            now = p;
            for (int k = 0; k < m; k++){
                if (i == k){
                    if (now == j)now++;
                    else if (now == j+1)now--;
                }
                if (now == x[k])now++;
                else if (now == x[k]+1)now--;
            }
            if (i == m){
                if (now == j)now++;
                else if (now == j+1)now--;
            }
            if (now == q){
                cout << j << ' ' << i << endl;
                return;
            }
        }
    }
    cout << "NG" << endl;
}

int main(){
    while(1){
        int n,m,p,q;
        cin >> n >> m >> p >> q;
        if (n == 0)return 0;
        solve(n,m,p,q);
    }
}
accuracy: 1
time_ms: 130
memory_kb: 3368
final_rank: 9

code_snippet: # pragma GCC target("avx")
# pragma GCC optimize("O3")
# pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

#include <atcoder/all>
using namespace atcoder;

using ll = long long;
using mint = modint998244353;

bool solve() {
  int N, M, P, Q;
  cin >> N >> M >> P >> Q;
  if(N == 0) return false;
  P--, Q--;
  auto CHECK = [&](vector<int> a) {
    int p = P;
    for(int x : a) {
      // cout << p + 1 << endl;
      if(p == x) p++;
      else if(p == x + 1) p--;
    }
    // cout << p + 1 << endl;
    return p == Q;
  };
  vector<int> A(M);
  for(int &x : A) cin >> x;
  for(int &x : A) x--;
  if(CHECK(A)) {
    cout << "OK" << endl;
    return true;
  }
  for(int y = 0; y <= M; y++) {
    for(int x = 0; x < N; x++) {
      vector<int> B = A;
      B.insert(B.begin() + y, x);
      // for(int v : B) cout << v + 1 << " "; cout << "\n";
      if(CHECK(B)) {
        cout << x + 1 << " " << y << endl;
        return true;
      }
    }
  }
  cout << "NG" << endl;
  return true;
}

int main() {
  while(solve());
}
accuracy: 1
time_ms: 90
memory_kb: 3584
final_rank: 15

code_snippet: // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
#define rep(i, a, b) for(int i = (a); i < (b); i++) // [a, b)を昇順に
#define drep(i, a, b) for(int i = (b) - 1; i >= a; i--) // [a, b)を降順に
#define all(v) v.begin(), v.end() // 昇順sortに
#define rall(v) v.rbegin(), v.rend() // 降順sortに
#define sz(x) (int)(x).size()
#define decimal(n) cout << fixed << setprecision(n);
#define debug(x) cerr << #x << " = " << x << endl;
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define yesno(condition) { if (condition) cout << "Yes" << endl; else cout << "No" << endl; }
using vi = vector<int>; using vvi = vector<vi>; using vs = vector<string>;
using ll = long long; using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>;
using lll = __int128;
// using mint = modint998244353;
const ll MOD = 998244353;
// using mint = modint;
// const ll MOD = 998244353;
// using mint = modint1000000007;
// const ll MOD = 1000000007;
// using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>;
using P = pair<ll, ll>; using T = tuple<ll, ll, ll>;
using vc = vector<char>;
const ll MAX = 5'000'009; const ll llINF = 2e18;
const int intINF = 2e9; const long double pi = acos(-1);
ll di[] = {0, -1, 0, 1, -1, -1, 1, 1};
ll dj[] = {1, 0, -1, 0, 1, -1, -1, 1};
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
template<class T> using pq = priority_queue<T, vector<T>>; //大きい順
template<class T> using pq_g = priority_queue<T, vector<T>, greater<T>>; //小さい順

bool isin(ll i, ll j, ll n, ll m) { return (0 <= i && i < n && 0 <= j && j < m); }
bool is_multipul_overflow(ll a, ll b, ll c) {
    // a * b > cかを判定する
    if (a == 0 || b == 0) return false;
    return (a <= c / b ? false : true);
}



void solve(int &n, int &m, int &p, int &q) {
    auto change = [](int &pp, int xx) -> void {
        if(pp == xx) pp = xx + 1;
        else if(pp == xx + 1) pp = xx;
    };

    p--; q--;
    vector<int> x(m);
    rep(i, 0, m) {
        cin >> x[i];
        x[i]--;
    }

    int cur = p;
    rep(i, 0, m) {
        change(cur, x[i]);
    }
    if(cur == q) {
        cout << "OK" << endl;
        return ;
    }

    vector<pair<int, int>> candidate;

    cur = p;
    rep(i, 0, m + 1) {
        rep(j, 0, n) {
            if(j == cur) {
                candidate.emplace_back(make_pair(i, j - 1));
                if(j + 1 < n) candidate.emplace_back(make_pair(i, j));
            }
        }

        if(i == m) break;

        change(cur, x[i]);
    }

    for(auto [ansy, ansx] : candidate) {
        cur = p;
        rep(i, 0, m + 1) {
            if(ansy == i) {
                change(cur, ansx);
            }
            if(i == m) break;
            change(cur, x[i]);
        }
        if(cur == q) {
            cout << ansx + 1 << " " << ansy << endl;
            return ;
        }
    }
    cout << "NG" << endl;
    return ;
}

int main() {
    int n, m, p, q;
    while(true) {
        cin >> n >> m >> p >> q;
        if(n == 0) return 0;
        solve(n, m, p, q);
    }
    
}
accuracy: 1
time_ms: 10
memory_kb: 3376
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
    while(true) {
        int N, M, P, Q;
        cin >> N >> M >> P >> Q;
        if(N == 0) break;
        --P; --Q;
        vector<int> X(M);
        for(int i=0; i<M; ++i) {
            cin >> X[i];
            --X[i];
        }

        int line = P;
        for(int i=0; i<M; ++i) {
            if(X[i] == line-1) --line;
            else if(X[i] == line) ++line;
        }
        if(line == Q) {
            cout << "OK" << endl;
            continue;
        }

        bool ok = false;
        for(int y=0; y<2*M; ++y) {
            int line = P;
            int x;
            for(int i=0; i<M; ++i) {
                if(i == y/2) {
                    if(0 < line && y%2 == 0) {
                        x = line-1;
                        --line;
                    } else if(line < N-1 && y%2 == 1) {
                        x = line;
                        ++line;
                    }
                }
                if(X[i] == line-1) --line;
                else if(X[i] == line) ++line;
            }
            if(line == Q) {
                ok = true;
                cout << x+1 << ' ' << y/2 << endl;
                break;
            }
        }
        if(ok) continue;

        line = P;
        for(int i=0; i<M; ++i) {
            if(X[i] == line-1) --line;
            else if(X[i] == line) ++line;
        }
        if(line == Q-1) {
            cout << line+1 << ' ' << M << endl;
            continue;
        }
        if(line == Q+1) {
            cout << Q+1 << ' ' << M << endl;
            continue;
        }

        cout << "NG" << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3584
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
//#include "atcoder/dsu.hpp"
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
using ll= long long;
using ull=unsigned long long;
using ldo =long double;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)
#define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__)
#define OVERLOAD_sum(_1, _2, _3,_4, name, ...) name
template<typename T>ll sum1(T &a){ll ans = 0;for(ll i=0;i<a.size();i++)ans+=a[i];return ans;}
template<typename T>ll sum2(T &a,ll start){ll ans = 0;for(ll i=start;i<a.size();i++)ans+=a[i];return ans;}
template<typename T>ll sum3(T &a,ll start,ll en){ll ans = 0;for(ll i=start;i<en;i++)ans+=a[i];return ans;}
template<typename T>ll sum4(T &a,ll start,ll en,ll tolerance){ll ans = 0;for(ll i=start;i<en;i+=tolerance)ans+=a[i];return ans;}
#define sum(...) OVERLOAD_sum(__VA_ARGS__,sum4, sum3, sum2, sum1)(__VA_ARGS__)
#define OVERLOAD_rep(_1, _2, _3,_4, name, ...) name
#define rep(i,n,k) for(ll i = k; i < (ll)(n); i++)


int amida(int n, int m, int p_start, const vector<int>& original_lines, int add_x, int add_y) {
    int current_pos = p_start; 
    for (int y_level = 0; y_level <= m; ++y_level){
        if (add_x != 0 && y_level == add_y) {
            if (current_pos == add_x) {
                current_pos++;
            } else if (current_pos == add_x + 1) {
                current_pos--;
            }
        }
        if (y_level < m) { 
            int original_line_x = original_lines[y_level]; 
            if (current_pos == original_line_x) {
                current_pos++;
            } else if (current_pos == original_line_x + 1) {
                current_pos--;
            }
        }
    }
    return current_pos;
}

int main() {
  while(true){
      int n, m, p_start_orig, q_target_orig;
      cin >> n >> m >> p_start_orig >> q_target_orig;
      if (n == 0 && m == 0 && p_start_orig == 0 && q_target_orig == 0) {
          break;
      }
      vector<int> original_lines(m);
      rep(i, m, 0) {
          cin >> original_lines[i];
      }
      if (amida(n, m, p_start_orig, original_lines, 0, 0) == q_target_orig) {
          cout << "OK" << endl;
          continue;
      }
      int best_add_x = -1;
      int best_add_y = -1;
      for (int add_y_try = 0; add_y_try <= m; ++add_y_try) { 
          for (int add_x_try = 1; add_x_try < n; ++add_x_try) { 
              if (amida(n, m, p_start_orig, original_lines, add_x_try, add_y_try) == q_target_orig) {
                  if (best_add_y == -1 || add_y_try < best_add_y) {
                      best_add_y = add_y_try;
                      best_add_x = add_x_try;
                  } else if (add_y_try == best_add_y) {
                  }
              }
          }
      }
      if (best_add_x != -1) {
          cout << best_add_x << " " << best_add_y << endl;
      } else {
          cout << "NG" << endl;
      }
    }
  return 0;
}
accuracy: 1
time_ms: 270
memory_kb: 3408
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
// using mint = modint998244353;
// using mint = modint1000000007;
using ll  = long long;
using ull = unsigned long long;
#define rep(i, n) for(ll i = 0; i < n; i++)
constexpr ll INF = ((1LL << 61) + (1LL << 30) - 1);

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

  while(true) {
    ll n, m, p, q;
    cin >> n >> m >> p >> q;
    if(n == 0) break;
    p--, q--;
    vector<ll> x(m);
    rep(i, m) {
      cin >> x[i];
      x[i]--;
    }
    auto check = [&](ll a, ll b) -> bool {
      // 上から a 番目, b ⇔ b + 1 に対して線を追加する
      ll now = p;
      rep(i, m) {
        if(i == a) {
          if(b + 1 == now) now = b;
          else if(b == now) now = b + 1;
        }
        if(x[i] + 1 == now) now = x[i];
        else if(x[i] == now) now = x[i] + 1;
      }
      if(a == m) {
        if(b == now) now = b + 1;
        else if(b + 1 == now) now = b;
      }
      return now == q;
    };
    if(check(-1, -1)) {
      cout << "OK" << endl;
      continue;
    }
    bool ok = false;
    rep(j, m + 1) {
      rep(i, n - 1) {
        if(check(j, i)) {
          cout << i + 1 << " " << j << endl;
          ok = true;
          break;
        }
      }
      if(ok) break;
    }
    if(!ok) cout << "NG" << endl;
  }

  return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 3368
final_rank: 5