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 Maxn = 1e6 + 10;
  int a[Maxn];
  pair<int, int> ma[Maxn][5];
  int pre[Maxn][5];
  int main() {
    int cas, n;
    cin >> cas;
    while (cas--) {
      cin >> n;
      for (int i = 1; i <= n; i++) cin >> a[i];
      int cnt = 0;
      ma[0][cnt++] = pair<int, int>(-Maxn, -Maxn);
      for (int i = 1; i <= n; i++) {
        if (cnt == 0) break;
        int b[4] = {Maxn, Maxn, Maxn, Maxn};
        int pre_index[4] = {-1, -1, -1, -1};
        for (int j = 0; j < cnt; j++) {
          if (-a[i] > ma[i - 1][j].first && b[0] > ma[i - 1][j].second)
            b[0] = ma[i - 1][j].second, pre_index[0] = j;
          else if (-a[i] > ma[i - 1][j].second && b[1] > ma[i - 1][j].first)
            b[1] = ma[i - 1][j].first, pre_index[1] = j;
          if (a[i] > ma[i - 1][j].first && b[2] > ma[i - 1][j].second)
            b[2] = ma[i - 1][j].second, pre_index[2] = j;
          else if (a[i] > ma[i - 1][j].second && b[3] > ma[i - 1][j].first)
            b[3] = ma[i - 1][j].first, pre_index[3] = j;
        }
        cnt = 0;
        if (b[0] != Maxn)
          pre[i][cnt] = pre_index[0], ma[i][cnt++] = pair<int, int>(-a[i], b[0]);
        if (b[1] != Maxn)
          pre[i][cnt] = pre_index[1], ma[i][cnt++] = pair<int, int>(b[1], -a[i]);
        if (b[2] != Maxn)
          pre[i][cnt] = pre_index[2], ma[i][cnt++] = pair<int, int>(a[i], b[2]);
        if (b[3] != Maxn)
          pre[i][cnt] = pre_index[3], ma[i][cnt++] = pair<int, int>(b[3], a[i]);
      }
      if (!cnt) {
        cout << "NO\n";
        continue;
      }
      cout << "YES\n";
      for (int i = n, index = 0; i > 0; i--) {
        pair<int, int> res = ma[i][index];
        index = pre[i][index];
        if (res.first == -a[i] || res.second == -a[i]) a[i] *= -1;
      }
      for (int i = 1; i <= n; i++) cout << a[i] << " ";
      cout << endl;
    }
  }
  