Submission #1004729

# Submission time Handle Problem Language Result Execution time Memory
1004729 2024-06-21T13:18:13 Z andrei_iorgulescu Catfish Farm (IOI22_fish) C++17
100 / 100
290 ms 96004 KB
#include <bits/stdc++.h>
#include "fish.h"

using namespace std;

using integer = int;

#define int long long

int n,m;
vector<pair<int,int>> a[100005];
vector<int> sp[100005];
vector<int> poz[100005];
vector<int> dp0[100005],dp1[100005];
vector<int> mp_dp0[100005],mp_dp1[100005],ms_dp0[100005],ms_dp1[100005];
vector<int> lol1[100005];///maxime pe prefix din dp1[i][h] - f(i,h)
vector<int> lol2[100005];///maxime pe sufix din max(dp0[i][h],dp1[i][h]) + f(i + 1,h)

int max_idx(int col,int h)
{
    int st = -1,pas = 1 << 16;
    while (pas != 0)
    {
        if (st + pas < poz[col].size() and poz[col][st + pas] <= h)
            st += pas;
        pas /= 2;
    }
    return st;
}

int max_idx_2(int col,int h)
{
    int st = -1,pas = 1 << 16;
    while (pas != 0)
    {
        if (st + pas < a[col].size() and a[col][st + pas].first <= h)
            st += pas;
        pas /= 2;
    }
    return st;
}

int f(int col,int h)
{
    int idx = max_idx_2(col,h);
    if (idx == -1)
        return 0;
    return sp[col][idx];
}

void build(int col)
{
    for (int i = 0; i < poz[col].size(); i++)
    {
        if (i == 0)
        {
            mp_dp0[col][i] = dp0[col][i];
            mp_dp1[col][i] = dp1[col][i];
        }
        else
        {
            mp_dp0[col][i] = max(mp_dp0[col][i - 1],dp0[col][i]);
            mp_dp1[col][i] = max(mp_dp1[col][i - 1],dp1[col][i]);
        }
    }
    for (int i = poz[col].size() - 1; i >= 0; i--)
    {
        if (i + 1 == poz[col].size())
        {
            ms_dp0[col][i] = dp0[col][i];
            ms_dp1[col][i] = dp1[col][i];
        }
        else
        {
            ms_dp0[col][i] = max(ms_dp0[col][i + 1],dp0[col][i]);
            ms_dp1[col][i] = max(ms_dp1[col][i + 1],dp1[col][i]);
        }
    }
    for (int i = 0; i < poz[col].size(); i++)
    {
        int vl = dp1[col][i] - f(col,poz[col][i]);
        if (i == 0)
            lol1[col][i] = vl;
        else
            lol1[col][i] = max(lol1[col][i - 1],vl);
    }
    for (int i = poz[col].size() - 1; i >= 0; i--)
    {
        int vl = max(dp0[col][i],dp1[col][i]) + f(col + 1,poz[col][i]);
        if (i + 1 == poz[col].size())
            lol2[col][i] = vl;
        else
            lol2[col][i] = max(lol2[col][i + 1],vl);
    }
}

/*
5 4
0 2 5
1 1 2
4 4 1
3 3 3
*/

