Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define x first
- #define y second
- #define mp make_pair
- #define pb push_back
- #define sqr(a) ((a) * (a))
- #define sz(a) int(a.size())
- #define all(a) a.begin(), a.end()
- #define forn(i, n) for(int i = 0; i < int(n); i++)
- #define fore(i, l, r) for(int i = int(l); i < int(r); i++)
- typedef long long li;
- typedef long double ld;
- typedef pair<int, int> pt;
- template <class A, class B> ostream& operator << (ostream& out, const pair<A, B> &a) {
- return out << "(" << a.x << ", " << a.y << ")";
- }
- template <class A> ostream& operator << (ostream& out, const vector<A> &v) {
- out << "[";
- forn(i, sz(v)) {
- if(i) out << ", ";
- out << v[i];
- }
- return out << "]";
- }
- mt19937 rnd(time(NULL));
- const int INF = int(1e9);
- const li INF64 = li(1e18);
- const int MOD = INF + 7;
- const ld EPS = 1e-9;
- const ld PI = acos(-1.0);
- const int N = 100 * 1000 + 13;
- int n;
- int a[N];
- bool read() {
- if (scanf("%d", &n) != 1)
- return false;
- forn(i, n)
- scanf("%d", &a[i]);
- return true;
- }
- li dp[N][4];
- int p[N][4];
- void solve() {
- forn(i, n) forn(j, 4) dp[i][j] = INF64;
- dp[0][2] = a[0];
- if (a[0] > a[1]){
- dp[1][1] = a[0] - a[1];
- p[1][1] = 2;
- }
- if (a[0] < a[1]){
- dp[1][2] = -a[0] + a[1];
- p[1][2] = 0;
- }
- dp[1][3] = a[0] + a[1];
- p[1][3] = 2;
- fore(i, 2, n){
- forn(mask, 4){
- forn(j, 2){
- int nmask = (mask >> 1) + (j << 1);
- li p2 = (mask & 1 ? a[i - 2] : -a[i - 2]);
- li p1 = (mask & 2 ? a[i - 1] : -a[i - 1]);
- li p0 = (j ? a[i] : -a[i]);
- if (p0 + p1 > 0 && p0 + p1 + p2 > 0 && dp[i - 1][mask] + p0 < dp[i][nmask]){
- dp[i][nmask] = dp[i - 1][mask] + p0;
- p[i][nmask] = mask;
- }
- }
- }
- }
- int st = 0;
- forn(i, 4) if (dp[n - 1][i] < dp[n - 1][st]) st = i;
- vector<int> ans;
- for (int i = n - 1; i >= 0; --i){
- ans.pb(st & 2 ? a[i] : -a[i]);
- st = p[i][st];
- }
- reverse(all(ans));
- forn(i, n)
- printf("%d ", ans[i]);
- puts("");
- }
- int main() {
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- // freopen("output.txt", "w", stdout);
- int tt = clock();
- #endif
- cerr.precision(15);
- cout.precision(15);
- cerr << fixed;
- cout << fixed;
- int tc;
- scanf("%d", &tc);
- while (tc--){
- read();
- solve();
- #ifdef _DEBUG
- cerr << "TIME = " << clock() - tt << endl;
- tt = clock();
- #endif
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement