codeforces1428 D. Bouncing Boomerangs(思维)

D. Bouncing Boomerangs

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

To improve the boomerang throwing skills of the animals, Zookeeper has set up an n×nn×n grid with some targets, where each row and each column has at most 22 targets each. The rows are numbered from 11 to nn from top to bottom, and the columns are numbered from 11to nn from left to right.

For each column, Zookeeper will throw a boomerang from the bottom of the column (below the grid) upwards. When the boomerang hits any target, it will bounce off, make a 9090 degree turn to the right and fly off in a straight line in its new direction. The boomerang can hit multiple targets and does not stop until it leaves the grid.

 

In the above example, n=6n=6 and the black crosses are the targets. The boomerang in column 11 (blue arrows) bounces 22 times while the boomerang in column 33 (red arrows) bounces 33 times.

The boomerang in column ii hits exactly aiai targets before flying out of the grid. It is known that ai≤3ai≤3.

However, Zookeeper has lost the original positions of the targets. Thus, he asks you to construct a valid configuration of targets that matches the number of hits for each column, or tell him that no such configuration exists. If multiple valid configurations exist, you may print any of them.

Input

The first line contains a single integer nn (1≤n≤105)(1≤n≤105).

The next line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤3)(0≤ai≤3).

Output

If no configuration of targets exist, print −1−1.

Otherwise, on the first line print a single integer tt (0≤t≤2n)(0≤t≤2n): the number of targets in your configuration.

Then print tt lines with two spaced integers each per line. Each line should contain two integers rr and cc (1≤r,c≤n)(1≤r,c≤n), where rr is the target's row and cc is the target's column. All targets should be different.

Every row and every column in your configuration should have at most two targets each.

Examples

input

Copy

6
2 0 3 0 1 1

output

Copy

5
2 1
2 5
3 3
3 6
5 6

input

Copy

1
0

output

Copy

0

input

Copy

6
3 2 2 2 1 1

output

Copy

-1

Note

For the first test, the answer configuration is the same as in the picture from the statement.

For the second test, the boomerang is not supposed to hit anything, so we can place 00 targets.

For the third test, the following configuration of targets matches the number of hits, but is not allowed as row 33 has 44 targets.

 

It can be shown for this test case that no valid configuration of targets will result in the given number of target hits.

题意:每行每列最多俩障碍物,飞镖撞到障碍物时会右转90度,现在每一列都有一个飞镖飞进来,给出每列飞镖的撞击次数,求一种障碍物的分布情况

思路:from here

按列从右向左构造,第 i 列的飞镖撞击了a[i]次,

(1)a[i] = 0,该列不能放障碍物

(2)a[i] = 1,在 (i, i) 处先放一个障碍物

(3)a[i] = 2,从右边选择一个还没用过的列 j ,且a[j] = 1,把该列的障碍物移动到第 i 行

(4)a[i] = 3,从右边选择一个还没有放满的列 j ,在该列多加一个障碍物(i, j)

如果某一步不能放说明不存在合法方案

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N], x[N << 1], y[N << 1];

int main() {
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
    int cnt = 0;
    stack<int>st1, st2;
    bool flag = 1;
    for(int i = n; i >= 1; --i) {
        if(a[i] == 1) st1.push(i);
        if(a[i] == 2) {
            x[++cnt] = i, y[cnt] = i;
            st2.push(i);
            if(!st1.empty()) {
                int top = st1.top();
                st1.pop();
                x[++cnt] = i, y[cnt] = top;
            }
            else {
                flag = 0;
                break;
            }
        }
        if(a[i] == 3) {
            x[++cnt] = i, y[cnt] = i;
            if(!st2.empty()) {
                int top = st2.top();
                st2.pop();
                x[++cnt] = i, y[cnt] = top;
                st2.push(i);
            }
            else if(!st1.empty()) {
                int top = st1.top();
                st1.pop();
                x[++cnt] = i, y[cnt] = top;
                x[++cnt] = top, y[cnt] = top;
                st2.push(i);
            }
            else {
                flag = 0;
                break;
            }
        }
    }
    if(!flag) {
        printf("-1\n");
        return 0;
    }
    while(!st1.empty()) {
        int top = st1.top();
        st1.pop();
        x[++cnt] = top, y[cnt] = top;
    }
    printf("%d\n", cnt);
    for(int i = 1; i <= cnt; ++i)
        printf("%d %d\n", x[i], y[i]);
    return 0;
}

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值