int max_weights(integer N, integer M, vector<integer> X, vector<integer> Y, vector<integer> W)
{
    n = N;
    m = M;
    for (int i = 0; i < m; i++)
        swap(X[i],Y[i]);
    for (int i = 0; i < m; i++)
        X[i]++,Y[i]++;
    for (int i = 0; i < m; i++)
        a[Y[i]].push_back({X[i],W[i]});
    for (int i = 0; i < m; i++)
        poz[Y[i]].push_back(X[i] - 1);
    for (int i = 1; i <= n; i++)
    {
        poz[i].push_back(n);
        sort(poz[i].begin(),poz[i].end());
    }
    for (int i = 1; i <= n; i++)
    {
        sort(a[i].begin(),a[i].end());
        sp[i].resize(a[i].size());
        if (a[i].size() == 0)
            continue;
        sp[i][0] = a[i][0].second;
        for (int j = 1; j < sp[i].size(); j++)
            sp[i][j] = sp[i][j - 1] + a[i][j].second;
    }
    for (int i = 1; i <= n; i++)
    {
        int szz = poz[i].size();
        dp0[i].resize(szz);
        dp1[i].resize(szz);
        mp_dp0[i].resize(szz);
        mp_dp1[i].resize(szz);
        ms_dp0[i].resize(szz);
        ms_dp1[i].resize(szz);
        lol1[i].resize(szz);
        lol2[i].resize(szz);
    }
    ///pentru coloana 1, dp-urile sunt 0, doar dau build care imi face chestiile auxiliare
    //cout << 1 << endl;
    build(1);
    //cout << 2 << endl;
    int ans = 0;
    for (int i = 2; i <= n; i++)
    {
        //cout << f(1,2) << ' ' << lol1[1][0] << endl;
        for (int j = 0; j < poz[i].size(); j++)
        {
            int hpmax = max_idx(i - 1,poz[i][j]);
            if (hpmax != -1)
            {
                dp0[i][j] = max(dp0[i][j],mp_dp0[i - 1][hpmax]);
                dp0[i][j] = max(dp0[i][j],f(i - 1,poz[i][j]) + lol1[i - 1][hpmax]);
            }
            hpmax++;
            if (hpmax != poz[i - 1].size())
                dp0[i][j] = max(dp0[i][j],lol2[i - 1][hpmax] - f(i,poz[i][j]));
        }
        for (int j = 0; j < poz[i].size(); j++)
        {
            int hpmax = max_idx(i - 1,poz[i][j]);
            if (hpmax != -1)
            {
                dp1[i][j] = max(dp1[i][j],mp_dp0[i - 1][hpmax]);
                dp1[i][j] = max(dp1[i][j],f(i - 1,poz[i][j]) + lol1[i - 1][hpmax]);
            }
            hpmax++;
            if (hpmax != poz[i - 1].size())
                dp1[i][j] = max(dp1[i][j],max(ms_dp0[i - 1][hpmax],ms_dp1[i - 1][hpmax]));
        }
        for (int j = 0; j < poz[i].size(); j++)
            ans = max(ans,max(dp0[i][j],dp1[i][j]));
        if (i != n)
            build(i);
    }
    return ans;
}

/**
Daca am pus pana la i, si vreau sa pun la i + 1, am pretty much cateva cazuri:
-daca pun mai jos decat era la i, atunci nu mi se vor mai aduna alea pana unde am pus la i+1
-daca pun mai sus decat era la i, iar am doua cazuri:
-daca pun mai jos decat era la i - 1, nu se intampla mare lucru
-daca pun mai sus decat era la i - 1, e ca si cum as lua toti pestii de la h[i] la h[i + 1], dar sa nu ii fi considerat la i - 1 ofc

In dp-ul curent (pana la i) consider doar ce iau de la 1 la i (eventual, fara i deoarece sunt pe ultimul caz si iau cu urmatorul)

dp0[i][h] = costul de pana la i, daca consider si i ca fiind full pus, si i are inaltimea h (relativ la el)
dp1[i][h] = costul de pana la i, dar cand nu consider i, pentru ca ma bazez ca o sa imi vina i + 1 mai mare

dp0[i][h] vine din doua cazuri:
-daca h' (inaltimea lui i - 1) <= h atunci am dp0[i - 1][h'] si dp1[i - 1][h'] + f(i - 1,h) - f(i - 1,h')
Retin maxime pe prefix din dp0[i][h] si maxime pe prefix din dp1[i][h] - f(i,h)
-daca h' > h, atunci am dp0[i - 1][h'] + f(i,h') - f(i,h) si dp1[i - 1][h'] + f(i,h') - f(i,h)
Retin maxime pe sufix din dp0[i][h] + f(i + 1,h) si maxime pe sufix din dp1[i][h] + f(i + 1,h)

dp1[i][h] vine si el din doua cazuri:
-daca h' <= h atunci am dp0[i - 1][h'] si dp1[i - 1][h'] + f(i - 1,h) - f(i - 1,h')
Cazul e identic cu primul caz de la dp0[i][h]
-daca h' > h, doar am dp0[i - 1][h'] si dp1[i - 1][h']
Retin maxime pe sufix din dp0[i][h] si din dp1[i][h]

h-urile relevante pentru col i sunt (linia unui peste de pe i) - 1 si n, deci total vor fi O(N + M) stari
Sortez si caut binar chestii deci am total O((N + M)log(N + M))
**/

Compilation message

fish.cpp: In function 'long long int max_idx(long long int, long long int)':
fish.cpp:24:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |         if (st + pas < poz[col].size() and poz[col][st + pas] <= h)
      |             ~~~~~~~~~^~~~~~~~~~~~~~~~~
fish.cpp: In function 'long long int max_idx_2(long long int, long long int)':
fish.cpp:36:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |         if (st + pas < a[col].size() and a[col][st + pas].first <= h)
      |             ~~~~~~~~~^~~~~~~~~~~~~~~
