Submission #858810

# Submission time Handle Problem Language Result Execution time Memory
858810 2023-10-09T08:22:49 Z LucaIlie Catfish Farm (IOI22_fish) C++17
35 / 100
1000 ms 1048700 KB
#include "fish.h"
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 3e5 + 2;
const long long INF = 1e18;
unordered_map<int, long long> costCell[MAX_N], dp[MAX_N], dpLower[MAX_N], spX[MAX_N], maxPref1[MAX_N], maxSuf2[MAX_N], maxPref3[MAX_N];
vector <int> ys[MAX_N];

long long max_weights( int n, int m, vector <int> X, vector <int> Y, vector <int> W ) {

    for ( int i = 0; i < m; i++ ) {
        X[i]++;
        Y[i]++;
        costCell[X[i]][Y[i]] = W[i];
        ys[X[i] - 1].push_back( Y[i] );
        ys[X[i] + 1].push_back( Y[i] );
        ys[X[i]].push_back( Y[i] );
    }

    ys[0].clear();
    ys[n + 1].clear();
    for ( int x = 0; x <= n + 1; x++ )
        ys[x].push_back( 0 );
    for ( int x = 1; x <= n; x++ ) {
        sort( ys[x].begin(), ys[x].end() );
        ys[x].resize( unique( ys[x].begin(), ys[x].end() ) - ys[x].begin() );
    }

    for ( int x = 1; x <= n; x++ ) {
        for ( int y = 1; y <= n; y++ )
            spX[x][y] = spX[x][y - 1] + costCell[x][y];
    }

    dp[0][0] = dpLower[0][0] = 0;
    for ( int x = 1; x <= n + 1; x++ ) {
        for ( int i = 0; i < ys[x].size(); i++ ) {
            int crtY = ys[x][i];
            dp[x][crtY] = dpLower[x][crtY] = -INF;

            if ( x >= 1 ) {
                auto lower = upper_bound( ys[x - 1].begin(), ys[x - 1].end(), crtY );
                auto upper = lower_bound( ys[x - 1].begin(), ys[x - 1].end(), crtY );
                if ( lower != ys[x - 1].begin() ) {
                    lower--;
                    int prevY = *lower;
                    dpLower[x][crtY] = max( dpLower[x][crtY], maxPref1[x - 1][prevY] + spX[x - 1][crtY] );
                    dp[x][crtY] = max( dp[x][crtY], maxPref1[x - 1][prevY] + spX[x - 1][crtY] );
                }
                if ( upper != ys[x - 1].end() ) {
                    int prevY = *upper;
                    dp[x][crtY] = max( dp[x][crtY], maxSuf2[x - 1][prevY] - spX[x][crtY] );
                }
            }

            if ( x >= 2 ) {
                auto lower = upper_bound( ys[x - 2].begin(), ys[x - 2].end(), crtY );
                auto upper = lower_bound( ys[x - 2].begin(), ys[x - 2].end(), crtY );
                if ( lower != ys[x - 2].begin() ) {
                    lower--;
                    int prevY = *lower;
                    dpLower[x][crtY] = max( dpLower[x][crtY], maxPref3[x - 2][prevY] + spX[x - 1][crtY] );
                    dp[x][crtY] = max( dp[x][crtY], maxPref3[x - 2][prevY] + spX[x - 1][crtY] );
                }
                if ( upper != ys[x - 2].end() ) {
                    int prevY = *upper;
                    dpLower[x][crtY] = max( dpLower[x][crtY], maxSuf2[x - 2][prevY] );
                    dp[x][crtY] = max( dp[x][crtY], maxSuf2[x - 2][prevY] );
                }
            }

            maxPref1[x][crtY] = dpLower[x][crtY] - spX[x][crtY];
            maxSuf2[x][crtY] = dp[x][crtY] + spX[x + 1][crtY];
            maxPref3[x][crtY] = dpLower[x][crtY];
        }

        for ( int i = 1; i < ys[x].size(); i++ ) {
            maxPref1[x][ys[x][i]] = max( maxPref1[x][ys[x][i]], maxPref1[x][ys[x][i - 1]] );
            maxPref3[x][ys[x][i]] = max( maxPref3[x][ys[x][i]], maxPref3[x][ys[x][i - 1]] );
        }
        for ( int i = ys[x].size() - 2; i >= 0; i-- )
            maxSuf2[x][ys[x][i]] = max( maxSuf2[x][ys[x][i]], maxSuf2[x][ys[x][i + 1]] );
    }

    return dp[n + 1][0];
}

