Submission #959857

# Submission time Handle Problem Language Result Execution time Memory
959857 2024-04-09T08:39:16 Z LucaIlie Constellation 3 (JOI20_constellation3) C++17
35 / 100
1047 ms 102872 KB
#include <bits/stdc++.h>

using namespace std;

struct star {
    int i, j, c;
};

const int MAX_N = 2e5;
const int MAX_M = 2e5;
int n;
int h[MAX_N + 2], parent[MAX_N + 2], leftSon[MAX_N + 2], rightSon[MAX_N + 2], leftt[MAX_N + 2], rightt[MAX_N + 2], cost[MAX_N + 2];
long long dp[2002][2002];
star stars[MAX_M];
vector<int> starsByColumn[MAX_N + 2];

void dfs( int v, int l, int r ) {
    if ( v == 0 )
        return;

    leftt[v] = l;
    rightt[v] = r;
    //printf( "%d %d %d\n", v, l, r );

    dfs( leftSon[v], l, v - 1 );
    dfs( rightSon[v], v + 1, r );
}

void calcCost( int v ) {
    if ( v == 0 )
        return;

    calcCost( leftSon[v] );
    calcCost( rightSon[v] );

    for ( int x = 0; x <= n; x++ ) {
        if ( x <= h[v] )
            dp[v][x] = dp[leftSon[v]][x] + dp[rightSon[v]][x];
        else
            dp[v][x] = max( dp[leftSon[v]][x] + dp[rightSon[v]][h[v]], dp[leftSon[v]][h[v]] + dp[rightSon[v]][x] );
    }

    for ( int i: starsByColumn[v] )
        dp[v][stars[i].j] = max( dp[v][stars[i].j], dp[v][h[v]] + stars[i].c );

    for ( int x = 1; x <= n; x++ )
        dp[v][x] = max( dp[v][x], dp[v][x - 1] );
}

int main() {
    int m;
    long long total = 0;

    cin >> n;
    for ( int i = 1; i <= n; i++ )
        cin >> h[i];
    cin >> m;
    for ( int i = 0; i < m; i++ ) {
        cin >> stars[i].i >> stars[i].j >> stars[i].c;
        total += stars[i].c;
    }

    h[n + 1] = n + 1;
    vector<int> stack;
    for ( int i = 1; i <= n + 1; i++ ) {
        vector<int> path;
        while ( !stack.empty() && h[i] >= h[stack.back()] ) {
            path.push_back( stack.back() );
            stack.pop_back();
        }
        path.push_back( i );
        stack.push_back( i );

        for ( int j = 0; j < path.size() - 1; j++ )
            parent[path[j]] = path[j + 1];
    }

    //for ( int v = 1; v <= n; v++ )
        //printf( "%d %d, %d %d\n", v, parent[v], h[v], h[parent[v]] );
    for ( int v = 1; v <= n; v++ ) {
        if ( v < parent[v] )
            leftSon[parent[v]] = v;
        else
            rightSon[parent[v]] = v;
    }

    for ( int i = 0; i < m; i++ )
        starsByColumn[stars[i].i].push_back( i );

    calcCost( n + 1 );

    cout << total - dp[n + 1][n];

    return 0;
}

Compilation message

constellation3.cpp: In function 'int main()':
constellation3.cpp:74:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |         for ( int j = 0; j < path.size() - 1; j++ )
      |                          ~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15448 KB Output is correct
