QOJ.ac

QOJ

実行時間制限: 2.0 s メモリ制限: 1024 MB 満点: 100

#10874. 直方图上的山峰

統計

我们希望通过在直方图形状的画布上重复绘制山峰,来绘制出一幅直方图形状的图画。

直方图定义为一种形状,由一个或多个共享公共底边并紧密排列(无间隙)的矩形组成,底边平行于 $x$ 轴。每个矩形的宽度和高度均为整数,且可以互不相同。

单次山峰绘制过程如下: 在画布的顶边上选择一个点作为山峰的峰顶。 绘制适合该峰顶的尽可能大的山峰形状。

山峰不能超出画布范围,且之前的绘制不会影响当前的绘制。此外,矩形的左端点或右端点不能作为山峰的峰顶。

山峰的具体定义如下: 包含峰顶的左侧部分高度单调递增。 包含峰顶的右侧部分高度单调递减。

在此,如果函数 $f$ 对于所有 $x_1 < x_2$ 满足 $f(x_1) \le f(x_2)$,则称其为单调递增;如果对于所有 $x_1 < x_2$ 满足 $f(x_1) \ge f(x_2)$,则称其为单调递减。

换句话说,选择峰顶后,我们对画布上所有可以通过从峰顶向下、向左、向右移动到达的点进行着色。

如果可以通过重复绘制山峰来绘制出给定的图画,请以最少的绘制次数完成;如果无法完成,输出 $-1$。

输入格式

第一行包含构成画布直方图的矩形数量 $n$ ($1 \le n \le 2 \cdot 10^5$)。 接下来的 $n$ 行,每行包含两个整数 $w_i$ 和 $h_i$:画布中每个矩形的宽度和高度 ($1 \le w_i, h_i \le 10^9$)。 下一行包含构成图画直方图的矩形数量 $m$ ($1 \le m \le 2 \cdot 10^5$)。 接下来的 $m$ 行,每行包含两个整数 $w'_j$ 和 $h'_j$:图画中每个矩形的宽度和高度 ($1 \le w'_j, h'_j \le 10^9$)。

满足以下保证: 画布和图画的总宽度相同且不超过 $10^9$:$\sum w_i = \sum w'_j \le 10^9$。 画布包含图画。 * 相邻矩形高度不同:$h_i \neq h_{i+1}$ 且 $h'_j \neq h'_{j+1}$。

输出格式

第一行输出完成绘制所需的最少山峰绘制次数。如果无法完成图画,输出 $-1$ 并结束程序。 第二行输出每次绘制时,山峰峰顶所在的画布矩形索引(从 1 开始),以空格分隔。 如果存在多种有效答案,输出其中任意一种即可。

样例

样例输入 1

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

样例输出 1

2
3 5

样例输入 2

2
4 5
4 7
2
4 5
4 6

样例输出 2

-1

Figure 1. 样例 1 的直方图表示

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.