Submission #829379

# Submission time Handle Problem Language Result Execution time Memory
829379 2023-08-18T09:59:31 Z tolbi Catfish Farm (IOI22_fish) C++17
100 / 100
622 ms 95240 KB
#include <bits/stdc++.h>
using namespace std;
#define coutarr(x) for (auto &it : x) cout<<it<<" ";cout<<endl;
typedef long long ll;
#include "fish.h"
vector<vector<ll>> dp;
vector<int> X,Y;
int N,M;
vector<vector<ll>> pref;
vector<vector<int>> ind;
vector<vector<int>> pos;
ll query(int x, int l, int r){
    if (pos[x].back()<l) return 0ll;
    if (pos[x][0]>r) return 0ll;
    if (l>r) return 0ll;
    auto itl = lower_bound(pos[x].begin(), pos[x].end(), l);
    auto itr = lower_bound(pos[x].begin(), pos[x].end(), r+1);
    itr--;
    ll hueh = 0;
    if (itl!=pos[x].begin()){
        itl--;
        hueh = pref[x][(itl-pos[x].begin())];
    }
    return pref[x][(itr-pos[x].begin())]-hueh;
}
int getup(int node){
    int xv = X[node];
    int yv = Y[node];
    return ind[xv][lower_bound(pos[xv].begin(), pos[xv].end(), yv+1)-pos[xv].begin()];
}
int getdown(int node){
    int xv = X[node];
    int yv = Y[node];
    return ind[xv][lower_bound(pos[xv].begin(), pos[xv].end(), yv)-pos[xv].begin()-1];
}
int getnextup(int node){
    int xv = X[node];
    int yv = Y[node];
    return ind[xv+1][lower_bound(pos[xv+1].begin(), pos[xv+1].end(), yv)-pos[xv+1].begin()];
}
int getnextdown(int node){
    int xv = X[node];
    int yv = Y[node];
    return ind[xv+1][lower_bound(pos[xv+1].begin(), pos[xv+1].end(), yv+1)-pos[xv+1].begin()-1];
}
long long f(int node, int flag){
    int x = X[node];
    int y = Y[node];
    //cout<<x<<" "<<y<<" "<<flag<<endl;
    if (dp[node][flag]!=-1) return dp[node][flag];
    if (flag==1){
        dp[node][flag]=0;
        if (y<N){
            ll crr = 0;
            int next = getup(node);
            if (x>0) crr = query(x-1,y,Y[next]-1);
            dp[node][flag]=crr+f(getup(node),1);
        }
        if (x+1<N){
            int next = getnextup(node);
            ll crr = query(x,y,Y[next]-1);
            dp[node][flag]=max(dp[node][flag],crr+f(next,1));
            next=getnextdown(node);
            crr = query(x+1,Y[next],y-1);
            dp[node][flag]=max(dp[node][flag],crr+f(next,0));
        }
    }
    else if (flag==0){
        dp[node][flag]=0;
        if (y-1>=0){
            int next = getdown(node);
            dp[node][flag]=query(x,Y[next],y-1)+f(next,0);
        }
        if (x+1<N){
            dp[node][flag]=max(dp[node][flag],f(getnextup(node),0));
            if (y==0){
                dp[node][flag]=max(dp[node][flag],f(M+x+1,2));
            }
        }
    }
    else {
        dp[node][flag]=0;
        if (y<N){
            dp[node][flag]=f(getup(node),2);
        }
        if (x+1<N){
            int next = getnextup(node);
            ll crr = query(x,y,Y[next]-1);
            dp[node][flag]=max(dp[node][flag],crr+f(next,1));
            next=getnextdown(node);
            crr = query(x+1,Y[next],y-1);
            dp[node][flag]=max(dp[node][flag],crr+f(next,0));
        }
    }
    if (flag!=0 && x+2<N){
        dp[node][flag]=max(dp[node][flag],f(M+x+2,1));
    }
    //cout<<x<<" "<<y<<" "<<flag<<" "<<dp[node][flag]<<endl;
    return dp[node][flag];
};