2 Correct 3 ms 15452 KB Output is correct
3 Correct 3 ms 15448 KB Output is correct
4 Correct 3 ms 15452 KB Output is correct
5 Correct 6 ms 15488 KB Output is correct
6 Correct 3 ms 15452 KB Output is correct
7 Correct 3 ms 15380 KB Output is correct
8 Correct 4 ms 15452 KB Output is correct
9 Correct 4 ms 15452 KB Output is correct
10 Correct 3 ms 15452 KB Output is correct
11 Correct 3 ms 15452 KB Output is correct
12 Correct 4 ms 15452 KB Output is correct
13 Correct 3 ms 15528 KB Output is correct
14 Correct 3 ms 15452 KB Output is correct
15 Correct 3 ms 15452 KB Output is correct
16 Correct 5 ms 13404 KB Output is correct
17 Correct 3 ms 15276 KB Output is correct
18 Correct 3 ms 15452 KB Output is correct
19 Correct 3 ms 15452 KB Output is correct
20 Correct 3 ms 15480 KB Output is correct
21 Correct 3 ms 15500 KB Output is correct
22 Correct 3 ms 15452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15448 KB Output is correct
2 Correct 3 ms 15452 KB Output is correct
3 Correct 3 ms 15448 KB Output is correct
4 Correct 3 ms 15452 KB Output is correct
5 Correct 6 ms 15488 KB Output is correct
6 Correct 3 ms 15452 KB Output is correct
7 Correct 3 ms 15380 KB Output is correct
8 Correct 4 ms 15452 KB Output is correct
9 Correct 4 ms 15452 KB Output is correct
10 Correct 3 ms 15452 KB Output is correct
11 Correct 3 ms 15452 KB Output is correct
12 Correct 4 ms 15452 KB Output is correct
13 Correct 3 ms 15528 KB Output is correct
14 Correct 3 ms 15452 KB Output is correct
15 Correct 3 ms 15452 KB Output is correct
16 Correct 5 ms 13404 KB Output is correct
17 Correct 3 ms 15276 KB Output is correct
18 Correct 3 ms 15452 KB Output is correct
19 Correct 3 ms 15452 KB Output is correct
20 Correct 3 ms 15480 KB Output is correct
21 Correct 3 ms 15500 KB Output is correct
22 Correct 3 ms 15452 KB Output is correct
23 Correct 26 ms 40808 KB Output is correct
24 Correct 15 ms 40796 KB Output is correct
25 Correct 15 ms 40796 KB Output is correct
26 Correct 16 ms 43056 KB Output is correct
27 Correct 17 ms 42844 KB Output is correct
28 Correct 15 ms 40796 KB Output is correct
29 Correct 15 ms 42844 KB Output is correct
30 Correct 16 ms 42844 KB Output is correct
31 Correct 15 ms 42844 KB Output is correct
32 Correct 16 ms 43080 KB Output is correct
33 Correct 17 ms 43100 KB Output is correct
34 Correct 15 ms 42892 KB Output is correct
35 Correct 16 ms 43100 KB Output is correct
36 Correct 16 ms 43100 KB Output is correct
37 Correct 16 ms 43100 KB Output is correct
38 Correct 15 ms 41052 KB Output is correct
39 Correct 15 ms 42844 KB Output is correct
40 Correct 15 ms 40960 KB Output is correct
41 Correct 15 ms 42844 KB Output is correct
42 Correct 15 ms 42836 KB Output is correct
43 Correct 15 ms 41048 KB Output is correct
44 Correct 15 ms 42844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15448 KB Output is correct
2 Correct 3 ms 15452 KB Output is correct
3 Correct 3 ms 15448 KB Output is correct
4 Correct 3 ms 15452 KB Output is correct
5 Correct 6 ms 15488 KB Output is correct
6 Correct 3 ms 15452 KB Output is correct
7 Correct 3 ms 15380 KB Output is correct
8 Correct 4 ms 15452 KB Output is correct
9 Correct 4 ms 15452 KB Output is correct
10 Correct 3 ms 15452 KB Output is correct
11 Correct 3 ms 15452 KB Output is correct
12 Correct 4 ms 15452 KB Output is correct
13 Correct 3 ms 15528 KB Output is correct
14 Correct 3 ms 15452 KB Output is correct
15 Correct 3 ms 15452 KB Output is correct
16 Correct 5 ms 13404 KB Output is correct
17 Correct 3 ms 15276 KB Output is correct
18 Correct 3 ms 15452 KB Output is correct
19 Correct 3 ms 15452 KB Output is correct
20 Correct 3 ms 15480 KB Output is correct
21 Correct 3 ms 15500 KB Output is correct
22 Correct 3 ms 15452 KB Output is correct
23 Correct 26 ms 40808 KB Output is correct
24 Correct 15 ms 40796 KB Output is correct
25 Correct 15 ms 40796 KB Output is correct
26 Correct 16 ms 43056 KB Output is correct
27 Correct 17 ms 42844 KB Output is correct
28 Correct 15 ms 40796 KB Output is correct
29 Correct 15 ms 42844 KB Output is correct
30 Correct 16 ms 42844 KB Output is correct
31 Correct 15 ms 42844 KB Output is correct
32 Correct 16 ms 43080 KB Output is correct
33 Correct 17 ms 43100 KB Output is correct
34 Correct 15 ms 42892 KB Output is correct
35 Correct 16 ms 43100 KB Output is correct
36 Correct 16 ms 43100 KB Output is correct
37 Correct 16 ms 43100 KB Output is correct
38 Correct 15 ms 41052 KB Output is correct
39 Correct 15 ms 42844 KB Output is correct
40 Correct 15 ms 40960 KB Output is correct
41 Correct 15 ms 42844 KB Output is correct
42 Correct 15 ms 42836 KB Output is correct
43 Correct 15 ms 41048 KB Output is correct
44 Correct 15 ms 42844 KB Output is correct
45 Runtime error 1047 ms 102872 KB Execution killed with signal 11
46 Halted 0 ms 0 KB -