CF1172D.Nauuo and Portals

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Nauuo is a girl who loves playing games related to portals.

One day she was playing a game as follows.

In an n×nn\times n grid, the rows are numbered from 11 to nn from top to bottom, the columns are numbered from 11 to nn from left to right. We denote a cell on the intersection of the rr -th row and cc -th column as (r,c)(r,c) .

A portal is a pair of doors. You can travel from one of them to another without changing your direction. More formally, if you walk into a cell with a door, you will teleport to the cell with the other door of the same portal and then walk into the next cell facing the original direction. There can not be more than one doors in a single cell.

The "next cell" is the nearest cell in the direction you are facing. For example, if you are facing bottom, the next cell of (2,5)(2,5) is (3,5)(3,5) .

If you walk into a cell without a door, you must walk into the next cell after that without changing the direction. If the next cell does not exist, you must exit the grid.

You have to set some (possibly zero) portals in the grid, so that if you walk into (i,1)(i,1) facing right, you will eventually exit the grid from (ri,n)(r_i,n) , if you walk into (1,i)(1, i) facing bottom, you will exit the grid from (n,ci)(n,c_i) .

It is guaranteed that both r1..nr_{1..n} and c1..nc_{1..n} are permutations of nn elements. A permutation of nn elements is a sequence of numbers p1,p2,,pnp_1,p_2,\ldots,p_n in which every integer from 11 to nn appears exactly once.

She got confused while playing the game, can you help her to find a solution?

输入格式

The first line contains a single integer nn ( 1n10001\le n\le 1000 ) — the side length of the grid.

The second line contains nn integers r1,r2,,rnr_1,r_2,\ldots,r_n ( 1rin1\le r_i\le n ) — if you walk into (i,1)(i,1) facing right, you should exit the grid from (ri,n)(r_i,n) . It is guaranteed that r1..nr_{1..n} is a permutation of nn elements.

The third line contains nn integers c1,c2,,cnc_1,c_2,\ldots,c_n ( 1cin1\le c_i\le n ) — if you walk into (1,i)(1,i) facing bottom, you should exit the grid from (n,ci)(n,c_i) . It is guaranteed that c1..nc_{1..n} is a permutation of nn elements.

输出格式

If it is impossible to satisfy the rule, print the only number 1-1 .

Otherwise the first line should contain a single integer mm ( 0mn220\le m\le\frac{n^2}2 ) — the number of portals you set.

In the following mm lines, each line should contain four integers x1,y1,x2,y2x_1,y_1,x_2,y_2 , represents that you set a portal consisting of two doors in (x1,y1)(x_1,y_1) and (x2,y2)(x_2,y_2) .

If there are multiple answers, print any. You do not have to minimize mm .

输入输出样例

  • 输入#1

    3
    1 3 2
    3 1 2
    

    输出#1

    2
    1 1 1 3
    2 2 3 1
  • 输入#2

    5
    3 1 5 4 2
    4 2 1 3 5
    

    输出#2

    3
    1 1 3 4
    2 2 3 2
    2 3 5 1
    

说明/提示

Example 1

The cells with the same letter are a portal. You can set portals in this way:

It satisfies the rule, because:

Example 2

You can set portals in this way:

首页