답안 #629278

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
629278 2022-08-14T10:28:41 Z qwerasdfzxcl 메기 농장 (IOI22_fish) C++17
18 / 100
828 ms 132180 KB
#include "fish.h"
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll INF = 4e18;
vector<int> H[100100], A[100100];
vector<ll> dp[2][100100], S[100100], prf[2][100100], suf[2][100100];
int n, sz[100100];

ll cost(int i, int s, int e){
    if (s>e || i==0) return 0;
    int j1 = upper_bound(H[i].begin(), H[i].end(), s-1) - H[i].begin() - 1;
    int j2 = upper_bound(H[i].begin(), H[i].end(), e) - H[i].begin() - 1;
    return S[i][j2] - S[i][j1];
}

long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y,
                      std::vector<int> W) {
    n = N;
    for (int i=0;i<=N;i++) {H[i].push_back(0); H[i].push_back(N);}
    for (int i=0;i<M;i++){
        ++X[i], ++Y[i];
        H[X[i]].push_back(Y[i]);
        H[X[i]].push_back(Y[i]-1);
        if (X[i]>1) H[X[i]-1].push_back(Y[i]);
        if (X[i]<N) H[X[i]+1].push_back(Y[i]);
    }


    for (int i=0;i<=N;i++){
        sort(H[i].begin(), H[i].end());
        H[i].erase(unique(H[i].begin(), H[i].end()), H[i].end());
        sz[i] = H[i].size();

        A[i].resize(sz[i], 0);
        S[i].resize(sz[i], 0);
        dp[0][i].resize(sz[i], 0);
        dp[1][i].resize(sz[i], 0);
        prf[0][i].resize(sz[i]);
        prf[1][i].resize(sz[i]);
        suf[0][i].resize(sz[i]);
        suf[1][i].resize(sz[i]);
    }

    for (int i=0;i<M;i++){
        int idx = lower_bound(H[X[i]].begin(), H[X[i]].end(), Y[i]) - H[X[i]].begin();
        assert(idx < sz[X[i]]);
        A[X[i]][idx] = W[i];
    }


    for (int i=1;i<=n;i++){
        for (int j=1;j<sz[i];j++) S[i][j] = S[i][j-1] + A[i][j];
    }


    dp[0][0][0] = 0;
    dp[1][0][0] = 0;
    assert(sz[0]==2);
    dp[0][0][1] = -INF;
    dp[1][0][1] = -INF;

    for (int i=1;i<=n;i++){
        --i;
        for (int j=0;j<sz[i];j++){
            prf[0][i][j] = dp[0][i][j] + cost(i, H[i][j]+1, n);
            prf[1][i][j] = max(dp[0][i][j], dp[1][i][j]);
            suf[0][i][j] = max(dp[0][i][j], dp[1][i][j]) + cost(i+1, 1, H[i][j]);
            suf[1][i][j] = dp[1][i][j] + cost(i+1, 1, H[i][j]);

            if (j==0) continue;
            prf[0][i][j] = max(prf[0][i][j], prf[0][i][j-1]);
            prf[1][i][j] = max(prf[1][i][j], prf[1][i][j-1]);
            suf[0][i][j] = max(suf[0][i][j], suf[0][i][j-1]);
            suf[1][i][j] = max(suf[1][i][j], suf[1][i][j-1]);
        }
        ++i;

        for (int j=0;j<sz[i];j++){
            ///0 -> 0
            int idx00 = upper_bound(H[i-1].begin(), H[i-1].end(), H[i][j]) - H[i-1].begin() - 1;
            dp[0][i][j] = max(dp[0][i][j], prf[0][i-1][idx00] - cost(i-1, H[i][j]+1, n));
            /*for (int k=0;k<sz[i-1] && H[i-1][k]<=H[i][j];k++){
                dp[0][i][j] = max(dp[0][i][j], dp[0][i-1][k] + cost(i-1, H[i-1][k]+1, H[i][j]));
            }*/

            ///1 -> 0
            if (i>=2){
                int idx10 = upper_bound(H[i-2].begin(), H[i-2].end(), H[i][j]) - H[i-2].begin() - 1;
                dp[0][i][j] = max(dp[0][i][j], prf[1][i-2][idx10] + cost(i-1, 1, H[i][j]));
                if (idx10+1 < sz[i-2]) dp[0][i][j] = max(dp[0][i][j], suf[0][i-2][idx10+1]);

                /*for (int k=0;k<sz[i-2];k++){
                    dp[0][i][j] = max(dp[0][i][j], max(dp[0][i-2][k], dp[1][i-2][k]) + cost(i-1, 1, max(H[i-2][k], H[i][j])));
                }*/
            }

            ///0 -> 1
            dp[1][i][j] = max(dp[1][i][j], dp[0][i-1][sz[i-1]-1] + cost(i, H[i][j]+1, n));

            ///1 -> 1
            int idx11 = lower_bound(H[i-1].begin(), H[i-1].end(), H[i][j]) - H[i-1].begin();
            dp[1][i][j] = max(dp[1][i][j], suf[1][i-1][idx11] - cost(i, 1, H[i][j]));
            //for (int k=0;k<sz[i-1];k++) if (H[i-1][k] >= H[i][j]) dp[1][i][j] = max(dp[1][i][j], dp[1][i-1][k] + cost(i, H[i][j]+1, H[i-1][k]));
        }
    }

    //printf(" %lld %lld %lld\n", H[1][0], H[1][1], H[1][2]);
    //printf(" %lld -> %lld(%d)\n", cost(1, 1, 1), dp[0][2][1], H[2][1]);

    return max(*max_element(dp[0][n].begin(), dp[0][n].end()), *max_element(dp[1][n].begin(), dp[1][n].end()));
}
# 결과 실행 시간 메모리 Grader output
1 Correct 271 ms 60084 KB Output is correct
2 Correct 248 ms 65976 KB Output is correct
3 Correct 89 ms 50004 KB Output is correct
4 Correct 82 ms 49996 KB Output is correct
5 Correct 797 ms 117756 KB Output is correct
6 Correct 828 ms 132180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 21460 KB Output is correct
2 Correct 337 ms 68544 KB Output is correct
3 Correct 377 ms 75688 KB Output is correct
4 Correct 216 ms 60520 KB Output is correct
5 Correct 238 ms 65900 KB Output is correct
6 Correct 11 ms 21460 KB Output is correct
7 Correct 14 ms 21464 KB Output is correct
8 Correct 12 ms 21460 KB Output is correct
9 Correct 12 ms 21460 KB Output is correct
10 Correct 81 ms 49996 KB Output is correct
11 Correct 79 ms 49988 KB Output is correct
12 Correct 274 ms 65700 KB Output is correct
13 Correct 349 ms 72084 KB Output is correct
14 Correct 247 ms 63228 KB Output is correct
15 Correct 228 ms 62440 KB Output is correct
16 Correct 244 ms 63060 KB Output is correct
17 Correct 268 ms 67704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 84 ms 50012 KB Output is correct
2 Correct 81 ms 50008 KB Output is correct
3 Correct 118 ms 49456 KB Output is correct
4 Correct 117 ms 51420 KB Output is correct
5 Correct 175 ms 54448 KB Output is correct
6 Correct 200 ms 54348 KB Output is correct
7 Correct 169 ms 54416 KB Output is correct
8 Correct 184 ms 54332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 21444 KB Output is correct
2 Correct 11 ms 21460 KB Output is correct
3 Correct 12 ms 21460 KB Output is correct
4 Correct 13 ms 21460 KB Output is correct
5 Correct 12 ms 21436 KB Output is correct
6 Correct 12 ms 21396 KB Output is correct
7 Correct 12 ms 21460 KB Output is correct
8 Incorrect 11 ms 21436 KB 1st lines differ - on the 1st token, expected: '2', found: '1'
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 21444 KB Output is correct
2 Correct 11 ms 21460 KB Output is correct
3 Correct 12 ms 21460 KB Output is correct
4 Correct 13 ms 21460 KB Output is correct
5 Correct 12 ms 21436 KB Output is correct
6 Correct 12 ms 21396 KB Output is correct
7 Correct 12 ms 21460 KB Output is correct
8 Incorrect 11 ms 21436 KB 1st lines differ - on the 1st token, expected: '2', found: '1'
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 21444 KB Output is correct
2 Correct 11 ms 21460 KB Output is correct
3 Correct 12 ms 21460 KB Output is correct
4 Correct 13 ms 21460 KB Output is correct
5 Correct 12 ms 21436 KB Output is correct
6 Correct 12 ms 21396 KB Output is correct
7 Correct 12 ms 21460 KB Output is correct
8 Incorrect 11 ms 21436 KB 1st lines differ - on the 1st token, expected: '2', found: '1'
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 84 ms 50012 KB Output is correct
2 Correct 81 ms 50008 KB Output is correct
3 Correct 118 ms 49456 KB Output is correct
4 Correct 117 ms 51420 KB Output is correct
5 Correct 175 ms 54448 KB Output is correct
6 Correct 200 ms 54348 KB Output is correct
7 Correct 169 ms 54416 KB Output is correct
8 Correct 184 ms 54332 KB Output is correct
9 Correct 219 ms 65480 KB Output is correct
10 Correct 154 ms 46056 KB Output is correct
11 Correct 268 ms 70848 KB Output is correct
12 Correct 11 ms 21460 KB Output is correct
13 Correct 12 ms 21460 KB Output is correct
14 Correct 12 ms 21468 KB Output is correct
15 Correct 11 ms 21460 KB Output is correct
16 Correct 12 ms 21460 KB Output is correct
17 Incorrect 12 ms 21460 KB 1st lines differ - on the 1st token, expected: '2', found: '1'
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 271 ms 60084 KB Output is correct
2 Correct 248 ms 65976 KB Output is correct
3 Correct 89 ms 50004 KB Output is correct
4 Correct 82 ms 49996 KB Output is correct
5 Correct 797 ms 117756 KB Output is correct
6 Correct 828 ms 132180 KB Output is correct
7 Correct 12 ms 21460 KB Output is correct
8 Correct 337 ms 68544 KB Output is correct
9 Correct 377 ms 75688 KB Output is correct
10 Correct 216 ms 60520 KB Output is correct
11 Correct 238 ms 65900 KB Output is correct
12 Correct 11 ms 21460 KB Output is correct
13 Correct 14 ms 21464 KB Output is correct
14 Correct 12 ms 21460 KB Output is correct
15 Correct 12 ms 21460 KB Output is correct
16 Correct 81 ms 49996 KB Output is correct
17 Correct 79 ms 49988 KB Output is correct
18 Correct 274 ms 65700 KB Output is correct
19 Correct 349 ms 72084 KB Output is correct
20 Correct 247 ms 63228 KB Output is correct
21 Correct 228 ms 62440 KB Output is correct
22 Correct 244 ms 63060 KB Output is correct
23 Correct 268 ms 67704 KB Output is correct
24 Correct 84 ms 50012 KB Output is correct
25 Correct 81 ms 50008 KB Output is correct
26 Correct 118 ms 49456 KB Output is correct
27 Correct 117 ms 51420 KB Output is correct
28 Correct 175 ms 54448 KB Output is correct
29 Correct 200 ms 54348 KB Output is correct
30 Correct 169 ms 54416 KB Output is correct
31 Correct 184 ms 54332 KB Output is correct
32 Correct 12 ms 21444 KB Output is correct
33 Correct 11 ms 21460 KB Output is correct
34 Correct 12 ms 21460 KB Output is correct
35 Correct 13 ms 21460 KB Output is correct
36 Correct 12 ms 21436 KB Output is correct
37 Correct 12 ms 21396 KB Output is correct
38 Correct 12 ms 21460 KB Output is correct
39 Incorrect 11 ms 21436 KB 1st lines differ - on the 1st token, expected: '2', found: '1'
40 Halted 0 ms 0 KB -