CF2171B.Yuu Koito and Minimum Absolute Sum

入门

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

The words in shoujo manga and love songs... they're always sparkling brightly. I don't need a dictionary to understand the meaning... but I've never felt them for myself.

— Yuu Koito

Yuu is trying out the student council! Unfortunately, she is being forced to do clerical work... Touko wants her to fill out the blanks in various student council documents.

You are given a partially filled array of nonnegative integers a1,a2,…,ana_1, a_2, \dots, a_n, where blank elements are denoted with −1-1. You would like to fill in the blank elements with nonnegative integers, such that the absolute value of the sum of the elements in its difference array is minimized.

More formally, let bb be the array of length n−1n-1 such that bi=ai+1−aib_i = a_{i+1} - a_i for all 1≤i≤n−11\leq i\leq n-1. Find the minimum possible value of ∣b1+b2+⋯+bn−1∣|b_1 + b_2 + \dots + b_{n-1}|, across all possible ways to fill in the blank elements of aa.

Additionally, output the array that achieves this minimum. If there are multiple such arrays, output the one that is lexicographically smallest∗^{\text{∗}}.

∗^{\text{∗}}For two arbitrary arrays cc and dd of length nn, we say that cc is lexicographically smaller than dd if there exists an index ii (1≤i≤n1\leq i\leq n) such that cj=djc_j = d_j for all j<ij \lt i, and ci<dic_i \lt d_i. In other words, cc and dd differ in at least one index, and at the first index at which they differ, cic_i is smaller than did_i.

少女漫画和情歌中的词语……总是闪耀着耀眼的光芒。我不需要字典就能理解它们的含义……但我却从未真正切身感受过。

——小野夕

夕正在尝试加入学生会!但不幸的是,她被迫做一些文书工作……藤子希望她填写学生会各类文件中的空白处。

给你一个部分填充的非负整数数组 a1,a2,…,ana_1, a_2, \dots, a_n,其中空白元素用 −1-1 表示。你需要将所有空白元素替换为非负整数,使得其差分数组中所有元素之和的绝对值最小。

更准确地说,令 bb 为长度为 n−1n-1 的数组,满足对所有 1≤i≤n−11\leq i\leq n-1,有 bi=ai+1−aib_i = a_{i+1} - a_i。在所有可能的 aa 数组(即所有将 −1-1 替换为非负整数的方式)中,求 ∣b1+b2+⋯+bn−1∣|b_1 + b_2 + \dots + b_{n-1}| 的最小可能值。

此外,请输出达到该最小值的数组 aa。若存在多个这样的数组,请输出字典序最小的那个∗^{\text{∗}}。

∗^{\text{∗}}对于两个任意长度为 nn 的数组 cc 和 dd,我们称 cc 的字典序小于 dd,当且仅当存在某个下标 ii(1≤i≤n1\leq i\leq n),使得对所有 j<ij < i 都有 cj=djc_j = d_j,且 ci<dic_i < d_i。换言之,cc 与 dd 至少在一个位置上不同,且在第一个不同的位置 ii 上,cic_i 小于 did_i。

输入格式

The first line contains a single integer tt (1≤t≤1041 \leq t \leq 10^4) — the number of test cases.

The first line of each test case contains a single integer nn (2≤n≤2⋅1052\leq n\leq 2\cdot 10^5).

The second line of each test case contains nn integers, a1,a2,…,ana_1, a_2, \dots, a_n (−1≤ai≤106-1\leq a_i \leq 10^6).

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052\cdot 10^5.

第一行包含一个整数 tt(1≤t≤1041 \leq t \leq 10^4)—— 表示测试用例的数量。

每个测试用例的第一行包含一个整数 nn(2≤n≤2⋅1052\leq n\leq 2\cdot 10^5)。

每个测试用例的第二行包含 nn 个整数:a1,a2,…,ana_1, a_2, \dots, a_n(−1≤ai≤106-1\leq a_i \leq 10^6)。

保证所有测试用例的 nn 之和不超过 2⋅1052\cdot 10^5。

输出格式

For each test case, on the first line, output the minimum possible value of ∣b1+b2+⋯+bn−1∣|b_1 + b_2 + \dots + b_{n-1}|. Then, on the second line, output nn integers, the values of a1,a2,…,ana_1, a_2, \dots, a_n in the lexicographically smallest array achieving this minimum.

对于每个测试用例,第一行输出 ∣b1+b2+⋯+bn−1∣|b_1 + b_2 + \dots + b_{n-1}| 的最小可能值。然后,在第二行输出 nn 个整数,即在达到该最小值的所有数组中字典序最小的数组 a1,a2,…,ana_1, a_2, \dots, a_n 的取值。

输入输出样例

  • 输入#1

    6
    4
    2 -1 7 1
    4
    -1 2 4 -1
    8
    2 -1 1 5 11 12 1 -1
    3
    -1 -1 -1
    3
    2 5 4
    2
    -1 5

    输出#1

    1
    2 0 7 1
    0
    0 2 4 0
    0
    2 0 1 5 11 12 1 2
    0
    0 0 0
    2
    2 5 4
    0
    5 5

说明/提示

In the first example, we fill in the array a=[2,0,7,1]a = [2, 0, 7, 1], which yields the difference array b=[−2,7,−6]b = [-2, 7, -6].

The absolute value of the sum of the elements in bb is 11. It can be proven that this is the minimum possible. Furthermore, it can be proven that this is the lexicographically smallest array aa that achieves this minimum.

在第一个例子中,我们填充数组 a=[2,0,7,1]a = [2, 0, 7, 1],得到差分数组 b=[−2,7,−6]b = [-2, 7, -6]。

$ b $ 中所有元素之和的绝对值为 11。可以证明这是可能的最小值。此外,还可以证明,这是达到该最小值的所有数组 aa 中字典序最小的一个。

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

首页