Compilation message

fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:38:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |         for ( int i = 0; i < ys[x].size(); i++ ) {
      |                          ~~^~~~~~~~~~~~~~
fish.cpp:78:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |         for ( int i = 1; i < ys[x].size(); i++ ) {
      |                          ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Execution timed out 1097 ms 1044284 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 28 ms 122460 KB Output is correct
2 Execution timed out 1061 ms 1002968 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1065 ms 1048700 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 30 ms 122540 KB Output is correct
2 Correct 29 ms 122520 KB Output is correct
3 Correct 29 ms 122332 KB Output is correct
4 Correct 28 ms 122456 KB Output is correct
5 Correct 28 ms 122524 KB Output is correct
6 Correct 28 ms 122468 KB Output is correct
7 Correct 28 ms 122456 KB Output is correct
8 Correct 28 ms 122456 KB Output is correct
9 Correct 34 ms 124752 KB Output is correct
10 Correct 44 ms 131420 KB Output is correct
11 Correct 36 ms 124832 KB Output is correct
12 Correct 44 ms 131152 KB Output is correct
13 Correct 29 ms 122968 KB Output is correct
14 Correct 42 ms 131192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 122540 KB Output is correct
2 Correct 29 ms 122520 KB Output is correct
3 Correct 29 ms 122332 KB Output is correct
4 Correct 28 ms 122456 KB Output is correct
5 Correct 28 ms 122524 KB Output is correct
6 Correct 28 ms 122468 KB Output is correct
7 Correct 28 ms 122456 KB Output is correct
8 Correct 28 ms 122456 KB Output is correct
9 Correct 34 ms 124752 KB Output is correct
10 Correct 44 ms 131420 KB Output is correct
11 Correct 36 ms 124832 KB Output is correct
12 Correct 44 ms 131152 KB Output is correct
13 Correct 29 ms 122968 KB Output is correct
14 Correct 42 ms 131192 KB Output is correct
15 Correct 45 ms 131100 KB Output is correct
16 Correct 31 ms 123736 KB Output is correct
17 Correct 105 ms 146392 KB Output is correct
18 Correct 110 ms 144208 KB Output is correct
19 Correct 98 ms 144464 KB Output is correct
20 Correct 92 ms 142572 KB Output is correct
21 Correct 95 ms 142524 KB Output is correct
22 Correct 177 ms 154568 KB Output is correct
23 Correct 66 ms 136528 KB Output is correct
24 Correct 104 ms 145488 KB Output is correct
25 Correct 43 ms 131296 KB Output is correct
26 Correct 62 ms 135764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 122540 KB Output is correct
2 Correct 29 ms 122520 KB Output is correct
3 Correct 29 ms 122332 KB Output is correct
4 Correct 28 ms 122456 KB Output is correct
5 Correct 28 ms 122524 KB Output is correct
6 Correct 28 ms 122468 KB Output is correct
7 Correct 28 ms 122456 KB Output is correct
8 Correct 28 ms 122456 KB Output is correct
9 Correct 34 ms 124752 KB Output is correct
10 Correct 44 ms 131420 KB Output is correct
11 Correct 36 ms 124832 KB Output is correct
12 Correct 44 ms 131152 KB Output is correct
13 Correct 29 ms 122968 KB Output is correct
14 Correct 42 ms 131192 KB Output is correct
15 Correct 45 ms 131100 KB Output is correct
16 Correct 31 ms 123736 KB Output is correct
17 Correct 105 ms 146392 KB Output is correct
18 Correct 110 ms 144208 KB Output is correct
19 Correct 98 ms 144464 KB Output is correct
20 Correct 92 ms 142572 KB Output is correct
21 Correct 95 ms 142524 KB Output is correct
22 Correct 177 ms 154568 KB Output is correct
23 Correct 66 ms 136528 KB Output is correct
24 Correct 104 ms 145488 KB Output is correct
25 Correct 43 ms 131296 KB Output is correct
26 Correct 62 ms 135764 KB Output is correct
27 Execution timed out 1086 ms 929104 KB Time limit exceeded
28 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1065 ms 1048700 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1097 ms 1044284 KB Time limit exceeded
2 Halted 0 ms 0 KB -