Codeforces 1375 A. Sign Flipping

You are given 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛, where 𝑛 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 𝑛−12 of the adjacent differences 𝑎𝑖+1−𝑎𝑖 for 𝑖=1,2,…,𝑛−1 are greater than or equal to 0.
At least 𝑛−12 of the adjacent differences 𝑎𝑖+1−𝑎𝑖 for 𝑖=1,2,…,𝑛−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 𝑡 (1≤𝑡≤500) — the number of test cases. The description of the test cases follows.

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

The second line of each test case contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (−109≤𝑎𝑖≤109) — the numbers themselves.

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

Output
For each test case, print 𝑛 integers 𝑏1,𝑏2,…,𝑏𝑛, corresponding to the integers after flipping signs. 𝑏𝑖 has to be equal to either 𝑎𝑖 or −𝑎𝑖, and of the adjacent differences 𝑏𝑖+1−𝑏𝑖 for 𝑖=1,…,𝑛−1, at least 𝑛−12 should be non-negative and at least 𝑛−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
inputCopy
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
outputCopy
-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 non-positive, while the difference 3−(−4)=7 is non-negative.

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

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

亂搞

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <map>
 
using namespace std;
 
typedef long long ll;
 
const int maxn = 1e5 + 7;
int a[maxn];
 
int main() {
    int T;scanf("%d",&T);
    while(T--) {
        int n;scanf("%d",&n);
        for(int i = 1;i <= n;i++) {
            scanf("%d",&a[i]);
            a[i] = abs(a[i]);
        }
        for(int i = 1;i <= n;i++) {
            if(i & 1) {
                printf("%d ",-a[i]);
            } else {
                printf("%d ",a[i]);
            }
        }
        printf("\n");
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章