description: You are given a permutation p consisting of n integers 1, 2, ..., n (a permutation is an array where each element from 1 to n occurs exactly once).
  
  Let's call an array a bipartite if the following undirected graph is bipartite:
  
    * the graph consists of n vertices; 
    * two vertices i and j are connected by an edge if i < j and a_i > a_j. 
  
  
  
  Your task is to find a bipartite array of integers a of size n, such that a_i = p_i or a_i = -p_i, or report that no such array exists. If there are multiple answers, print any of them.
  
  Input
  
  The first line contains a single integer t (1 ≤ t ≤ 2 ⋅ 10^5) — the number of test cases.
  
  The first line of each test case contains a single integer n (1 ≤ n ≤ 10^6) — the size of the permutation.
  
  The second line contains n integers p_1, p_2, ..., p_n.
  
  The sum of n over all test cases doesn't exceed 10^6.
  
  Output
  
  For each test case, print the answer in the following format. If such an array a does not exist, print "NO" in a single line. Otherwise, print "YES" in the first line and n integers — array a in the second line.
  
  Example
  
  Input
  
  
  4
  3
  1 2 3
  6
  1 3 2 6 5 4
  4
  4 1 3 2
  8
  3 2 1 6 7 8 5 4
  
  
  Output
  
  
  YES
  1 2 3
  NO
  YES
  -4 -1 -3 -2
  YES
  -3 -2 1 6 7 -8 -5 -4
solution: #include <bits/stdc++.h>
  using namespace std;
  const int N = 1e6 + 10;
  int t, n;
  int a[N];
  int dp[N][2][2];
  pair<int, pair<int, int> > be[N][2][2];
  int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> t;
    while (t--) {
      cin >> n;
      for (int i = 1; i <= n; ++i)
        cin >> a[i], dp[i][0][0] = dp[i][1][0] = dp[i][1][1] = dp[i][0][1] = 1e9,
                     be[i][0][0] = be[i][1][0] = be[i][1][1] = be[i][0][1] =
                         make_pair(0, make_pair(0, 0));
      be[1][0][1] = be[1][1][1] = make_pair(-1, make_pair(0, 0));
      dp[1][0][1] = -1e9;
      dp[1][1][1] = -1e9;
      for (int i = 2; i <= n; ++i)
        for (int j = 0; j <= 1; ++j)
          for (int op1 = 0; op1 <= 1; ++op1)
            for (int op2 = 0; op2 <= 1; ++op2) {
              int now = j == 0 ? -a[i] : a[i];
              int from = op1 == 0 ? -a[i - 1] : a[i - 1];
              if (be[i - 1][op1][op2].first == 0) continue;
              if (op2 == 0) {
                if (now < from) continue;
                if (now > dp[i - 1][op1][op2]) {
                  if (from < dp[i][j][1])
                    dp[i][j][1] = from,
                    be[i][j][1] = make_pair(i - 1, make_pair(op1, op2));
                } else {
                  if (dp[i - 1][op1][op2] < dp[i][j][0])
                    dp[i][j][0] = dp[i - 1][op1][op2],
                    be[i][j][0] = make_pair(i - 1, make_pair(op1, op2));
                }
              } else {
                if (now < dp[i - 1][op1][op2]) continue;
                if (now > from) {
                  if (dp[i - 1][op1][op2] < dp[i][j][1])
                    dp[i][j][1] = dp[i - 1][op1][op2],
                    be[i][j][1] = make_pair(i - 1, make_pair(op1, op2));
                } else {
                  if (from < dp[i][j][0])
                    dp[i][j][0] = from,
                    be[i][j][0] = make_pair(i - 1, make_pair(op1, op2));
                }
              }
            }
      int op1, op2, flag = 0;
      for (op1 = 0; op1 <= 1 && flag == 0; ++op1) {
        for (op2 = 0; op2 <= 1 && flag == 0; ++op2)
          if (be[n][op1][op2].first != 0) {
            flag = 1;
            break;
          }
        if (flag) break;
      }
      if (op1 > 1)
        cout << "NO\n";
      else {
        cout << "YES\n";
        for (int i = n; i; --i) {
          pair<int, pair<int, int> > from = be[i][op1][op2];
          if (op1 == 0) a[i] = -a[i];
          op1 = from.second.first;
          op2 = from.second.second;
        }
        for (int i = 1; i <= n; ++i) cout << a[i] << " ";
        cout << "\n";
      }
    }
  }
  