Sign Flipping CodeForces - 1375A rat1100

这道题目要求在给定奇数长度的整数序列中,通过翻转一些元素的符号,使得至少一半的相邻差值为非负,且至少一半为非正。保证在约束条件下总能找到至少一种解决方案。输出一种满足条件的翻转后的序列。
摘要由CSDN通过智能技术生成

You are given n integers a1,a2,…,an, where n 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:

At least n−12 of the adjacent differences ai+1−ai for i=1,2,…,n−1 are greater than or equal to 0.
At least n−12 of the adjacent differences ai+1−ai for i=1,2,…,n−1 are less than or equal to 0.
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.

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

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

The second line of each test case contains n integers a1,a2,…,an (−109≤ai≤109) — the numbers themselves.

It is guaranteed that the sum of n over all test cases does not exceed 10000.

Output
For each test case, print n integers b1,b2,…,bn, corresponding to the integers after flipping signs. bi has to be equal to either ai or −ai, and of the adjacent differences bi+1−bi for i=1,…,n−1, at least n−12 should be non-negative and at least n−12 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.

Example
Input
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
Output
-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
Note
In the first test case, the difference (−4)−(−2)=−2 is

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值