답안 #1066062

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1066062 2024-08-19T14:33:44 Z j_vdd16 메기 농장 (IOI22_fish) C++17
26 / 100
1000 ms 131784 KB
#include "fish.h"

#include <algorithm>
#include <bitset>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define ll long long
#define loop(X, N) for(int X = 0; X < (N); X++)
#define all(V) V.begin(), V.end()
#define rall(V) V.rbegin(), V.rend()

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vector<ii>> vvii;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;

vector<vector<pair<int, ll>>> fishes;
vector<map<int, ll>> prefixWeight;

inline ll weightSum(int x, int y1, int y2) {
    if (x >= fishes.size() || y2 <= y1)
        return 0;

    // int res = 0;
    // for (auto [y, w] : fishes[x]) {
    //     if (y1 <= y && y < y2)
    //         res += w;
    // }

    // return res;

    auto it1 = prefixWeight[x].upper_bound(y2 - 1);
    auto it2 = prefixWeight[x].upper_bound(y1 - 1);
    ll res = (--it1)->second - (--it2)->second;
    //cout << x << ' ' << y1 << ' ' << y2 << " = " << res << endl;
    return res;
}

long long max_weights(signed N, signed M, vector<signed> X, std::vector<signed> Y, std::vector<signed> W) {
    fishes = vector<vector<pair<int, ll>>>(N);
    loop(i, M) {
        fishes[X[i]].push_back({Y[i], W[i]});
    }
    fishes.push_back({});

    prefixWeight = vector<map<int, ll>>(N + 1);
    loop(x, N + 1) {
        sort(all(fishes[x]));

        ll total = 0;
        prefixWeight[x][-2] = 0;
        for (auto [y, w] : fishes[x]) {
            total += w;

            prefixWeight[x][y] = total;
        }
    }

    vector<map<int, ll>> beforeDp(N);
    vector<map<int, ll>> bothDp(N);

    vector<ll> maxBefore(N);
    vector<ll> maxBoth(N);

    //bestBefore, bestBeforeAndAfter
    loop(x, N) {
        if (x == 0) {
            for (const auto& [y, w] : fishes[x + 1]) {
                bothDp[0][y + 1] = weightSum(x + 1, 0, y + 1);
                maxBoth[x] = max(maxBoth[x], bothDp[0][y + 1]);
            }
            beforeDp[0][0] = 0;
            maxBefore[0] = 0;

            continue;
        }

        ll bestBefore2 = 0;
        ll bestBoth3 = 0;
        if (x >= 2) {
            bestBefore2 = maxBefore[x - 2];
        }
        if (x >= 3) {
            bestBoth3 = maxBoth[x - 3];
        }

        int counter = 0;
        for (const auto& [y, w] : fishes.at(x - 1)) {
            int height = y + 1;
            ll& both = bothDp[x][height];
            ll& before = beforeDp[x][height];

            for (const auto& [prevHeight, prevScore] : beforeDp.at(x - 1)) {
                counter++;
                ll prevCur = weightSum(x - 1, prevHeight, height);

                both = max(both, prevScore + prevCur);
                before = max(before, prevScore + prevCur);
            }
            for (const auto& [prevY, prevScore] : bothDp.at(x - 1)) {
                //counter++;
                if (prevY <= height) 
                    continue;

                int prevCur = -weightSum(x, 0, height);
                both = max(both, prevScore + prevCur);
            }

            ll prevWeight = weightSum(x - 1, 0, height);
            both = max(both, bestBefore2 + prevWeight);
            both = max(both, bestBoth3 + prevWeight);

            before = max(before, bestBefore2 + prevWeight);
            before = max(before, bestBoth3 + prevWeight);
            
            maxBefore[x] = max(maxBefore[x], before);
        }
        for (const auto& [y, w] : fishes.at(x + 1)) {
            int height = y + 1;
            ll& both = bothDp[x][height];

            for (const auto& [prevHeight, prevScore] : beforeDp.at(x - 1)) {
                counter++;
                ll prevCur = weightSum(x - 1, prevHeight, height);

                both = max(both, prevScore + prevCur);
            }
            for (const auto& [prevY, prevScore] : bothDp.at(x - 1)) {
                //counter++;
                if (prevY <= height) 
                    continue;

                ll prevCur = -weightSum(x, 0, height);
                both = max(both, prevScore + prevCur);
            }

            ll prevWeight = weightSum(x - 1, 0, height);
            both = max(both, bestBefore2 + prevWeight);
            both = max(both, bestBoth3 + prevWeight);
        }

        for (const auto& [height, score] : bothDp.at(x)) {
            counter++;
            ll nextWeight = weightSum(x + 1, 0, height);
            bothDp[x][height] += nextWeight;
            
            maxBoth[x] = max(maxBoth[x], bothDp[x][height]);
        }

        //cout << "Counter: " << counter << endl;

        ll& both = bothDp[x][0];
        ll& before = beforeDp[x][0];

        both = max({both, maxBefore[x - 1], bestBefore2, bestBoth3});
        before = max(before, both);

        maxBefore[x] = max(maxBefore[x], before);
        maxBoth[x] = max(maxBoth[x], both);
    }

    ll res = 0;
    loop(x, N) {
        for (auto [y, score] : beforeDp[x]) {
            res = max(res, score);
        }
        for (auto [y, score] : bothDp[x]) {
            res = max(res, score);
        }
    }

    return res;
}

