CF1770H.Koxia, Mahiru and Winter Festival

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Wow, what a big face!

Kagura Mahiru

Koxia and Mahiru are enjoying the Winter Festival. The streets of the Winter Festival can be represented as a n×nn \times n undirected grid graph. Formally, the set of vertices is (i,j)    1i,jn{(i,j) \; | \; 1 \leq i,j\leq n } and two vertices (i1,j1)(i_1,j_1) and (i2,j2)(i_2,j_2) are connected by an edge if and only if i1i2+j1j2=1|i_1-i_2|+|j_1-j_2|=1.

A network with size n=3n = 3.

Koxia and Mahiru are planning to visit The Winter Festival by traversing 2n2n routes. Although routes are not planned yet, the endpoints of the routes are already planned as follows:

  • In the ii-th route, they want to start from vertex (1,i)(1, i) and end at vertex (n,pi)(n, p_i), where pp is a permutation of length nn.
  • In the (i+n)(i+n)-th route, they want to start from vertex (i,1)(i, 1) and end at vertex (qi,n)(q_i, n), where qq is a permutation of length nn.

A network with size n=3n = 3, points to be connected are shown in the same color for p=[3,2,1]p = [3, 2, 1] and q=[3,1,2]q = [3, 1, 2].

Your task is to find a routing scheme — 2n2n paths where each path connects the specified endpoints. Let's define the congestion of an edge as the number of times it is used (both directions combined) in the routing scheme. In order to ensure that Koxia and Mahiru won't get too bored because of traversing repeated edges, please find a routing scheme that minimizes the maximum congestion among all edges.

An example solution — the maximum congestion is 22, which is optimal in this case.

哇,好大的脸!

神乐麻里

小糸和麻里正在享受冬季祭典。冬季祭典的街道可以建模为一个 n×nn \times n 的无向网格图。形式化地说,顶点集为 {(i,j)1i,jn}\{(i,j) \mid 1 \leq i,j \leq n\},且当且仅当 i1i2+j1j2=1|i_1-i_2|+|j_1-j_2|=1 时,顶点 (i1,j1)(i_1,j_1)(i2,j2)(i_2,j_2) 之间存在一条边。

规模 n=3n = 3 的网络。

小糸和麻里计划通过 2n2n 条路径来游览冬季祭典。尽管这些路径尚未规划,但每条路径的端点已预先确定如下:

  • ii 条路径需从顶点 (1,i)(1, i) 出发,到达顶点 (n,pi)(n, p_i),其中 pp 是一个长度为 nn 的排列;
  • (i+n)(i+n) 条路径需从顶点 (i,1)(i, 1) 出发,到达顶点 (qi,n)(q_i, n),其中 qq 是一个长度为 nn 的排列。

规模 n=3n = 3 的网络,其中 p=[3,2,1]p = [3, 2, 1]q=[3,1,2]q = [3, 1, 2],相同颜色的点表示需被连接的端点对。

你的任务是设计一种路由方案——即找出 2n2n 条路径,使得每条路径均连接其指定的端点。我们定义一条边的拥塞度为该边在整套路由方案中被使用的总次数(两个方向合并计算)。为了确保小糸和麻里不会因反复经过相同边而感到乏味,请你找出一种使所有边中最大拥塞度最小化的路由方案。

一个示例解——最大拥塞度为 22,在此情形下是最优的。

输入格式

The first line contains an integer nn (2n2002 \leq n \leq 200) — the size of the network.

The second line contains nn integers p1,p2,,pnp_1, p_2, \dots, p_n (1pin1 \leq p_i \leq n).

The third line contains nn integers q1,q2,,qnq_1, q_2, \dots, q_n (1qin1 \leq q_i \leq n).

It is guaranteed that both pp and qq are permutations of length nn.

第一行包含一个整数 nn2n2002 \leq n \leq 200)—— 网络的规模。

第二行包含 nn 个整数 p1,p2,,pnp_1, p_2, \dots, p_n1pin1 \leq p_i \leq n)。

第三行包含 nn 个整数 q1,q2,,qnq_1, q_2, \dots, q_n1qin1 \leq q_i \leq n)。

