답안 #627307

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
627307 2022-08-12T12:35:15 Z czhang2718 메기 농장 (IOI22_fish) C++17
100 / 100
210 ms 59896 KB
#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
#define f first
#define s second

const int N=3e5+4;
int n,m;
vector<pair<int,int>> fish[N];
vector<ll> dp[N], dp1[N], inc[N];

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

    for(int i=1; i<=n; i++){
        fish[i].push_back({0, 0});
        fish[i].push_back({n, 0});
        sort(fish[i].begin(), fish[i].end());
        int p=fish[i].size();
        if(p>1 && fish[i][1].f==fish[i][0].f) fish[i].erase(fish[i].begin());
    }

    int i=0;
    fish[i].push_back({0, 0});
    dp[i].resize(2);
    dp1[i].resize(2);
    inc[i].resize(2);
    
    for(int i=1; i<=n; i++){
        int p=fish[i].size();
        dp[i].resize(p+1, -1e18);
        dp1[i].resize(p+1, -1e18);
        inc[i].resize(p+1, -1e18);
        ll sum=0;
        pair<int, ll> nxt={0,0}, prv={0,0};
        for(int j=0; j<p; j++){
            auto fi=fish[i][j];
            int y=fi.f, w=fi.s;
            while(nxt.f<fish[i+1].size() && fish[i+1][nxt.f].f+1<=y){
                nxt.s+=fish[i+1][nxt.f].s;
                nxt.f++;
            }
            while(prv.f<fish[i-1].size() && fish[i-1][prv.f].f+1<=y){
                prv.s+=fish[i-1][prv.f].s;
                prv.f++;
            }
            int k=upper_bound(fish[i-1].begin(), fish[i-1].end(), make_pair(y, int(1e9)))-fish[i-1].begin();
            dp[i][j]=max(dp1[i-1][k]-sum, prv.s+inc[i-1][k-1]);
            // cout << "inc " << prv.s+(fish[i-1][k-1].f?inc[i-1][k-1]:0) << "\n";
            int k2;
            if(i>=2){
                k2=upper_bound(fish[i-2].begin(), fish[i-2].end(), make_pair(y, int(1e9)))-fish[i-2].begin();
                dp[i][j]=max({dp[i][j], dp1[i-2][k2], dp[i-2][k2-1]+prv.s});
            }
            inc[i][j]=max(
                j?inc[i][j-1]:ll(-1e18), 
                -sum + max(prv.s+inc[i-1][k-1], i>=2?max(dp1[i-2][k2], dp[i-2][k2-1]+prv.s):0));
            sum+=w;
            // cout << "inc[" << i << "][" << j << "] " << inc[i][j] << "\n";
            // cout << "dp[" << i << "][" << j << "] " << dp[i][j] << "\n";
            dp1[i][j]=dp[i][j]+nxt.s;
            if(j) dp[i][j]=max(dp[i][j], dp[i][j-1]);
        }

        for(int j=p-1; j>=0; j--){
            dp1[i][j]=max(dp1[i][j+1], dp1[i][j]);
        }
    }


    return dp1[n][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:43:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |             while(nxt.f<fish[i+1].size() && fish[i+1][nxt.f].f+1<=y){
      |                   ~~~~~^~~~~~~~~~~~~~~~~
fish.cpp:47:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |             while(prv.f<fish[i-1].size() && fish[i-1][prv.f].f+1<=y){
      |                   ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 72 ms 44064 KB Output is correct
2 Correct 86 ms 46408 KB Output is correct
3 Correct 46 ms 40968 KB Output is correct
4 Correct 50 ms 40908 KB Output is correct
5 Correct 165 ms 57416 KB Output is correct
6 Correct 204 ms 59272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 28496 KB Output is correct
2 Correct 107 ms 48556 KB Output is correct
3 Correct 122 ms 51980 KB Output is correct
4 Correct 74 ms 44088 KB Output is correct
5 Correct 80 ms 46420 KB Output is correct
6 Correct 16 ms 28372 KB Output is correct
7 Correct 17 ms 28456 KB Output is correct
8 Correct 15 ms 28480 KB Output is correct
9 Correct 17 ms 28476 KB Output is correct
10 Correct 55 ms 40928 KB Output is correct
11 Correct 43 ms 40920 KB Output is correct
12 Correct 71 ms 44164 KB Output is correct
13 Correct 89 ms 46432 KB Output is correct
14 Correct 73 ms 44196 KB Output is correct
15 Correct 84 ms 45944 KB Output is correct
16 Correct 73 ms 44232 KB Output is correct
17 Correct 80 ms 45868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 41012 KB Output is correct
2 Correct 45 ms 40984 KB Output is correct
3 Correct 60 ms 41924 KB Output is correct
4 Correct 66 ms 42484 KB Output is correct
5 Correct 83 ms 45336 KB Output is correct
6 Correct 91 ms 45400 KB Output is correct
7 Correct 90 ms 45348 KB Output is correct
8 Correct 87 ms 45320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 28480 KB Output is correct
2 Correct 18 ms 28372 KB Output is correct
3 Correct 15 ms 28380 KB Output is correct
4 Correct 16 ms 28480 KB Output is correct
5 Correct 14 ms 28372 KB Output is correct
6 Correct 17 ms 28500 KB Output is correct
7 Correct 15 ms 28484 KB Output is correct
8 Correct 14 ms 28372 KB Output is correct
9 Correct 15 ms 28500 KB Output is correct
10 Correct 15 ms 28668 KB Output is correct
11 Correct 14 ms 28548 KB Output is correct
12 Correct 15 ms 28500 KB Output is correct
13 Correct 17 ms 28500 KB Output is correct
14 Correct 15 ms 28560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 28480 KB Output is correct
2 Correct 18 ms 28372 KB Output is correct
3 Correct 15 ms 28380 KB Output is correct
4 Correct 16 ms 28480 KB Output is correct
5 Correct 14 ms 28372 KB Output is correct
6 Correct 17 ms 28500 KB Output is correct
7 Correct 15 ms 28484 KB Output is correct
8 Correct 14 ms 28372 KB Output is correct
9 Correct 15 ms 28500 KB Output is correct
10 Correct 15 ms 28668 KB Output is correct
11 Correct 14 ms 28548 KB Output is correct
12 Correct 15 ms 28500 KB Output is correct
13 Correct 17 ms 28500 KB Output is correct
14 Correct 15 ms 28560 KB Output is correct
15 Correct 15 ms 28472 KB Output is correct
16 Correct 15 ms 28500 KB Output is correct
17 Correct 36 ms 31052 KB Output is correct
18 Correct 33 ms 31408 KB Output is correct
19 Correct 33 ms 31316 KB Output is correct
20 Correct 38 ms 31312 KB Output is correct
21 Correct 31 ms 31316 KB Output is correct
22 Correct 48 ms 33996 KB Output is correct
23 Correct 18 ms 29012 KB Output is correct
24 Correct 29 ms 30204 KB Output is correct
25 Correct 16 ms 28496 KB Output is correct
26 Correct 18 ms 28976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 28480 KB Output is correct
2 Correct 18 ms 28372 KB Output is correct
3 Correct 15 ms 28380 KB Output is correct
4 Correct 16 ms 28480 KB Output is correct
5 Correct 14 ms 28372 KB Output is correct
6 Correct 17 ms 28500 KB Output is correct
7 Correct 15 ms 28484 KB Output is correct
8 Correct 14 ms 28372 KB Output is correct
9 Correct 15 ms 28500 KB Output is correct
10 Correct 15 ms 28668 KB Output is correct
11 Correct 14 ms 28548 KB Output is correct
12 Correct 15 ms 28500 KB Output is correct
13 Correct 17 ms 28500 KB Output is correct
14 Correct 15 ms 28560 KB Output is correct
15 Correct 15 ms 28472 KB Output is correct
16 Correct 15 ms 28500 KB Output is correct
17 Correct 36 ms 31052 KB Output is correct
18 Correct 33 ms 31408 KB Output is correct
19 Correct 33 ms 31316 KB Output is correct
20 Correct 38 ms 31312 KB Output is correct
21 Correct 31 ms 31316 KB Output is correct
22 Correct 48 ms 33996 KB Output is correct
23 Correct 18 ms 29012 KB Output is correct
24 Correct 29 ms 30204 KB Output is correct
25 Correct 16 ms 28496 KB Output is correct
26 Correct 18 ms 28976 KB Output is correct
27 Correct 18 ms 29012 KB Output is correct
28 Correct 121 ms 41756 KB Output is correct
29 Correct 153 ms 46412 KB Output is correct
30 Correct 142 ms 46052 KB Output is correct
31 Correct 166 ms 46084 KB Output is correct
32 Correct 138 ms 47240 KB Output is correct
33 Correct 140 ms 46068 KB Output is correct
34 Correct 141 ms 46088 KB Output is correct
35 Correct 79 ms 35616 KB Output is correct
36 Correct 158 ms 46628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 41012 KB Output is correct
2 Correct 45 ms 40984 KB Output is correct
3 Correct 60 ms 41924 KB Output is correct
4 Correct 66 ms 42484 KB Output is correct
5 Correct 83 ms 45336 KB Output is correct
6 Correct 91 ms 45400 KB Output is correct
7 Correct 90 ms 45348 KB Output is correct
8 Correct 87 ms 45320 KB Output is correct
9 Correct 90 ms 50388 KB Output is correct
10 Correct 67 ms 40640 KB Output is correct
11 Correct 134 ms 52852 KB Output is correct
12 Correct 18 ms 28372 KB Output is correct
13 Correct 15 ms 28372 KB Output is correct
14 Correct 15 ms 28432 KB Output is correct
15 Correct 14 ms 28372 KB Output is correct
16 Correct 15 ms 28372 KB Output is correct
17 Correct 14 ms 28372 KB Output is correct
18 Correct 44 ms 41036 KB Output is correct
19 Correct 42 ms 40912 KB Output is correct
20 Correct 42 ms 40956 KB Output is correct
21 Correct 43 ms 40908 KB Output is correct
22 Correct 95 ms 47604 KB Output is correct
23 Correct 144 ms 52844 KB Output is correct
24 Correct 126 ms 52844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 72 ms 44064 KB Output is correct
2 Correct 86 ms 46408 KB Output is correct
3 Correct 46 ms 40968 KB Output is correct
4 Correct 50 ms 40908 KB Output is correct
5 Correct 165 ms 57416 KB Output is correct
6 Correct 204 ms 59272 KB Output is correct
7 Correct 17 ms 28496 KB Output is correct
8 Correct 107 ms 48556 KB Output is correct
9 Correct 122 ms 51980 KB Output is correct
10 Correct 74 ms 44088 KB Output is correct
11 Correct 80 ms 46420 KB Output is correct
12 Correct 16 ms 28372 KB Output is correct
13 Correct 17 ms 28456 KB Output is correct
14 Correct 15 ms 28480 KB Output is correct
15 Correct 17 ms 28476 KB Output is correct
16 Correct 55 ms 40928 KB Output is correct
17 Correct 43 ms 40920 KB Output is correct
18 Correct 71 ms 44164 KB Output is correct
19 Correct 89 ms 46432 KB Output is correct
20 Correct 73 ms 44196 KB Output is correct
21 Correct 84 ms 45944 KB Output is correct
22 Correct 73 ms 44232 KB Output is correct
23 Correct 80 ms 45868 KB Output is correct
24 Correct 50 ms 41012 KB Output is correct
25 Correct 45 ms 40984 KB Output is correct
26 Correct 60 ms 41924 KB Output is correct
27 Correct 66 ms 42484 KB Output is correct
28 Correct 83 ms 45336 KB Output is correct
29 Correct 91 ms 45400 KB Output is correct
30 Correct 90 ms 45348 KB Output is correct
31 Correct 87 ms 45320 KB Output is correct
32 Correct 16 ms 28480 KB Output is correct
33 Correct 18 ms 28372 KB Output is correct
34 Correct 15 ms 28380 KB Output is correct
35 Correct 16 ms 28480 KB Output is correct
36 Correct 14 ms 28372 KB Output is correct
37 Correct 17 ms 28500 KB Output is correct
38 Correct 15 ms 28484 KB Output is correct
39 Correct 14 ms 28372 KB Output is correct
40 Correct 15 ms 28500 KB Output is correct
41 Correct 15 ms 28668 KB Output is correct
42 Correct 14 ms 28548 KB Output is correct
43 Correct 15 ms 28500 KB Output is correct
44 Correct 17 ms 28500 KB Output is correct
45 Correct 15 ms 28560 KB Output is correct
46 Correct 15 ms 28472 KB Output is correct
47 Correct 15 ms 28500 KB Output is correct
48 Correct 36 ms 31052 KB Output is correct
49 Correct 33 ms 31408 KB Output is correct
50 Correct 33 ms 31316 KB Output is correct
51 Correct 38 ms 31312 KB Output is correct
52 Correct 31 ms 31316 KB Output is correct
53 Correct 48 ms 33996 KB Output is correct
54 Correct 18 ms 29012 KB Output is correct
55 Correct 29 ms 30204 KB Output is correct
56 Correct 16 ms 28496 KB Output is correct
57 Correct 18 ms 28976 KB Output is correct
58 Correct 18 ms 29012 KB Output is correct
59 Correct 121 ms 41756 KB Output is correct
60 Correct 153 ms 46412 KB Output is correct
61 Correct 142 ms 46052 KB Output is correct
62 Correct 166 ms 46084 KB Output is correct
63 Correct 138 ms 47240 KB Output is correct
64 Correct 140 ms 46068 KB Output is correct
65 Correct 141 ms 46088 KB Output is correct
66 Correct 79 ms 35616 KB Output is correct
67 Correct 158 ms 46628 KB Output is correct
68 Correct 90 ms 50388 KB Output is correct
69 Correct 67 ms 40640 KB Output is correct
70 Correct 134 ms 52852 KB Output is correct
71 Correct 18 ms 28372 KB Output is correct
72 Correct 15 ms 28372 KB Output is correct
73 Correct 15 ms 28432 KB Output is correct
74 Correct 14 ms 28372 KB Output is correct
75 Correct 15 ms 28372 KB Output is correct
76 Correct 14 ms 28372 KB Output is correct
77 Correct 44 ms 41036 KB Output is correct
78 Correct 42 ms 40912 KB Output is correct
79 Correct 42 ms 40956 KB Output is correct
80 Correct 43 ms 40908 KB Output is correct
81 Correct 95 ms 47604 KB Output is correct
82 Correct 144 ms 52844 KB Output is correct
83 Correct 126 ms 52844 KB Output is correct
84 Correct 185 ms 56796 KB Output is correct
85 Correct 191 ms 57768 KB Output is correct
86 Correct 201 ms 58340 KB Output is correct
87 Correct 210 ms 59852 KB Output is correct
88 Correct 16 ms 28372 KB Output is correct
89 Correct 209 ms 59896 KB Output is correct
90 Correct 184 ms 58708 KB Output is correct
91 Correct 177 ms 58432 KB Output is correct