fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. inline int power(int a, int b) {
  6. int x = 1;
  7. while (b) {
  8. if (b & 1) x *= a;
  9. a *= a;
  10. b >>= 1;
  11. }
  12. return x;
  13. }
  14.  
  15.  
  16. const int M = 1000000007;
  17. const int N = 3e5+9;
  18. const int INF = 2e9+1;
  19. const int LINF = 2000000000000000001;
  20.  
  21. //_ ***************************** START Below *******************************
  22.  
  23.  
  24.  
  25. vector<int> a;
  26.  
  27. void consistency(int n) {
  28.  
  29. int i=0;
  30. while(i<n){
  31. if(a[i] > 0) break;
  32. i++;
  33. }
  34. int s = i-1;
  35. int e = i;
  36. vector<int> ans;
  37. while(s>=0 && e<n){
  38. int left = abs(a[s]);
  39. int right = a[e];
  40. if(left < right){
  41. ans.push_back(left*left);
  42. s--;
  43. }
  44. else{
  45. ans.push_back(right*right);
  46. e++;
  47. }
  48. }
  49. while(s>=0){
  50. int left = abs(a[s]);
  51. ans.push_back(left*left);
  52. s--;
  53. }
  54. while(e<n){
  55. int right = a[e];
  56. ans.push_back(right*right);
  57. e++;
  58. }
  59.  
  60. for(int i=0; i<n; i++){
  61. cout << ans[i] << " ";
  62. }cout << endl;
  63.  
  64. }
  65.  
  66.  
  67. void solve() {
  68.  
  69. int n;
  70. cin >> n ;
  71. a.resize(n);
  72. for(int i=0; i<n; i++) cin >> a[i];
  73.  
  74. consistency(n);
  75.  
  76. }
  77.  
  78.  
  79.  
  80.  
  81.  
  82. int32_t main() {
  83. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  84.  
  85. int t = 1;
  86. cin >> t;
  87. while (t--) {
  88. solve();
  89. }
  90.  
  91. return 0;
  92. }
Success #stdin #stdout 0.01s 5300KB
stdin
3
5 
1 2 3 4 5
5
-5 -4 -3 -2 -1
6
-3 -2 0 1 2 3
stdout
1 4 9 16 25 
1 4 9 16 25 
0 1 4 4 9 9