[官方英文题解]
[比赛传送门]
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−1)/2 of the adjacent differences ai+1−ai for i=1,2,…,n−1 are greater than or equal to 0.
At least (n−1)/2 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.
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.
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−1)/2 should be non-negative and at least (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.
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
-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 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.
一个原序列a有n个整数(n为奇数)。
你可以对序列a做出一些改变,将任意位置的数取相反数,亦可不进行操作。
对操作后的序列a,从第二个数到倒数第n个数,每个数都与前一个数相减,即ai+1-ai,得出序列b。且序列b大于等于0的数与小于等于0的数都要大于等于(n-1)/2
。题目要求输出改变后的序列a。
ai+1-ai>=0与ai+1-ai<=0即为ai+1>=ai与ai+1<=ai,所以若要保证序列b中两种都要占(n-1)/2,则将所有元素按照非负数,非正数相间设置即可,即,将序列a中位置为1,3,5,7,9…的数设为非正数,将2,4,6,8,10,设为非负数即可。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ll long long
#define db double
#define inf INT_MAX
#define s(a, n) memset(a, n, sizeof(a))
#define rep(l, a, b) for (ll l = a; l < b; ++l)
#define per(l, a, b) for (ll l = a; l >= b; --l)
#define debug(a) cout << '#' << a << '#' << endl
using namespace std;
bool fi = true;
const unsigned long long MOD = 1e9 + 7;
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t, n;
cin >> t;
int a[100];
while (t--) {
cin >> n;
int j, o;
j = o = 0;
rep(i, 0, n) {
cin >> a[i];
if ((i & 1)) {
if (a[i] > 0) a[i] = -a[i];
}
else {
if (a[i] < 0) a[i] = -a[i];
}
}
rep(i, 0, n) cout << a[i] << (i == n - 1 ? '\n' : ' ');
}
}
赛后补题
You are given an array a of length n, which initially is a permutation of numbers from 1 to n. In one operation, you can choose an index i (1≤i
For example, if you have the array [1,3,2], you can choose i=1 (since a1=12=3), then either remove a1 which gives the new array [3,2], or remove a2 which gives the new array [1,2].
Is it possible to make the length of this array equal to 1 with these operations?
The first line contains a single integer t (1≤t≤2⋅104) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer n (2≤n≤3⋅105) — the length of the array.
The second line of each test case contains n integers a1, a2, …, an (1≤ai≤n, ai are pairwise distinct) — elements of the array.
It is guaranteed that the sum of n over all test cases doesn’t exceed 3⋅105.
For each test case, output on a single line the word “YES” if it is possible to reduce the array to a single element using the aforementioned operation, or “NO” if it is impossible to do so.
4
3
1 2 3
4
3 1 2 4
3
2 3 1
6
2 4 6 1 3 5
YES
YES
NO
YES
For the first two test cases and the fourth test case, we can operate as follow (the bolded elements are the pair chosen for that operation):
[1,2,3]→[1,2]→[1]
[3,1,2,4]→[3,1,4]→[3,4]→[4]
[2,4,6,1,3,5]→[4,6,1,3,5]→[4,1,3,5]→[4,1,5]→[4,5]→[4]
给你一个长度为n的整数数列,数列由1到n组成。
当相邻两个数满足 aii+1 ,则可删去 ai 与 ai+1 中的任意一个,数列中剩下的元素成为一个新数列。
题目要求,当数列可以通过多次操作转化为元素数量为一的数列时,输出“YES”,否则输出“”NO”。
数列中有n个数,当一个较大元素出现在数列的前列,我们需要一个较小的数出现在这个数的前端,通过操作让这两个数接触,再销毁,或者需要一个更大的数出现在这个元素的后端,将通过有限次操作将其销毁。
这么说可能有点云里雾里的(qs),直接给出结论和解释:
对输入的数列,直接判断第一个数是否小于最后一个数,当条件成立时,在数列中出现小数时,可以通过销毁操作,将小数与最后一个元素接触,然后销毁小数;同理,当数列中出现大数时,可以通过销毁操作,使大数与第一个元素接触,然后销毁大数,最后当数列只剩下最初的第一个元素和最后一个元素时,同样可以满足销毁操作的条件,销毁任意一个数,达到题目的要求。
所以,当第一个数小于最后一个数,输出“YES”,否则输出“NO”。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ll long long
#define db double
#define inf INT_MAX
#define s(a, n) memset(a, n, sizeof(a))
#define rep(l, a, b) for (ll l = a; l < b; ++l)
#define per(l, a, b) for (ll l = a; l >= b; --l)
#define debug(a) cout << '#' << a << '#' << endl
using namespace std;
bool fi = true;
const unsigned long long MOD = 1e9 + 7;
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t, n;
cin >> t;
int a[300005];
while (t--) {
cin >> n;
int head, end;
rep(i, 0, n) {
cin >> a[i];
if (i == 0) {
head = a[i];
}
if (i == n - 1) {
end = a[i];
}
}
if (head > end) {
cout << "NO" << endl;
} else {
cout << "YES" << endl;
}
}
}