QOJ.ac

QOJ

Límite de tiempo: 1 s Límite de memoria: 128 MB Puntuación total: 100

#16014. 序列

Estadísticas

给定一个数字序列。你的任务是构造一个单调递增的序列,使其能够最好地逼近给定的序列。最佳逼近序列是指与给定序列的总偏差最小的序列。

更具体地,设 $t_1, t_2, \dots, t_N$ 是给定的数字序列。你的任务是构造一个单调递增的数字序列 $z_1 < z_2 < \dots < z_N$。

使得以下总和达到最小:

$$|t_1 - z_1| + |t_2 - z_2| + \dots + |t_N - z_N|$$

输入格式

输入的第一行包含一个整数 $N$ ($1 \le N \le 1\,000\,000$)。

接下来的 $N$ 行,每行包含一个整数,表示给定序列的元素。其中第 $K+1$ 行包含 $t_K$。

所有元素均满足 $0 \le t_K \le 2\,000\,000\,000$。

输出格式

输出的第一行包含一个整数,表示最小的可能总偏差。

接下来的 $N$ 行,每行包含一个整数,表示最佳逼近序列中对应的元素。

如果存在多个解,你的程序可以输出任意一个能够达到最小总偏差的序列。

样例

输入样例 1

7
9
4
8
20
14
15
18

输出样例 1

13
6
7
8
13
14
15
18

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.