long long max_weights(int _N, int _M, std::vector<int> _X, std::vector<int> _Y, std::vector<int> _W) {
    vector<vector<pair<int,pair<ll,int>>>> arr;
    N=_N;
    vector<int> W;
    arr.resize(N);
    vector<int> bb(N,0);
    for (int i = 0; i < _Y.size(); ++i)
    {
        if (_Y[i]==0) bb[_X[i]]=_W[i];
        else {
            X.push_back(_X[i]);
            Y.push_back(_Y[i]);
            W.push_back(_W[i]);
            M++;
        }
    }
    for (int i = 0; i < N; ++i)
    {
        X.push_back(i);
        Y.push_back(0);
        W.push_back(bb[i]);
    }
    for (int i = 0; i < N; i++){
        X.push_back(i);
        Y.push_back(N);
        W.push_back(0);
    }
    for (int i = 0; i < Y.size(); i++){
        arr[X[i]].push_back({Y[i],{W[i],i}});
    }
    pref.resize(arr.size());
    pos.resize(arr.size());
    ind.resize(arr.size());
    for (int i = 0; i < N; ++i)
    {
        sort(arr[i].begin(), arr[i].end());
        pref[i].resize(arr[i].size());
        pos[i].resize(arr[i].size());
        ind[i].resize(arr[i].size());
        for (int j = 0; j < arr[i].size(); j++){
            pref[i][j]=arr[i][j].second.first;
            if (j) pref[i][j]+=pref[i][j-1];
            pos[i][j]=arr[i][j].first;
            ind[i][j]=arr[i][j].second.second;
        }
    }
    //0 decreasing
    //1 increasing
    //2 increasing, \
    but forbidden to profit from back
    //coutarr(X);
    //coutarr(Y);
    //coutarr(W);
    //cout<<M<<endl;
    dp.resize(Y.size(),vector<ll>(3,-1));
    return f(M,2);
}

Compilation message

fish.cpp:150:5: warning: multi-line comment [-Wcomment]
  150 |     //2 increasing, \
      |     ^
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:108:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  108 |     for (int i = 0; i < _Y.size(); ++i)
      |                     ~~^~~~~~~~~~~
