Problem:
Score : 1500 points

Problem Statement

#nck {
width: 30px;
height: auto;
}

Construct an N-gon that satisfies the following conditions:

The polygon is simple (see notes for the definition).

Each edge of the polygon is parallel to one of the coordinate axes.

Each coordinate is an integer between 0 and 10^9, inclusive.

The vertices are numbered 1 through N in counter-clockwise order.

The internal angle at the i-th vertex is exactly a_i degrees.

In case there are multiple possible answers, you can output any.

Notes

A polygon is called simple if each edge has a positive length, and no two edges have a common point (except for adjacent edges touching at a vertex).

Constraints

3 ≤ N ≤ 1000

a_i is either 90 or 270.

Input

The input is given from Standard Input in the following format:

N
a_1
:
a_N

Output

In case the answer exists, print the answer in the following format:

x_1 y_1
:
x_N y_N

Here (x_i, y_i) are the coordinates of the i-th vertex.

In case the answer doesn't exist, print a single -1.

Sample Input 1

8
90
90
270
90
90
90
270
90

Sample Output 1

0 0
2 0
2 1
3 1
3 2
1 2
1 1
0 1

Sample Input 2

3
90
90
90

Sample Output 2

-1

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const deque<T> &vec){ os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template<typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
template<typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); }
template<typename T> bool mmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template<typename T> bool mmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }
template<typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
template<typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }
#define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl;
#define ALL(x) (x).begin(), (x).end()

int N;
vector<int> a;

vector<pint> ans;

constexpr int DMAX = 1 << 10;
int D;
void reshape(int DMAX=DMAX)
{
    set<int> X, Y;
    for (auto p : ans) X.insert(p.first), Y.insert(p.second);
    unordered_map<int, int> xz, yz;
    for (auto x : X)
    {
        int n = xz.size();
        xz[x] = n * DMAX;
    }
    for (auto y : Y)
    {
        int n = yz.size();
        yz[y] = n * DMAX;
    }
    vector<pint> ret;
    for (auto p : ans) ret.emplace_back(xz[p.first], yz[p.second]);
    ans = ret;
    D = DMAX / 2;
}

void dfs(int L, int R)
{
    if (L + 1 >= R) return;
    pint &A = ans[L], &B = ans[R];

    L++;
    int now = L + 1, cou = 0;
    while (cou or a[now] == a[L])
    {
        if (a[now] == 90) cou++;
        else cou--;
        now++;
    }
    // make pair between (L, now)
    pint X = make_pair((A.first + B.first) / 2, (A.second + B.second) / 2);
    pint Y = make_pair((A.first + B.first) / 2, (A.second + B.second) / 2);

    if (A.first == B.first)
    {
        if ((A.second < B.second) xor (a[L] == 90)) Y.first += D, B.first += D;
        else Y.first -= D, B.first -= D;
    }
    else
    {
        if ((A.first < B.first) xor (a[L] == 90)) Y.second -= D, B.second -= D;
        else Y.second += D, B.second += D;
    }
    
    ans[L] = X, ans[now] = Y;

    D /= 2;
    if (D == 0) reshape();
    dfs(L, now);
    dfs(now, R);
}

int main()
{
    cin >> N;
    a.resize(N * 2);
    REP(i, N)
    {
        cin >> a[i];
        a[i + N] = a[i];
    }

    vector<int> acc(N + 1);
    REP(i, N) acc[i + 1] = acc[i] + (a[i] == 90 ? 1 : -1);
    if (acc.back() != 4)
    {
        puts("-1");
        return 0;
    }

    vector<int> V(5);
    while (a[V[0]] != 90) V[0]++;
    REP(d, 3)
    {
        int acc = 0;
        V[d + 1] = V[d] + 1;
        while (acc != 0 or a[V[d + 1]] != 90)
        {
            if (a[V[d + 1]] == 90) acc++;
            else acc--;
            V[d + 1]++;
        }
    }
    V[4] = V[0] + N;
    ans.resize(2 * N);
    REP(i, 2) ans[V[0] + i * N] = make_pair(0, 0);
    REP(i, 2) ans[V[1] + i * N] = make_pair(DMAX, 0);
    REP(i, 2) ans[V[2] + i * N] = make_pair(DMAX, DMAX);
    REP(i, 2) ans[V[3] + i * N] = make_pair(0, DMAX);
    D = DMAX / 2;

    REP(i, 4) dfs(V[i], V[i + 1]);

    FOR(i, V[0], V[4] + 1) ans[i % N] = ans[i];
    reshape(1);
    REP(i, N) printf("%d %d\n", ans[i].first, ans[i].second);
}