답안 #1083853

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1083853 2024-09-04T10:11:47 Z SamueleVid 메기 농장 (IOI22_fish) C++17
18 / 100
1000 ms 2096860 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long

constexpr int MAXN = 1e5 + 5;

ll sum_tutto(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
    ll sum = 0;
    for (auto x : W) sum += x;
    return sum;
}

ll res_minore_di_due(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
    if (N == 2) {
        // massimo tra 0 e 1    
        ll one = 0;
        ll zero = 0;
        for (int i = 0; i < M; i ++) {
            if (X[i] == 0) zero += W[i];
            else one += W[i];
        }

        return max(one, zero);
    }

    // altrimenti piazza una riga in 2 e poi prendi prefisso maggiore piazzando in 1
    
    ll sum = 0;
    for (int i = 0; i < M; i ++) {
        if (X[i] == 1) sum += W[i];
    }

    vector<vector<ll>> pos(2, vector<ll>(N + 5, 0));

    for (int i = 0; i < M; i ++) {
        pos[X[i]][Y[i]] += W[i];
    }

    ll best_res = sum;

    ll sum_zero = 0;
    ll sum_uno = 0;
    for (int i = 0; i < N + 5; i ++) {
        sum_zero += pos[0][i];
        sum_uno += pos[1][i];
        best_res = max(best_res, sum - sum_uno + sum_zero);
    }

    return best_res;
}

ll res_y_zero(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
    vector<vector<ll>> dp(2, vector<ll>(N + 5)); // posso prendere a sx o no, posizione

    vector<ll> grid(N + 5);
    for (int i = 0; i < M; i ++) grid[X[i]] = W[i];

    for (int i = N - 1; i >= 0; i --) {
        dp[0][i] = dp[1][i + 1];
        dp[0][i] = max(dp[0][i], dp[0][i + 1]);
        dp[0][i] = max(dp[0][i], dp[0][i + 2] + grid[i + 1]);
        dp[0][i] = max(dp[0][i], dp[1][i + 3] + grid[i + 1]);
        dp[0][i] = max(dp[0][i], dp[0][i + 3] + grid[i + 1]);

        if (i != 0) {
            dp[1][i] = dp[1][i + 1];
            dp[1][i] = max(dp[1][i], dp[0][i + 1] + grid[i - 1]);
            dp[1][i] = max(dp[1][i], dp[0][i + 2] + grid[i - 1] + grid[i + 1]); 
            dp[1][i] = max(dp[1][i], dp[1][i + 3] + grid[i - 1] + grid[i + 1]); 
            dp[1][i] = max(dp[1][i], dp[0][i + 3] + grid[i - 1] + grid[i + 1]); 
        }
    }

    ll best_res = 0;
    for (int i = 0; i <= N; i ++) {
        best_res = max(best_res, dp[0][i]);
        best_res = max(best_res, dp[1][i]);
    }
    return best_res;
}

ll cubic(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
    // i, quanto lo alzo, quanto era alzato a destra
    // int MAXY = min(N, 9);
    int MAXY = N;
    ll dp[N + 5][MAXY + 5][MAXY + 5]; 

    for (int i = 0; i < N + 5; i ++) {
        for (int j = 0; j < MAXY + 5; j ++) {
            for (int k = 0; k < MAXY + 5; k ++) {
                dp[i][j][k] = 0;
            }
        }
    }

    ll grid[N + 5][MAXY + 5];
    for (int i = 0; i < N + 5; i ++) {
        for (int j = 0; j < MAXY + 5; j ++) {
                grid[i][j] = 0;
        }
    }

    for (int i = 0; i < M; i ++) grid[X[i]][Y[i]] = W[i];

    ll ps_grid[N + 5][MAXY + 5]; // 1 based per le Y

    for (int i = 0; i < N + 5; i ++) {
        ps_grid[i][0] = 0;
        for (int j = 0; j < MAXY + 4; j ++) {
            ps_grid[i][j + 1] = ps_grid[i][j] + grid[i][j];
        }
    }

    // cout << "N, MAXY : " << N << " " << MAXY << '\n';

    // cout << "grid : " << '\n';
    // for (int i = 0; i < N + 5; i ++) {
    //     for (int j = 0; j < MAXY + 5; j ++) {
    //         cout << grid[i][j] << " ";
    //     }
    //     cout << '\n';
    // }
    // cout << "ps_grid " << '\n';
    // for (int i = 0; i < N + 5; i ++) {
    //     for (int j = 0; j < MAXY + 5; j ++) {
    //         cout << ps_grid[i][j] << " ";
    //     }
    //     cout << '\n';
    // }

    for (int i = N - 1; i >= 0; i --) {
        for (int y = 0; y <= MAXY; y ++) {
            for (int dx = 0; dx <= MAXY; dx ++) {
                // quanto mi alzo
                for (int dxdx = 0; dxdx <= MAXY; dxdx ++) {
                    // cout << "-- i, y, dx, dxdx : " << i << " " << y << " " << dx << " " << dxdx << '\n';
                    // cout << "dp dopo : " << dp[i + 1][dx][dxdx] << '\n';
                    // cout << "a dx preso top : " << ps_grid[i + 1][y] << " e si sottrae " << ps_grid[i + 1][max(dx, dxdx)] << '\n';
                    ll preso = -ps_grid[i][min(dx, y)];
                    if (y > max(dx, dxdx)) {
                        preso += ps_grid[i + 1][y] - ps_grid[i + 1][max(dx, dxdx)];
                    }
                    if (i > 0) {
                        preso += ps_grid[i - 1][y];
                        // cout << " e poi si aggiunge a sx " << ps_grid[i - 1][y] << '\n';
                    }

                    // cout << "preso : " << preso << '\n';
                    // cout << "new value : " << dp[i + 1][dx][dxdx] + preso << '\n';

                    dp[i][y][dx] = max(dp[i][y][dx], dp[i + 1][dx][dxdx] + preso);
                }
            }
        }
    }

    ll res = 0;
    for (int i = 0; i < N; i ++) {
        for (int y = 0; y < MAXY; y ++) {
            for (int dx = 0; dx < MAXY; dx ++) {
                res = max(res, dp[i][y][dx]);
            }
        }
    }

    return res;
}


