QOJ.ac

QOJ

Time Limit: 1.5 s Memory Limit: 256 MB Total points: 100

#18414. 目标旋转

Statistics

在 Djelfa 市,玩家 Ahmad Karawita 和 Hamoud Habibi Hamoud 喜欢玩一种独特的拼图,它由若干同心圆盘组成。一共有 $n$ 个圆盘,分别命名为 $d_1, d_2, \dots, d_n$,其中 $d_i$ 表示半径为 $i$ 的圆盘。每个圆盘 $d_i$ 上都包含数字 $0, 1, \dots, 9$,这些数字按逆时针顺序排序并书写,从圆盘的最右端点开始。每个数字 $j$ 在圆盘 $d_i$ 上的出现次数由 $a_{j,i}$ 给出,且保证任何圆盘上的数字总数均为偶数。

这些圆盘同心堆叠,其中 $d_1$ 在最上方,$d_n$ 在最下方。玩家可以独立地将每个圆盘向顺时针或逆时针方向旋转任意次,每次旋转一个位置。

目标是将中间水平线上的数字对齐,如图所示。中间水平线恰好由 $2n$ 个数字组成:左侧和右侧各包含每个圆盘上的一个数字。

任务

给定 $q$ 个目标字符串 $x_1, x_2, \dots, x_q$,每个长度为 $2n$,确定将中间水平线对齐以完全匹配每个目标字符串所需的最小旋转次数。如果无法对齐到某个目标字符串,则输出 $-1$。每次询问是相互独立的,也就是说,对于每次询问,我们都从第一段中描述的相同初始配置开始。

输入格式

  1. 一个整数 $n$ ($1 \le n \le 10^5$):圆盘的数量。
  2. 一个 $10 \times n$ 的整数网格,其中条目 $a_{j,i} \le 10^9$ ($0 \le j \le 9, 1 \le i \le n$) 指定了数字 $j$ 在圆盘 $d_i$ 上出现的次数。保证每个圆盘都有偶数个数字。
  3. 一个整数 $q$ ($1 \le q \le 2 \times 10^6$):目标字符串的数量。
  4. $q$ 个长度为 $2n$ 的字符串 $x_1, x_2, \dots, x_q$:目标字符串。

保证 $nq \le 7 \times 10^6$。

输出格式

对于每个目标字符串 $x_k$ ($1 \le k \le q$),输出:

  • 第一行,如果可以将中间水平线对齐到 $x_k$,则输出 POSSIBLE
  • 第二行,输出将中间水平线对齐到 $x_k$ 所需的最小旋转次数,如果不可能,则输出 -1

如果输出的第一行正确而第二行不正确,将获得该测试点一半的分数。

数据范围

  • $1 \le n \le 10^5$
  • $0 \le a_{j,i} \le 10^9$
  • $1 \le q \le 2 \times 10^6$
  • $nq \le 7 \times 10^6$

子任务

子任务 分值 限制
1 6 $n = q = 1$
2 6 每个圆盘上只有一种数字
3 10 对于所有 $i$,$\sum_{j=0}^9 a_{j,i} \le 2$
4 8 $n = 1$
5 20 对于所有 $i, j, x, y$:$a_{i,j} = a_{x,y}$
6 30 $nq \le 10^5$
7 20 无附加限制

样例

输入样例 1

4
0 3 1 2 0 0 0 0 0 0
0 0 0 0 4 2 3 0 1 0
0 2 0 0 4 1 3 1 0 1
0 2 0 0 4 1 3 1 0 5
4
65521411
65521419
75521414
75521410

输出样例 1

POSSIBLE
0
POSSIBLE
1
POSSIBLE
2
IMPOSSIBLE
-1

说明 1

这是第 1 页插图中所展示的样例。

输入样例 2

1
4 3 2 3 1 1 4 3 9 2
1
71

输出样例 2

POSSIBLE
4

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.