fish.cpp: In function 'void build(long long int)':
fish.cpp:53:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |     for (int i = 0; i < poz[col].size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~
fish.cpp:68:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |         if (i + 1 == poz[col].size())
      |             ~~~~~~^~~~~~~~~~~~~~~~~~
fish.cpp:79:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |     for (int i = 0; i < poz[col].size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~
fish.cpp:90:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |         if (i + 1 == poz[col].size())
      |             ~~~~~~^~~~~~~~~~~~~~~~~~
fish.cpp: In function 'long long int max_weights(integer, integer, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:129:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  129 |         for (int j = 1; j < sp[i].size(); j++)
      |                         ~~^~~~~~~~~~~~~~
fish.cpp:152:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  152 |         for (int j = 0; j < poz[i].size(); j++)
      |                         ~~^~~~~~~~~~~~~~~
fish.cpp:161:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  161 |             if (hpmax != poz[i - 1].size())
      |                 ~~~~~~^~~~~~~~~~~~~~~~~~~~
fish.cpp:164:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  164 |         for (int j = 0; j < poz[i].size(); j++)
      |                         ~~^~~~~~~~~~~~~~~
fish.cpp:173:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  173 |             if (hpmax != poz[i - 1].size())
      |                 ~~~~~~^~~~~~~~~~~~~~~~~~~~
fish.cpp:176:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  176 |         for (int j = 0; j < poz[i].size(); j++)
      |                         ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 81 ms 60868 KB Output is correct
2 Correct 95 ms 66056 KB Output is correct
3 Correct 50 ms 54420 KB Output is correct
4 Correct 49 ms 54364 KB Output is correct
5 Correct 198 ms 90008 KB Output is correct
6 Correct 247 ms 88656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 26200 KB Output is correct
2 Correct 131 ms 70960 KB Output is correct
3 Correct 160 ms 77748 KB Output is correct
4 Correct 81 ms 60864 KB Output is correct
5 Correct 94 ms 65988 KB Output is correct
6 Correct 4 ms 26204 KB Output is correct
7 Correct 5 ms 26204 KB Output is correct
8 Correct 4 ms 26200 KB Output is correct
9 Correct 4 ms 26204 KB Output is correct
10 Correct 54 ms 54356 KB Output is correct
11 Correct 51 ms 54428 KB Output is correct
12 Correct 96 ms 62444 KB Output is correct
13 Correct 107 ms 67780 KB Output is correct
14 Correct 109 ms 62484 KB Output is correct
15 Correct 111 ms 66752 KB Output is correct
16 Correct 87 ms 62656 KB Output is correct
17 Correct 101 ms 66484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 54604 KB Output is correct
2 Correct 54 ms 54364 KB Output is correct
3 Correct 79 ms 56140 KB Output is correct
4 Correct 73 ms 57940 KB Output is correct
5 Correct 117 ms 64596 KB Output is correct
6 Correct 114 ms 63828 KB Output is correct
7 Correct 111 ms 64596 KB Output is correct
8 Correct 141 ms 64592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 26200 KB Output is correct
2 Correct 4 ms 26204 KB Output is correct
3 Correct 4 ms 26204 KB Output is correct
4 Correct 4 ms 26204 KB Output is correct
5 Correct 5 ms 26204 KB Output is correct
6 Correct 4 ms 26204 KB Output is correct
7 Correct 6 ms 26204 KB Output is correct
8 Correct 4 ms 26268 KB Output is correct
9 Correct 6 ms 26200 KB Output is correct
10 Correct 6 ms 26460 KB Output is correct
11 Correct 5 ms 26204 KB Output is correct
12 Correct 6 ms 26460 KB Output is correct
13 Correct 4 ms 26204 KB Output is correct
14 Correct 5 ms 26328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 26200 KB Output is correct
2 Correct 4 ms 26204 KB Output is correct
3 Correct 4 ms 26204 KB Output is correct
4 Correct 4 ms 26204 KB Output is correct
5 Correct 5 ms 26204 KB Output is correct
6 Correct 4 ms 26204 KB Output is correct
7 Correct 6 ms 26204 KB Output is correct
8 Correct 4 ms 26268 KB Output is correct
9 Correct 6 ms 26200 KB Output is correct
10 Correct 6 ms 26460 KB Output is correct
11 Correct 5 ms 26204 KB Output is correct
12 Correct 6 ms 26460 KB Output is correct
13 Correct 4 ms 26204 KB Output is correct
14 Correct 5 ms 26328 KB Output is correct
15 Correct 5 ms 26200 KB Output is correct
16 Correct 6 ms 26760 KB Output is correct
17 Correct 24 ms 32604 KB Output is correct
18 Correct 24 ms 33104 KB Output is correct
19 Correct 24 ms 33116 KB Output is correct
20 Correct 24 ms 33116 KB Output is correct
21 Correct 23 ms 33116 KB Output is correct
22 Correct 45 ms 40064 KB Output is correct
23 Correct 8 ms 27484 KB Output is correct
24 Correct 17 ms 30404 KB Output is correct
25 Correct 5 ms 26460 KB Output is correct
26 Correct 8 ms 27484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 26200 KB Output is correct
2 Correct 4 ms 26204 KB Output is correct
3 Correct 4 ms 26204 KB Output is correct
4 Correct 4 ms 26204 KB Output is correct
5 Correct 5 ms 26204 KB Output is correct
6 Correct 4 ms 26204 KB Output is correct
7 Correct 6 ms 26204 KB Output is correct
8 Correct 4 ms 26268 KB Output is correct
9 Correct 6 ms 26200 KB Output is correct
10 Correct 6 ms 26460 KB Output is correct
11 Correct 5 ms 26204 KB Output is correct
12 Correct 6 ms 26460 KB Output is correct
13 Correct 4 ms 26204 KB Output is correct
14 Correct 5 ms 26328 KB Output is correct
15 Correct 5 ms 26200 KB Output is correct
16 Correct 6 ms 26760 KB Output is correct
17 Correct 24 ms 32604 KB Output is correct
18 Correct 24 ms 33104 KB Output is correct
19 Correct 24 ms 33116 KB Output is correct
20 Correct 24 ms 33116 KB Output is correct
21 Correct 23 ms 33116 KB Output is correct
22 Correct 45 ms 40064 KB Output is correct
23 Correct 8 ms 27484 KB Output is correct
24 Correct 17 ms 30404 KB Output is correct
25 Correct 5 ms 26460 KB Output is correct
26 Correct 8 ms 27484 KB Output is correct
27 Correct 7 ms 27224 KB Output is correct
28 Correct 105 ms 58708 KB Output is correct
29 Correct 148 ms 70344 KB Output is correct
30 Correct 139 ms 69724 KB Output is correct
31 Correct 141 ms 69716 KB Output is correct
32 Correct 141 ms 72020 KB Output is correct
33 Correct 138 ms 69844 KB Output is correct
34 Correct 137 ms 69848 KB Output is correct
35 Correct 61 ms 44092 KB Output is correct
36 Correct 134 ms 71884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 54604 KB Output is correct
2 Correct 54 ms 54364 KB Output is correct
3 Correct 79 ms 56140 KB Output is correct
4 Correct 73 ms 57940 KB Output is correct
5 Correct 117 ms 64596 KB Output is correct
6 Correct 114 ms 63828 KB Output is correct
7 Correct 111 ms 64596 KB Output is correct
8 Correct 141 ms 64592 KB Output is correct
9 Correct 106 ms 64340 KB Output is correct
10 Correct 80 ms 49336 KB Output is correct
11 Correct 167 ms 72528 KB Output is correct
12 Correct 4 ms 26200 KB Output is correct
13 Correct 4 ms 26204 KB Output is correct
14 Correct 4 ms 26056 KB Output is correct
15 Correct 4 ms 26204 KB Output is correct
16 Correct 5 ms 26204 KB Output is correct
17 Correct 4 ms 26204 KB Output is correct
18 Correct 51 ms 54432 KB Output is correct
19 Correct 50 ms 54432 KB Output is correct
20 Correct 49 ms 54352 KB Output is correct
21 Correct 49 ms 54360 KB Output is correct
22 Correct 106 ms 64084 KB Output is correct
23 Correct 148 ms 72684 KB Output is correct
24 Correct 156 ms 73044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 81 ms 60868 KB Output is correct
2 Correct 95 ms 66056 KB Output is correct
3 Correct 50 ms 54420 KB Output is correct
4 Correct 49 ms 54364 KB Output is correct
5 Correct 198 ms 90008 KB Output is correct
6 Correct 247 ms 88656 KB Output is correct
7 Correct 4 ms 26200 KB Output is correct
8 Correct 131 ms 70960 KB Output is correct
9 Correct 160 ms 77748 KB Output is correct
10 Correct 81 ms 60864 KB Output is correct
11 Correct 94 ms 65988 KB Output is correct
12 Correct 4 ms 26204 KB Output is correct
13 Correct 5 ms 26204 KB Output is correct
14 Correct 4 ms 26200 KB Output is correct
15 Correct 4 ms 26204 KB Output is correct
16 Correct 54 ms 54356 KB Output is correct
17 Correct 51 ms 54428 KB Output is correct
18 Correct 96 ms 62444 KB Output is correct
19 Correct 107 ms 67780 KB Output is correct
20 Correct 109 ms 62484 KB Output is correct
21 Correct 111 ms 66752 KB Output is correct
22 Correct 87 ms 62656 KB Output is correct
23 Correct 101 ms 66484 KB Output is correct
24 Correct 49 ms 54604 KB Output is correct
25 Correct 54 ms 54364 KB Output is correct
26 Correct 79 ms 56140 KB Output is correct
27 Correct 73 ms 57940 KB Output is correct
28 Correct 117 ms 64596 KB Output is correct
29 Correct 114 ms 63828 KB Output is correct
30 Correct 111 ms 64596 KB Output is correct
31 Correct 141 ms 64592 KB Output is correct
32 Correct 4 ms 26200 KB Output is correct
33 Correct 4 ms 26204 KB Output is correct
34 Correct 4 ms 26204 KB Output is correct
35 Correct 4 ms 26204 KB Output is correct
36 Correct 5 ms 26204 KB Output is correct
37 Correct 4 ms 26204 KB Output is correct
38 Correct 6 ms 26204 KB Output is correct
39 Correct 4 ms 26268 KB Output is correct
40 Correct 6 ms 26200 KB Output is correct
41 Correct 6 ms 26460 KB Output is correct
42 Correct 5 ms 26204 KB Output is correct
43 Correct 6 ms 26460 KB Output is correct
44 Correct 4 ms 26204 KB Output is correct
45 Correct 5 ms 26328 KB Output is correct
46 Correct 5 ms 26200 KB Output is correct
47 Correct 6 ms 26760 KB Output is correct
48 Correct 24 ms 32604 KB Output is correct
49 Correct 24 ms 33104 KB Output is correct
50 Correct 24 ms 33116 KB Output is correct
51 Correct 24 ms 33116 KB Output is correct
52 Correct 23 ms 33116 KB Output is correct
53 Correct 45 ms 40064 KB Output is correct
54 Correct 8 ms 27484 KB Output is correct
55 Correct 17 ms 30404 KB Output is correct
56 Correct 5 ms 26460 KB Output is correct
57 Correct 8 ms 27484 KB Output is correct
58 Correct 7 ms 27224 KB Output is correct
59 Correct 105 ms 58708 KB Output is correct
60 Correct 148 ms 70344 KB Output is correct
61 Correct 139 ms 69724 KB Output is correct
62 Correct 141 ms 69716 KB Output is correct
63 Correct 141 ms 72020 KB Output is correct
64 Correct 138 ms 69844 KB Output is correct
65 Correct 137 ms 69848 KB Output is correct
66 Correct 61 ms 44092 KB Output is correct
67 Correct 134 ms 71884 KB Output is correct
68 Correct 106 ms 64340 KB Output is correct
69 Correct 80 ms 49336 KB Output is correct
70 Correct 167 ms 72528 KB Output is correct
71 Correct 4 ms 26200 KB Output is correct
72 Correct 4 ms 26204 KB Output is correct
73 Correct 4 ms 26056 KB Output is correct
74 Correct 4 ms 26204 KB Output is correct
75 Correct 5 ms 26204 KB Output is correct
76 Correct 4 ms 26204 KB Output is correct
77 Correct 51 ms 54432 KB Output is correct
78 Correct 50 ms 54432 KB Output is correct
79 Correct 49 ms 54352 KB Output is correct
80 Correct 49 ms 54360 KB Output is correct
81 Correct 106 ms 64084 KB Output is correct
82 Correct 148 ms 72684 KB Output is correct
83 Correct 156 ms 73044 KB Output is correct
84 Correct 225 ms 93888 KB Output is correct
85 Correct 221 ms 96004 KB Output is correct
86 Correct 262 ms 88656 KB Output is correct
87 Correct 270 ms 91228 KB Output is correct
88 Correct 5 ms 26204 KB Output is correct
89 Correct 290 ms 91216 KB Output is correct
90 Correct 216 ms 94032 KB Output is correct
91 Correct 198 ms 95568 KB Output is correct