답안 #1019907

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1019907 2024-07-11T10:31:22 Z Gray 메기 농장 (IOI22_fish) C++17
100 / 100
142 ms 36432 KB
#include "fish.h"
 
#include <algorithm>
#include <cassert>
#include <deque>
#include <iostream>
#include <vector>
#define ll long long
#define ff first
#define ss second
#define ln "\n"
using namespace std;
 
 
 
long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y,
                      std::vector<int> W) {
    ll n=N, m=M;
    vector a(N, vector<pair<ll, ll>>());
    vector dp(N, vector<pair<ll, ll>>());
    for (ll i=0; i<m; i++){
        a[X[i]].push_back({W[i], Y[i]});
        dp[X[i]].push_back({0, 0});
    }
    for (ll i=0; i<n; i++){
        sort(a[i].begin(), a[i].end(), [](auto op1, auto op2)->bool{ return op1.ss<op2.ss;});
        dp[i].push_back({0, 0});
    }
    for (ll i=1; i<N; i++){
        // cout << i << ": " << endl;
        deque<pair<ll, ll>> bestU;
        pair<ll, ll> mx = {-1, -1};
        ll sum=0, rsum=0;
        for (ll j=0; j<(ll)a[i].size(); j++) rsum+=a[i][j].ff;
        sum=rsum;
        ll pi=a[i].size()-1;
        for (ll j=a[i-1].size(); j>=0; j--){
            while (j<(ll)a[i-1].size() and pi>=0 and a[i-1][j].ss-1<a[i][pi].ss) sum-=a[i][pi--].ff;
            if (max(dp[i-1][j].ff, dp[i-1][j].ss)+sum>mx.ff){
                mx = {max(dp[i-1][j].ff, dp[i-1][j].ss)+sum, (j==(ll)a[i-1].size())?n:a[i-1][j].ss-1};
                bestU.push_back(mx);
                // cout << "Add: " << mx.ff << " " << mx.ss << " at " << j << endl;
            }
        }
        sum=0;
        for (ll j=0; j<=(ll)a[i].size(); j++){
            while (!bestU.empty() and bestU.back().ss<(j==(ll)a[i].size()?n:a[i][j].ss-1)) {
                // cout << "Remove: " << bestU.back().ff << " " << bestU.back().ss << " at " << j << endl;
                bestU.pop_back();
            }
            // cout << i << "-" << j << endl;
            dp[i][j].ss=(bestU.empty()?0:bestU.back().ff)-sum;
            if (j<(ll)a[i].size()) sum+=a[i][j].ff;
            // cout << i << "-" << j << endl;
        }
        if (i>1){ //-Change-
            // cout << i << ": ";
            pair<ll, ll> best = {-1, -1};
            for (ll j=0; j<=(ll)a[i-2].size(); j++){
                best=max(best, {max(dp[i-2][j].ff, dp[i-2][j].ss), (j==(ll)(a[i-2].size())?n:a[i-2][j].ss-1)});
            }
            ll osum=0;
            for (ll j=0; j<(ll)a[i-1].size() and a[i-1][j].ss<=best.ss; j++){
                osum+=a[i-1][j].ff;
            }
            // cout << osum << " -> ";
            sum=0;
            pi=0;
            for (ll j=0; j<=(ll)a[i].size(); j++){
                while (pi<(ll)a[i-1].size() and a[i-1][pi].ss<=(j==(ll)(a[i].size())?n:a[i][j].ss-1)) sum+=a[i-1][pi++].ff;
                dp[i][j].ff=best.ff+max(sum, osum);
            }
        }
        assert(bestU.size()<=1);
        while (!bestU.empty()) bestU.pop_back();
        mx = {-1, -1};
        sum=rsum=0;
        for (ll j=0; j<(ll)a[i-1].size(); j++) rsum+=a[i-1][j].ff;
        for (ll j=0; j<=(ll)a[i-1].size(); j++){
            if (rsum-sum+dp[i-1][j].ff>mx.ff){
                mx = {rsum-sum+dp[i-1][j].ff, (j==(ll)a[i-1].size()?n:a[i-1][j].ss-1)};
                bestU.push_back(mx);
                // cout << "Add: " << mx.ff << " " << mx.ss << " at " << j << endl;
            }
            if (j<(ll)a[i-1].size()) sum+=a[i-1][j].ff;
        }
        sum=0;
        pi=(ll)a[i-1].size()-1;
        for (ll j=(ll)(a[i].size()); j>=0; j--){
            // cout << "Spec: " << a[i-1][pi].ss << " w " << (j==(ll)(a[i].size())?n+1:a[i][j].ss-1) << endl;
            while (pi>=0 and a[i-1][pi].ss>(j==(ll)(a[i].size())?n+1:a[i][j].ss-1)) {sum+=a[i-1][pi--].ff;}
            while (!bestU.empty() and bestU.back().ss>(j==(ll)(a[i].size())?n+1:a[i][j].ss-1)) {
                // cout << "Remove: " << bestU.back().ff << " " << bestU.back().ss << " at " << j << endl;
                bestU.pop_back();
            }
            // cout << i << "-" << j << "-" << sum << endl;
            dp[i][j].ff=max(dp[i][j].ff, (bestU.empty()?0:bestU.back().ff)-sum);
        }
    }
    // for (ll i=0; i<n; i++){
    //     for (ll j=0; j<(ll)dp[i].size(); j++) cout << dp[i][j].ff << " ";
    //     cout << ln;
    // }
    // for (ll i=0; i<n; i++){
    //     for (ll j=0; j<(ll)dp[i].size(); j++) cout << dp[i][j].ss << " ";
    //     cout << ln;
    // }
    // Thing to lookup -> the fact that dp[i][j] is column up to height[i][j]-1;
    ll res=0;
    for (ll i=0; i<=(ll)a[n-1].size(); i++){
        res=max(res, max(dp[n-1][i].ff,dp[n-1][i].ss));
    }
    return res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 11704 KB Output is correct
2 Correct 39 ms 13760 KB Output is correct
3 Correct 12 ms 8024 KB Output is correct
4 Correct 12 ms 8024 KB Output is correct
5 Correct 111 ms 26228 KB Output is correct
6 Correct 133 ms 29012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 76 ms 16284 KB Output is correct
3 Correct 71 ms 22588 KB Output is correct
4 Correct 34 ms 13244 KB Output is correct
5 Correct 42 ms 15284 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 440 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 12 ms 8028 KB Output is correct
11 Correct 13 ms 8028 KB Output is correct
12 Correct 35 ms 14524 KB Output is correct
13 Correct 43 ms 17080 KB Output is correct
14 Correct 63 ms 13336 KB Output is correct
15 Correct 38 ms 14652 KB Output is correct
16 Correct 35 ms 13140 KB Output is correct
17 Correct 37 ms 14680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 8024 KB Output is correct
2 Correct 13 ms 8028 KB Output is correct
3 Correct 39 ms 11604 KB Output is correct
4 Correct 29 ms 10588 KB Output is correct
5 Correct 62 ms 18212 KB Output is correct
6 Correct 81 ms 18252 KB Output is correct
7 Correct 73 ms 18260 KB Output is correct
8 Correct 64 ms 18424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 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 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 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 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 452 KB Output is correct
17 Correct 12 ms 3932 KB Output is correct
18 Correct 13 ms 5232 KB Output is correct
19 Correct 14 ms 4700 KB Output is correct
20 Correct 16 ms 4924 KB Output is correct
21 Correct 14 ms 4784 KB Output is correct
22 Correct 24 ms 9460 KB Output is correct
23 Correct 3 ms 1116 KB Output is correct
24 Correct 8 ms 2816 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 3 ms 1116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 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 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 452 KB Output is correct
17 Correct 12 ms 3932 KB Output is correct
18 Correct 13 ms 5232 KB Output is correct
19 Correct 14 ms 4700 KB Output is correct
20 Correct 16 ms 4924 KB Output is correct
21 Correct 14 ms 4784 KB Output is correct
22 Correct 24 ms 9460 KB Output is correct
23 Correct 3 ms 1116 KB Output is correct
24 Correct 8 ms 2816 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 3 ms 1116 KB Output is correct
27 Correct 2 ms 860 KB Output is correct
28 Correct 62 ms 20876 KB Output is correct
29 Correct 94 ms 27472 KB Output is correct
30 Correct 119 ms 26200 KB Output is correct
31 Correct 124 ms 26212 KB Output is correct
32 Correct 86 ms 29524 KB Output is correct
33 Correct 99 ms 26328 KB Output is correct
34 Correct 96 ms 26196 KB Output is correct
35 Correct 37 ms 11356 KB Output is correct
36 Correct 93 ms 29780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 8024 KB Output is correct
2 Correct 13 ms 8028 KB Output is correct
3 Correct 39 ms 11604 KB Output is correct
4 Correct 29 ms 10588 KB Output is correct
5 Correct 62 ms 18212 KB Output is correct
6 Correct 81 ms 18252 KB Output is correct
7 Correct 73 ms 18260 KB Output is correct
8 Correct 64 ms 18424 KB Output is correct
9 Correct 63 ms 18392 KB Output is correct
10 Correct 50 ms 13392 KB Output is correct
11 Correct 117 ms 30072 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 14 ms 8212 KB Output is correct
19 Correct 13 ms 8028 KB Output is correct
20 Correct 21 ms 8028 KB Output is correct
21 Correct 12 ms 8028 KB Output is correct
22 Correct 59 ms 17244 KB Output is correct
23 Correct 115 ms 30284 KB Output is correct
24 Correct 95 ms 30812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 11704 KB Output is correct
2 Correct 39 ms 13760 KB Output is correct
3 Correct 12 ms 8024 KB Output is correct
4 Correct 12 ms 8024 KB Output is correct
5 Correct 111 ms 26228 KB Output is correct
6 Correct 133 ms 29012 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 76 ms 16284 KB Output is correct
9 Correct 71 ms 22588 KB Output is correct
10 Correct 34 ms 13244 KB Output is correct
11 Correct 42 ms 15284 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 440 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 12 ms 8028 KB Output is correct
17 Correct 13 ms 8028 KB Output is correct
18 Correct 35 ms 14524 KB Output is correct
19 Correct 43 ms 17080 KB Output is correct
20 Correct 63 ms 13336 KB Output is correct
21 Correct 38 ms 14652 KB Output is correct
22 Correct 35 ms 13140 KB Output is correct
23 Correct 37 ms 14680 KB Output is correct
24 Correct 13 ms 8024 KB Output is correct
25 Correct 13 ms 8028 KB Output is correct
26 Correct 39 ms 11604 KB Output is correct
27 Correct 29 ms 10588 KB Output is correct
28 Correct 62 ms 18212 KB Output is correct
29 Correct 81 ms 18252 KB Output is correct
30 Correct 73 ms 18260 KB Output is correct
31 Correct 64 ms 18424 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 1 ms 604 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 344 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 1 ms 452 KB Output is correct
48 Correct 12 ms 3932 KB Output is correct
49 Correct 13 ms 5232 KB Output is correct
50 Correct 14 ms 4700 KB Output is correct
51 Correct 16 ms 4924 KB Output is correct
52 Correct 14 ms 4784 KB Output is correct
53 Correct 24 ms 9460 KB Output is correct
54 Correct 3 ms 1116 KB Output is correct
55 Correct 8 ms 2816 KB Output is correct
56 Correct 1 ms 348 KB Output is correct
57 Correct 3 ms 1116 KB Output is correct
58 Correct 2 ms 860 KB Output is correct
59 Correct 62 ms 20876 KB Output is correct
60 Correct 94 ms 27472 KB Output is correct
61 Correct 119 ms 26200 KB Output is correct
62 Correct 124 ms 26212 KB Output is correct
63 Correct 86 ms 29524 KB Output is correct
64 Correct 99 ms 26328 KB Output is correct
65 Correct 96 ms 26196 KB Output is correct
66 Correct 37 ms 11356 KB Output is correct
67 Correct 93 ms 29780 KB Output is correct
68 Correct 63 ms 18392 KB Output is correct
69 Correct 50 ms 13392 KB Output is correct
70 Correct 117 ms 30072 KB Output is correct
71 Correct 1 ms 344 KB Output is correct
72 Correct 0 ms 348 KB Output is correct
73 Correct 1 ms 348 KB Output is correct
74 Correct 1 ms 344 KB Output is correct
75 Correct 0 ms 348 KB Output is correct
76 Correct 0 ms 348 KB Output is correct
77 Correct 14 ms 8212 KB Output is correct
78 Correct 13 ms 8028 KB Output is correct
79 Correct 21 ms 8028 KB Output is correct
80 Correct 12 ms 8028 KB Output is correct
81 Correct 59 ms 17244 KB Output is correct
82 Correct 115 ms 30284 KB Output is correct
83 Correct 95 ms 30812 KB Output is correct
84 Correct 115 ms 29964 KB Output is correct
85 Correct 103 ms 30380 KB Output is correct
86 Correct 132 ms 35412 KB Output is correct
87 Correct 142 ms 36432 KB Output is correct
88 Correct 1 ms 348 KB Output is correct
89 Correct 137 ms 36180 KB Output is correct
90 Correct 130 ms 34128 KB Output is correct
91 Correct 113 ms 34120 KB Output is correct