ll max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
    bool all_even = 1;
    for (auto x : X) if (x % 2) all_even = 0;
    if (all_even) return sum_tutto(N, M, X, Y, W);

    bool minore_di_due = 1;
    for (auto x : X) if (x >= 2) minore_di_due = 0;
    if (minore_di_due) return res_minore_di_due(N, M, X, Y, W);

    bool y_zero = 1;
    for (auto y : Y) if (y > 0) y_zero = 0;
    if (y_zero) return res_y_zero(N, M, X, Y, W);

    return cubic(N, M, X, Y, W);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 4444 KB Output is correct
2 Correct 17 ms 5720 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 62 ms 16144 KB Output is correct
6 Correct 61 ms 16720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 30 ms 9956 KB Output is correct
3 Correct 40 ms 11832 KB Output is correct
4 Correct 16 ms 4180 KB Output is correct
5 Correct 19 ms 4948 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 16 ms 6236 KB Output is correct
13 Correct 21 ms 7456 KB Output is correct
14 Correct 30 ms 6420 KB Output is correct
15 Correct 20 ms 6996 KB Output is correct
16 Correct 18 ms 6236 KB Output is correct
17 Correct 18 ms 7004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 4 ms 2652 KB Output is correct
3 Correct 14 ms 5208 KB Output is correct
4 Correct 9 ms 4440 KB Output is correct
5 Correct 22 ms 7336 KB Output is correct
6 Correct 18 ms 7004 KB Output is correct
7 Correct 19 ms 7260 KB Output is correct
8 Correct 19 ms 7256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 634 ms 29784 KB Output is correct
10 Execution timed out 1061 ms 223960 KB Time limit exceeded
11 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 634 ms 29784 KB Output is correct
10 Execution timed out 1061 ms 223960 KB Time limit exceeded
11 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 634 ms 29784 KB Output is correct
10 Execution timed out 1061 ms 223960 KB Time limit exceeded
11 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 4 ms 2652 KB Output is correct
3 Correct 14 ms 5208 KB Output is correct
4 Correct 9 ms 4440 KB Output is correct
5 Correct 22 ms 7336 KB Output is correct
6 Correct 18 ms 7004 KB Output is correct
7 Correct 19 ms 7260 KB Output is correct
8 Correct 19 ms 7256 KB Output is correct
9 Execution timed out 1141 ms 2096860 KB Time limit exceeded
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 4444 KB Output is correct
2 Correct 17 ms 5720 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 62 ms 16144 KB Output is correct
6 Correct 61 ms 16720 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 30 ms 9956 KB Output is correct
9 Correct 40 ms 11832 KB Output is correct
10 Correct 16 ms 4180 KB Output is correct
11 Correct 19 ms 4948 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 16 ms 6236 KB Output is correct
19 Correct 21 ms 7456 KB Output is correct
20 Correct 30 ms 6420 KB Output is correct
21 Correct 20 ms 6996 KB Output is correct
22 Correct 18 ms 6236 KB Output is correct
23 Correct 18 ms 7004 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 4 ms 2652 KB Output is correct
26 Correct 14 ms 5208 KB Output is correct
27 Correct 9 ms 4440 KB Output is correct
28 Correct 22 ms 7336 KB Output is correct
29 Correct 18 ms 7004 KB Output is correct
30 Correct 19 ms 7260 KB Output is correct
31 Correct 19 ms 7256 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 344 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 634 ms 29784 KB Output is correct
41 Execution timed out 1061 ms 223960 KB Time limit exceeded
42 Halted 0 ms 0 KB -