fish.cpp:129:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  129 |     for (int i = 0; i < Y.size(); i++){
      |                     ~~^~~~~~~~~~
fish.cpp:141:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<long long int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  141 |         for (int j = 0; j < arr[i].size(); j++){
      |                         ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 166 ms 61612 KB Output is correct
2 Correct 160 ms 70764 KB Output is correct
3 Correct 72 ms 48368 KB Output is correct
4 Correct 72 ms 48448 KB Output is correct
5 Correct 582 ms 95240 KB Output is correct
6 Correct 369 ms 89616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 248 KB Output is correct
2 Correct 203 ms 72192 KB Output is correct
3 Correct 289 ms 83696 KB Output is correct
4 Correct 125 ms 61648 KB Output is correct
5 Correct 155 ms 70640 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 70 ms 48444 KB Output is correct
11 Correct 65 ms 48444 KB Output is correct
12 Correct 144 ms 54036 KB Output is correct
13 Correct 202 ms 61320 KB Output is correct
14 Correct 127 ms 57800 KB Output is correct
15 Correct 161 ms 64840 KB Output is correct
16 Correct 127 ms 57948 KB Output is correct
17 Correct 149 ms 64308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 67 ms 48388 KB Output is correct
2 Correct 68 ms 48448 KB Output is correct
3 Correct 72 ms 44856 KB Output is correct
4 Correct 73 ms 49304 KB Output is correct
5 Correct 96 ms 50672 KB Output is correct
6 Correct 88 ms 50808 KB Output is correct
7 Correct 87 ms 50788 KB Output is correct
8 Correct 88 ms 50732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 2 ms 736 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 2 ms 596 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 2 ms 736 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 2 ms 596 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 3 ms 596 KB Output is correct
17 Correct 44 ms 6468 KB Output is correct
18 Correct 49 ms 7232 KB Output is correct
19 Correct 34 ms 6988 KB Output is correct
20 Correct 34 ms 6972 KB Output is correct
21 Correct 35 ms 7028 KB Output is correct
22 Correct 123 ms 13364 KB Output is correct
23 Correct 7 ms 1748 KB Output is correct
24 Correct 28 ms 4520 KB Output is correct
25 Correct 1 ms 596 KB Output is correct
26 Correct 8 ms 1620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 2 ms 736 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 2 ms 596 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 3 ms 596 KB Output is correct
17 Correct 44 ms 6468 KB Output is correct
18 Correct 49 ms 7232 KB Output is correct
19 Correct 34 ms 6988 KB Output is correct
20 Correct 34 ms 6972 KB Output is correct
21 Correct 35 ms 7028 KB Output is correct
22 Correct 123 ms 13364 KB Output is correct
23 Correct 7 ms 1748 KB Output is correct
24 Correct 28 ms 4520 KB Output is correct
25 Correct 1 ms 596 KB Output is correct
26 Correct 8 ms 1620 KB Output is correct
27 Correct 4 ms 2132 KB Output is correct
28 Correct 266 ms 30956 KB Output is correct
29 Correct 376 ms 43292 KB Output is correct
30 Correct 376 ms 42300 KB Output is correct
31 Correct 407 ms 42300 KB Output is correct
32 Correct 345 ms 43184 KB Output is correct
33 Correct 354 ms 42304 KB Output is correct
34 Correct 368 ms 42316 KB Output is correct
35 Correct 118 ms 17896 KB Output is correct
36 Correct 412 ms 44208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 67 ms 48388 KB Output is correct
2 Correct 68 ms 48448 KB Output is correct
3 Correct 72 ms 44856 KB Output is correct
4 Correct 73 ms 49304 KB Output is correct
5 Correct 96 ms 50672 KB Output is correct
6 Correct 88 ms 50808 KB Output is correct
7 Correct 87 ms 50788 KB Output is correct
8 Correct 88 ms 50732 KB Output is correct
9 Correct 141 ms 62284 KB Output is correct
10 Correct 78 ms 32488 KB Output is correct
11 Correct 176 ms 64664 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 64 ms 48444 KB Output is correct
19 Correct 66 ms 48396 KB Output is correct
20 Correct 79 ms 48400 KB Output is correct
21 Correct 71 ms 48440 KB Output is correct
22 Correct 145 ms 61092 KB Output is correct
23 Correct 212 ms 72736 KB Output is correct
24 Correct 250 ms 73252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 166 ms 61612 KB Output is correct
2 Correct 160 ms 70764 KB Output is correct
3 Correct 72 ms 48368 KB Output is correct
4 Correct 72 ms 48448 KB Output is correct
5 Correct 582 ms 95240 KB Output is correct
6 Correct 369 ms 89616 KB Output is correct
7 Correct 1 ms 248 KB Output is correct
8 Correct 203 ms 72192 KB Output is correct
9 Correct 289 ms 83696 KB Output is correct
10 Correct 125 ms 61648 KB Output is correct
11 Correct 155 ms 70640 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 70 ms 48444 KB Output is correct
17 Correct 65 ms 48444 KB Output is correct
18 Correct 144 ms 54036 KB Output is correct
19 Correct 202 ms 61320 KB Output is correct
20 Correct 127 ms 57800 KB Output is correct
21 Correct 161 ms 64840 KB Output is correct
22 Correct 127 ms 57948 KB Output is correct
23 Correct 149 ms 64308 KB Output is correct
24 Correct 67 ms 48388 KB Output is correct
25 Correct 68 ms 48448 KB Output is correct
26 Correct 72 ms 44856 KB Output is correct
27 Correct 73 ms 49304 KB Output is correct
28 Correct 96 ms 50672 KB Output is correct
29 Correct 88 ms 50808 KB Output is correct
30 Correct 87 ms 50788 KB Output is correct
31 Correct 88 ms 50732 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 2 ms 736 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 2 ms 596 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 2 ms 468 KB Output is correct
46 Correct 1 ms 468 KB Output is correct
47 Correct 3 ms 596 KB Output is correct
48 Correct 44 ms 6468 KB Output is correct
49 Correct 49 ms 7232 KB Output is correct
50 Correct 34 ms 6988 KB Output is correct
51 Correct 34 ms 6972 KB Output is correct
52 Correct 35 ms 7028 KB Output is correct
53 Correct 123 ms 13364 KB Output is correct
54 Correct 7 ms 1748 KB Output is correct
55 Correct 28 ms 4520 KB Output is correct
56 Correct 1 ms 596 KB Output is correct
57 Correct 8 ms 1620 KB Output is correct
58 Correct 4 ms 2132 KB Output is correct
59 Correct 266 ms 30956 KB Output is correct
60 Correct 376 ms 43292 KB Output is correct
61 Correct 376 ms 42300 KB Output is correct
62 Correct 407 ms 42300 KB Output is correct
63 Correct 345 ms 43184 KB Output is correct
64 Correct 354 ms 42304 KB Output is correct
65 Correct 368 ms 42316 KB Output is correct
66 Correct 118 ms 17896 KB Output is correct
67 Correct 412 ms 44208 KB Output is correct
68 Correct 141 ms 62284 KB Output is correct
69 Correct 78 ms 32488 KB Output is correct
70 Correct 176 ms 64664 KB Output is correct
71 Correct 0 ms 212 KB Output is correct
72 Correct 0 ms 212 KB Output is correct
73 Correct 0 ms 212 KB Output is correct
74 Correct 0 ms 212 KB Output is correct
75 Correct 1 ms 212 KB Output is correct
76 Correct 0 ms 212 KB Output is correct
77 Correct 64 ms 48444 KB Output is correct
78 Correct 66 ms 48396 KB Output is correct
79 Correct 79 ms 48400 KB Output is correct
80 Correct 71 ms 48440 KB Output is correct
81 Correct 145 ms 61092 KB Output is correct
82 Correct 212 ms 72736 KB Output is correct
83 Correct 250 ms 73252 KB Output is correct
84 Correct 553 ms 85276 KB Output is correct
85 Correct 622 ms 89380 KB Output is correct
86 Correct 348 ms 83624 KB Output is correct
87 Correct 368 ms 88636 KB Output is correct
88 Correct 0 ms 212 KB Output is correct
89 Correct 379 ms 88648 KB Output is correct
90 Correct 334 ms 87672 KB Output is correct
91 Correct 300 ms 87484 KB Output is correct