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;
  typedef struct _node {
    int pa;
    bool plus;
    int x;
    int y;
  } node;
  void solve() {
    int n;
    cin >> n;
    vector<node> nodes;
    vector<int> valid_nodes;
    vector<int> arr;
    for (int i = 0; i < n; i++) {
      int x;
      cin >> x;
      arr.push_back(x);
      if (i == 0) {
        node start{-1, false, -x, -0x7f7f7f7f};
        nodes.push_back(start);
        valid_nodes.push_back(0);
        continue;
      }
      vector<int> next_valid;
      for (int idx : valid_nodes)
        for (int s = 0; s <= 1; s++) {
          int val = s ? x : -x;
          node prev = nodes[idx];
          if (val < prev.y) continue;
          node nex{idx, (bool)s, prev.x, prev.y};
          if (val < prev.x)
            nex.y = val;
          else
            nex.x = val;
          nodes.push_back(nex);
          next_valid.push_back(nodes.size() - 1);
        }
      valid_nodes.clear();
      vector<bool> optimal(next_valid.size(), true);
      for (int me = 0; me < next_valid.size(); me++)
        for (int you = 0; you < next_valid.size(); you++) {
          if (me == you || !optimal[me]) continue;
          node& mynode = nodes[next_valid[me]];
          node& yournode = nodes[next_valid[you]];
          if (mynode.x == yournode.x && mynode.y == yournode.y) {
            optimal[you] = false;
          } else if (mynode.x >= yournode.x && mynode.y >= yournode.y) {
            optimal[me] = false;
            break;
          }
        }
      for (int me = 0; me < next_valid.size(); me++) {
        if (optimal[me]) valid_nodes.push_back(next_valid[me]);
      }
      if (valid_nodes.size() == 0) {
        int dbg = 0;
      }
    }
    if (valid_nodes.empty()) {
      cout << "NO\n";
      return;
    }
    cout << "YES\n";
    int cur = valid_nodes[0];
    vector<int> ans(n);
    for (int i = n - 1; i >= 0; i--) {
      int x = nodes[cur].plus ? arr[i] : -arr[i];
      ans[i] = x;
      cur = nodes[cur].pa;
    }
    for (int k : ans) cout << k << ' ';
    cout << '\n';
  }
  int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    int t;
    cin >> t;
    while (t--) solve();
  }
  