Compilation message

fish.cpp: In function 'long long int weightSum(int, int, int)':
fish.cpp:37:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<std::pair<int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |     if (x >= fishes.size() || y2 <= y1)
      |         ~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 145 ms 53888 KB Output is correct
2 Correct 155 ms 62792 KB Output is correct
3 Correct 26 ms 37680 KB Output is correct
4 Correct 27 ms 38452 KB Output is correct
5 Correct 566 ms 128164 KB Output is correct
6 Correct 299 ms 131784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Execution timed out 1073 ms 45992 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 37172 KB Output is correct
2 Correct 26 ms 37836 KB Output is correct
3 Correct 66 ms 47892 KB Output is correct
4 Correct 47 ms 48680 KB Output is correct
5 Correct 80 ms 64040 KB Output is correct
6 Correct 77 ms 62096 KB Output is correct
7 Correct 99 ms 62504 KB Output is correct
8 Correct 83 ms 62480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 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 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Incorrect 1 ms 604 KB 1st lines differ - on the 1st token, expected: '216624184325', found: '242644412274'
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 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 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Incorrect 1 ms 604 KB 1st lines differ - on the 1st token, expected: '216624184325', found: '242644412274'
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 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 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Incorrect 1 ms 604 KB 1st lines differ - on the 1st token, expected: '216624184325', found: '242644412274'
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 37172 KB Output is correct
2 Correct 26 ms 37836 KB Output is correct
3 Correct 66 ms 47892 KB Output is correct
4 Correct 47 ms 48680 KB Output is correct
5 Correct 80 ms 64040 KB Output is correct
6 Correct 77 ms 62096 KB Output is correct
7 Correct 99 ms 62504 KB Output is correct
8 Correct 83 ms 62480 KB Output is correct
9 Correct 103 ms 70816 KB Output is correct
10 Correct 77 ms 43580 KB Output is correct
11 Correct 146 ms 88000 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 30 ms 37064 KB Output is correct
19 Correct 27 ms 38452 KB Output is correct
20 Correct 26 ms 38964 KB Output is correct
21 Correct 27 ms 37160 KB Output is correct
22 Correct 110 ms 69712 KB Output is correct
23 Correct 180 ms 96432 KB Output is correct
24 Correct 197 ms 98856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 145 ms 53888 KB Output is correct
2 Correct 155 ms 62792 KB Output is correct
3 Correct 26 ms 37680 KB Output is correct
4 Correct 27 ms 38452 KB Output is correct
5 Correct 566 ms 128164 KB Output is correct
6 Correct 299 ms 131784 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Execution timed out 1073 ms 45992 KB Time limit exceeded
9 Halted 0 ms 0 KB -