/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Wrong Answer 1ms 540.0 KiB
#2 Wrong Answer 34ms 644.0 KiB

Code

#include <bits/stdc++.h>
using namespace std;
using int64 = long long;
const int INF = 0x3f3f3f3f;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;
    while(T--){
        int N;
        cin >> N;
        vector<int64> A(N+1);
        for(int i=1;i<=N;i++) cin >> A[i];

        // dp[i][c1][c2]: min cost up to (i-1), with color(i-2)=c1, color(i-1)=c2
        // colors: 0 = none (before start), 1,2,3
        static int64 dp[3][4][4];
        // We roll i dimension, so only two layers needed
        // Initialize for i=1: no previous colors
        for(int c1=0;c1<4;c1++)
            for(int c2=0;c2<4;c2++)
                dp[0][c1][c2] = (int64)1e30;
        dp[0][0][0] = 0;

        int cur = 0, nxt = 1;
        for(int i=1;i<=N;i++){
            // reset next layer
            for(int c1=0;c1<4;c1++)
                for(int c2=0;c2<4;c2++)
                    dp[nxt][c1][c2] = (int64)1e30;

            for(int p1=0;p1<4;p1++){
                for(int p2=0;p2<4;p2++){
                    int64 base = dp[cur][p1][p2];
                    if(base > (int64)1e29) continue;
                    // try color c for position i
                    for(int c=1;c<=3;c++){
                        // must differ from p1,p2 to avoid fights within distance<=2
                        if(c==p1 || c==p2) continue;
                        int64 add = (c-1) * A[i];  
                        // color 1 -> add 0*A[i], color 2 -> add 1*A[i], color 3 -> add 2*A[i]
                        dp[nxt][p2][c] = min(dp[nxt][p2][c], base + add);
                    }
                }
            }
            swap(cur,nxt);
        }

        // answer = min over final (p1,p2) of dp[cur][p1][p2]
        int64 ans = (int64)1e30;
        for(int p1=0;p1<4;p1++){
            for(int p2=0;p2<4;p2++){
                ans = min(ans, dp[cur][p1][p2]);
            }
        }
        cout << ans << "\n";
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1087 G. Face the monsters
Language
C++17 (G++ 13.2.0)
Submit At
2025-05-14 09:47:54
Judged At
2025-05-14 09:47:54
Judged By
Score
0
Total Time
34ms
Peak Memory
644.0 KiB