CF1375A.Sign Flipping

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given nn integers a1,a2,…,ana_1, a_2, \dots, a_n, where nn is odd. You are allowed to flip the sign of some (possibly all or none) of them. You wish to perform these flips in such a way that the following conditions hold:

  1. At least n−12\frac{n - 1}{2} of the adjacent differences ai+1−aia_{i + 1} - a_i for i=1,2,…,n−1i = 1, 2, \dots, n - 1 are greater than or equal to 00.
  2. At least n−12\frac{n - 1}{2} of the adjacent differences ai+1−aia_{i + 1} - a_i for i=1,2,…,n−1i = 1, 2, \dots, n - 1 are less than or equal to 00.

Find any valid way to flip the signs. It can be shown that under the given constraints, there always exists at least one choice of signs to flip that satisfies the required condition. If there are several solutions, you can find any of them.

给你 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n,其中 nn 为奇数。你可以翻转其中某些(可能全部、也可能一个都不翻转)数的符号。你需要以如下方式执行这些翻转操作:

  1. 在所有相邻差值 ai+1−aia_{i + 1} - a_i(其中 i=1,2,…,n−1i = 1, 2, \dots, n - 1)中,至少有 n−12\frac{n - 1}{2} 个大于等于 00;
  2. 在所有相邻差值 ai+1−aia_{i + 1} - a_i(其中 i=1,2,…,n−1i = 1, 2, \dots, n - 1)中,至少有 n−12\frac{n - 1}{2} 个小于等于 00。

请找出任意一种满足条件的符号翻转方案。可以证明:在题目给定的约束条件下,总存在至少一种符号翻转方案满足上述要求。若存在多种解,输出任意一种即可。

输入格式

The input consists of multiple test cases. The first line contains an integer tt (1≤t≤5001 \le t \le 500) — the number of test cases. The description of the test cases follows.

The first line of each test case contains an integer nn (3≤n≤993 \le n \le 99, nn is odd) — the number of integers given to you.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (−109≤ai≤109-10^9 \le a_i \le 10^9) — the numbers themselves.

It is guaranteed that the sum of nn over all test cases does not exceed 1000010000.

输入包含多个测试用例。第一行包含一个整数 tt(1≤t≤5001 \le t \le 500),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(3≤n≤993 \le n \le 99,且 nn 为奇数),表示给定的整数个数。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(−109≤ai≤109-10^9 \le a_i \le 10^9),即这些数字本身。

保证所有测试用例中 nn 的总和不超过 1000010000。

输出格式

For each test case, print nn integers b1,b2,…,bnb_1, b_2, \dots, b_n, corresponding to the integers after flipping signs. bib_i has to be equal to either aia_i or −ai-a_i, and of the adjacent differences bi+1−bib_{i + 1} - b_i for i=1,…,n−1i = 1, \dots, n - 1, at least n−12\frac{n - 1}{2} should be non-negative and at least n−12\frac{n - 1}{2} should be non-positive.

It can be shown that under the given constraints, there always exists at least one choice of signs to flip that satisfies the required condition. If there are several solutions, you can find any of them.

对于每个测试用例,输出 nn 个整数 b1,b2,…,bnb_1, b_2, \dots, b_n,表示对符号进行翻转后的整数序列。其中每个 bib_i 必须等于 aia_i 或 −ai-a_i;并且在相邻差值 bi+1−bib_{i + 1} - b_i(i=1,…,n−1i = 1, \dots, n - 1)中,至少有 n−12\frac{n - 1}{2} 个非负,且至少有 n−12\frac{n - 1}{2} 个非正。

可以证明,在给定约束条件下,总存在至少一种符号翻转方案满足上述要求。若存在多种解,输出任意一种即可。

输入输出样例

  • 输入#1

    5
    3
    -2 4 3
    5
    1 1 1 1 1
    5
    -2 4 7 -6 4
    9
    9 7 -4 -2 1 -3 9 -4 -5
    9
    -4 1 9 4 8 9 5 1 -9

    输出#1

    -2 -4 3
    1 1 1 1 1
    -2 -4 7 -6 4
    -9 -7 -4 2 1 -3 -9 -4 -5
    4 -1 -9 -4 -8 -9 -5 -1 9

说明/提示

In the first test case, the difference (−4)−(−2)=−2(-4) - (-2) = -2 is non-positive, while the difference 3−(−4)=73 - (-4) = 7 is non-negative.

In the second test case, we don't have to flip any signs. All 44 differences are equal to 00, which is both non-positive and non-negative.

In the third test case, 7−(−4)7 - (-4) and 4−(−6)4 - (-6) are non-negative, while (−4)−(−2)(-4) - (-2) and (−6)−7(-6) - 7 are non-positive.

在第一个测试用例中,差值 (−4)−(−2)=−2(-4) - (-2) = -2 为非正数,而差值 3−(−4)=73 - (-4) = 7 为非负数。

在第二个测试用例中,我们无需翻转任何符号。全部 44 个差值均为 00,而 00 既是非正数也是非负数。

在第三个测试用例中,7−(−4)7 - (-4) 和 4−(−6)4 - (-6) 为非负数,而 (−4)−(−2)(-4) - (-2) 和 (−6)−7(-6) - 7 为非正数。

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

首页