保证 ppqq 均为长度为 nn 的排列。

输出格式

Output 2n2n lines, each line describing a route.

The first nn lines should describe the connections from top to bottom. The ii-th line should describe the route starting at vertex (1,i)(1, i) and ending at vertex (n,pi)(n, p_i).

The next nn lines should describe the connections from left to right. The (i+n)(i+n)-th line should describe the route starting at vertex (i,1)(i, 1) and ending at vertex (qi,n)(q_i, n).

Each line describing a route should start with an integer kk (2k1052 \le k \le 10^5) — the number of vertices the route passes, including the starting and ending vertices. Then output all the vertices on the route in order. In other words, if the route is (x1,y1)(x2,y2)(xk,yk)(x_1, y_1) \rightarrow (x_2, y_2) \rightarrow \dots \rightarrow (x_k, y_k), then output k x1 y1 x2 y2xk ykk~x_1~y_1~x_2~y_2 \ldots x_k~y_k. Note that xixi+1+yiyi+1=1|x_i-x_{i+1}|+|y_i-y_{i+1}| = 1 should holds for 1i<k1 \le i \lt k.

If there are multiple solutions that minimize the maximum congestion, you may output any.

输出 2n2n 行,每行描述一条路径。

nn 行应描述从上到下的连接。第 ii 行应描述从顶点 (1,i)(1, i) 出发、终止于顶点 (n,pi)(n, p_i) 的路径。

nn 行应描述从左到右的连接。第 (i+n)(i+n) 行应描述从顶点 (i,1)(i, 1) 出发、终止于顶点 (qi,n)(q_i, n) 的路径。

每条路径的描述行应以一个整数 kk2k1052 \le k \le 10^5)开头,表示该路径经过的顶点数(包括起点和终点)。随后按顺序输出路径上的所有顶点。换言之,若路径为 (x1,y1)(x2,y2)(xk,yk)(x_1, y_1) \rightarrow (x_2, y_2) \rightarrow \dots \rightarrow (x_k, y_k),则输出 k x1 y1 x2 y2xk ykk~x_1~y_1~x_2~y_2 \ldots x_k~y_k。注意:对所有 1i<k1 \le i < k,需满足 xixi+1+yiyi+1=1|x_i-x_{i+1}|+|y_i-y_{i+1}| = 1

若存在多个解均能最小化最大拥塞度,则输出任意一个即可。

输入输出样例

  • 输入#1

    3
    3 2 1
    3 1 2

    输出#1

    5 1 1 2 1 2 2 3 2 3 3 
    3 1 2 2 2 3 2 
    5 1 3 1 2 1 1 2 1 3 1 
    5 1 1 1 2 1 3 2 3 3 3
    4 2 1 2 2 2 3 1 3 
    4 3 1 3 2 3 3 2 3
  • 输入#2

    4
    3 4 2 1
    2 4 1 3

    输出#2

    6 1 1 1 2 2 2 2 3 3 3 4 3
    6 1 2 1 3 2 3 2 4 3 4 4 4
    5 1 3 2 3 2 2 3 2 4 2
    7 1 4 1 3 1 2 2 2 2 1 3 1 4 1
    7 1 1 2 1 3 1 3 2 3 3 2 3 2 4
    6 2 1 2 2 3 2 4 2 4 3 4 4
    6 3 1 3 2 3 3 3 4 2 4 1 4
    5 4 1 4 2 4 3 3 3 3 4
  • 输入#3

    3
    1 2 3
    1 2 3

    输出#3

    3 1 1 2 1 3 1 
    3 1 2 2 2 3 2 
    3 1 3 2 3 3 3 
    3 1 1 1 2 1 3 
    3 2 1 2 2 2 3 
    3 3 1 3 2 3 3

说明/提示

The first example corresponds to the figures in the problem statement.

The output for examples 22 and 33 respectively are visualized below:

Sample output for examples 22 and 33. Maximum congestions are 22 and 11 respectively.

第一个示例对应题目描述中的图示。

示例 2233 的输出分别如下图所示:

示例 2233 的输出样例。最大拥塞度分别为 2211

输入解题思路,AI测评打分。不知道怎么写?

首页