답안 #705236

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
705236 2023-03-03T16:36:25 Z danikoynov 메기 농장 (IOI22_fish) C++17
100 / 100
712 ms 124056 KB
#include "fish.h"

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxn = 1e5 + 10, smalln = 3010;

vector < pair < int, int > > col[maxn];
vector < int > state[maxn];
 unordered_set < int >  state_set[maxn];
vector < vector < ll > > dp[maxn];

vector < ll > pref[maxn];

ll get_sum(int j, int left, int right)
{
    if (left > right)
        return 0;

    int lf = 0, rf = (int)(col[j].size()) - 1;
    while(lf <= rf)
    {
        int mf = (lf + rf) / 2;
        if (col[j][mf].first >= left)
            rf = mf - 1;
        else
            lf = mf + 1;
    }

    int lb = rf;
    lf = 0;
    rf = (int)(col[j].size()) - 1;
    while(lf <= rf)
    {
        int mf = (lf + rf) / 2;
        ///cout << col[j][mf].second << endl;
        if (col[j][mf].first <= right)
            lf = mf + 1;
        else
            rf = mf - 1;
    }
    int rb = rf;
   /// cout << lb << " " << rb << " " << j << " " << left << " " << right << endl;
    if (lb >= rb || rb < 0)
        return 0;

    ll sum = pref[j][rb];
    if (lb >= 0)
        sum = sum - pref[j][lb];
       /// cout << sum << endl;
    return sum;
}

long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y,
                      std::vector<int> W)
{
    bool all_even = true, small_x = true;
    for (int i = 0; i < M; i ++)
    {
        if (X[i] % 2 != 0)
            all_even = false;
        if (X[i] > 1)
            small_x = false;
    }

    for (int i = 0; i < M; i ++)
    {
        col[X[i]].push_back({Y[i], W[i]});
    }

    for (int i = 0; i < N; i ++)
        sort(col[i].begin(), col[i].end());

    for (int i = 0; i < M; i ++)
    {
        if (X[i] != 0)
            state_set[X[i] - 1].insert(Y[i] + 1);
        if (X[i] != N - 1)
        state_set[X[i] + 1].insert(Y[i] + 1);
    }

    for (int i = 0; i < N; i ++)
    {
        state_set[i].insert(0);
        state_set[i].insert(N);
    }
    for (int i = 0; i < N; i ++)
    {
        for (auto it : state_set[i])
            state[i].push_back(it);
        sort(state[i].begin(), state[i].end());
    }

    if (all_even)
    {
        ll sum = 0;
        for (int i = 0; i < M; i ++)
            sum = sum + W[i];
        return sum;

    }
    else if (small_x)
    {

        ll zero = 0, one = 0;
        for (int i = 0; i < M; i ++)
        {
            if (X[i] == 0)
                zero += W[i];
            else
                one += W[i];
        }
        if (N == 2)
            return max(zero, one);

        int to = 0;
        ll cur = 0, ans = 0;
        for (int i = 0; i < col[0].size(); i ++)
        {
            if (cur + one > ans)
                ans = cur + one;
            cur += col[0][i].second;
            while(to < col[1].size() && col[1][to].first <= col[0][i].first)
            {
                one -= col[1][to].second;
                to ++;
            }
        }
        if (cur + one > ans)
            ans = cur + one;
        return ans;

    }



    /**for (int i = 0; i < N; i ++, cout << endl)
        for (int j = 0; j < N; j ++)
        cout << val[i][j] << " ";*/


    for (int i = 0; i < N; i ++)
    {
        pref[i].resize(col[i].size());
        if (col[i].size() == 0)
            continue;
        pref[i][0] = col[i][0].second;
        ///cout << col[i][0].second << endl;
        for (int j = 1; j < col[i].size(); j ++)
        {
            pref[i][j] = pref[i][j - 1] + col[i][j].second;
        }
    }

    for (int i = 0; i < N; i ++)
    {
        dp[i].resize(state[i].size());
        for (int j = 0; j < state[i].size(); j ++)
        {
            dp[i][j].resize(2);
        }
    }

    for (int j = 0; j < state[1].size(); j ++)
        dp[1][j][0] = dp[1][j][1] = 0;
    for (int i = 1; i < N; i ++)
    {

        ll so_far = 0, to = 0;
        for (int j = 0; j < state[i].size(); j ++)
        {
            if (j > 0)
                dp[i][j][1] = dp[i][j - 1][1] + get_sum(i - 1, state[i][j - 1], state[i][j] - 1);
            while(to < state[i - 1].size() && state[i - 1][to] <= state[i][j])
            {
                ///cout << i << " " << j << " :: " << state[i - 1][to] << " " << state[i][j] << endl;
                so_far = max(so_far, dp[i - 1][to][1] + get_sum(i - 1, state[i - 1][to], state[i][j] - 1));
                to ++;
            }
            dp[i][j][1] = max(dp[i][j][1], so_far);
        }

        so_far = 0;
        to = (int)(state[i - 1].size()) - 1;
        for (int j = (int)(state[i].size()) - 1; j >= 0; j --)
        {
            if (j + 1 < state[i].size())
                dp[i][j][0] = dp[i][j + 1][0] + get_sum(i, state[i][j], state[i][j + 1] - 1);
            while(to >= 0 && state[i - 1][to] >= state[i][j])
            {
                ///cout << i << " " << j << " ::: " << to << endl;
                so_far = max(so_far, max(dp[i - 1][to][0], dp[i - 1][to][1]) + get_sum(i, state[i][j], state[i - 1][to] - 1));
                to --;
            }
            dp[i][j][0] = max(dp[i][j][0], so_far);
        }


        if (i > 1)
        {

            so_far = 0;
            to = 0;
            for (int j = 0; j < state[i].size(); j ++)
            {
                while(to < state[i - 2].size() && state[i - 2][to] <= state[i][j])
                {
                    so_far = max(so_far, max(dp[i - 2][to][0], dp[i - 2][to][1]));
                    to ++;
                }
                dp[i][j][1] = max(so_far + get_sum(i - 1, 0, state[i][j] - 1), dp[i][j][1]);
            }
            so_far = 0;
            to = (int)(state[i - 2].size()) - 1;
            for (int j = (int)(state[i].size()) - 1; j >= 0; j --)
            {
                while(to >= 0 && state[i - 2][to] >= state[i][j])
                {
                    so_far = max(so_far, max(dp[i - 2][to][0], dp[i - 2][to][1]) + get_sum(i - 1, 0, state[i - 2][to] - 1));
                    to --;
                }
                dp[i][j][1] = max(so_far, dp[i][j][1]);
            }
        }
    }

    /**cout << "-----------" << endl;
    for (int i = 0; i < N; i ++, cout << endl)
        for (int j = 0; j < state[i].size(); j ++)
        cout << dp[i][j][0] << " ";
    cout << "-----------" << endl;
    for (int i = 0; i < N; i ++, cout << endl)
        for (int j = 0; j < state[i].size(); j ++)
        cout << dp[i][j][1] << " ";*/
    ll ans = 0;
    for (int j = 0; j < state[N - 1].size(); j ++)
        for (int t = 0; t < 2; t ++)
            ans = max(ans, dp[N - 1][j][t]);


    return ans;

}

Compilation message

fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:119:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  119 |         for (int i = 0; i < col[0].size(); i ++)
      |                         ~~^~~~~~~~~~~~~~~
fish.cpp:124:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  124 |             while(to < col[1].size() && col[1][to].first <= col[0][i].first)
      |                   ~~~^~~~~~~~~~~~~~~
fish.cpp:150:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  150 |         for (int j = 1; j < col[i].size(); j ++)
      |                         ~~^~~~~~~~~~~~~~~
fish.cpp:159:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  159 |         for (int j = 0; j < state[i].size(); j ++)
      |                         ~~^~~~~~~~~~~~~~~~~
fish.cpp:165:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  165 |     for (int j = 0; j < state[1].size(); j ++)
      |                     ~~^~~~~~~~~~~~~~~~~
fish.cpp:171:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  171 |         for (int j = 0; j < state[i].size(); j ++)
      |                         ~~^~~~~~~~~~~~~~~~~
fish.cpp:175:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  175 |             while(to < state[i - 1].size() && state[i - 1][to] <= state[i][j])
      |                   ~~~^~~~~~~~~~~~~~~~~~~~~
fish.cpp:188:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  188 |             if (j + 1 < state[i].size())
      |                 ~~~~~~^~~~~~~~~~~~~~~~~
fish.cpp:205:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  205 |             for (int j = 0; j < state[i].size(); j ++)
      |                             ~~^~~~~~~~~~~~~~~~~
fish.cpp:207:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  207 |                 while(to < state[i - 2].size() && state[i - 2][to] <= state[i][j])
      |                       ~~~^~~~~~~~~~~~~~~~~~~~~
fish.cpp:237:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  237 |     for (int j = 0; j < state[N - 1].size(); j ++)
      |                     ~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 79 ms 39412 KB Output is correct
2 Correct 98 ms 43584 KB Output is correct
3 Correct 35 ms 35408 KB Output is correct
4 Correct 35 ms 35504 KB Output is correct
5 Correct 430 ms 75544 KB Output is correct
6 Correct 490 ms 72032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 15060 KB Output is correct
2 Correct 151 ms 49044 KB Output is correct
3 Correct 196 ms 56444 KB Output is correct
4 Correct 78 ms 39468 KB Output is correct
5 Correct 96 ms 43520 KB Output is correct
6 Correct 8 ms 15060 KB Output is correct
7 Correct 9 ms 15060 KB Output is correct
8 Correct 7 ms 15060 KB Output is correct
9 Correct 8 ms 15092 KB Output is correct
10 Correct 34 ms 35464 KB Output is correct
11 Correct 35 ms 35492 KB Output is correct
12 Correct 95 ms 43012 KB Output is correct
13 Correct 118 ms 48392 KB Output is correct
14 Correct 93 ms 41432 KB Output is correct
15 Correct 101 ms 45064 KB Output is correct
16 Correct 88 ms 41312 KB Output is correct
17 Correct 102 ms 45040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 35392 KB Output is correct
2 Correct 60 ms 47924 KB Output is correct
3 Correct 105 ms 54776 KB Output is correct
4 Correct 93 ms 54956 KB Output is correct
5 Correct 159 ms 66240 KB Output is correct
6 Correct 151 ms 65340 KB Output is correct
7 Correct 160 ms 66216 KB Output is correct
8 Correct 155 ms 66004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 15060 KB Output is correct
2 Correct 8 ms 15060 KB Output is correct
3 Correct 8 ms 15060 KB Output is correct
4 Correct 8 ms 15060 KB Output is correct
5 Correct 8 ms 15060 KB Output is correct
6 Correct 8 ms 15060 KB Output is correct
7 Correct 8 ms 15060 KB Output is correct
8 Correct 8 ms 15060 KB Output is correct
9 Correct 8 ms 15188 KB Output is correct
10 Correct 10 ms 15556 KB Output is correct
11 Correct 9 ms 15356 KB Output is correct
12 Correct 10 ms 15440 KB Output is correct
13 Correct 9 ms 15188 KB Output is correct
14 Correct 9 ms 15316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 15060 KB Output is correct
2 Correct 8 ms 15060 KB Output is correct
3 Correct 8 ms 15060 KB Output is correct
4 Correct 8 ms 15060 KB Output is correct
5 Correct 8 ms 15060 KB Output is correct
6 Correct 8 ms 15060 KB Output is correct
7 Correct 8 ms 15060 KB Output is correct
8 Correct 8 ms 15060 KB Output is correct
9 Correct 8 ms 15188 KB Output is correct
10 Correct 10 ms 15556 KB Output is correct
11 Correct 9 ms 15356 KB Output is correct
12 Correct 10 ms 15440 KB Output is correct
13 Correct 9 ms 15188 KB Output is correct
14 Correct 9 ms 15316 KB Output is correct
15 Correct 8 ms 15316 KB Output is correct
16 Correct 10 ms 15540 KB Output is correct
17 Correct 52 ms 23252 KB Output is correct
18 Correct 47 ms 22640 KB Output is correct
19 Correct 48 ms 22912 KB Output is correct
20 Correct 43 ms 22072 KB Output is correct
21 Correct 42 ms 21980 KB Output is correct
22 Correct 84 ms 28784 KB Output is correct
23 Correct 20 ms 17364 KB Output is correct
24 Correct 46 ms 21588 KB Output is correct
25 Correct 9 ms 15444 KB Output is correct
26 Correct 18 ms 17132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 15060 KB Output is correct
2 Correct 8 ms 15060 KB Output is correct
3 Correct 8 ms 15060 KB Output is correct
4 Correct 8 ms 15060 KB Output is correct
5 Correct 8 ms 15060 KB Output is correct
6 Correct 8 ms 15060 KB Output is correct
7 Correct 8 ms 15060 KB Output is correct
8 Correct 8 ms 15060 KB Output is correct
9 Correct 8 ms 15188 KB Output is correct
10 Correct 10 ms 15556 KB Output is correct
11 Correct 9 ms 15356 KB Output is correct
12 Correct 10 ms 15440 KB Output is correct
13 Correct 9 ms 15188 KB Output is correct
14 Correct 9 ms 15316 KB Output is correct
15 Correct 8 ms 15316 KB Output is correct
16 Correct 10 ms 15540 KB Output is correct
17 Correct 52 ms 23252 KB Output is correct
18 Correct 47 ms 22640 KB Output is correct
19 Correct 48 ms 22912 KB Output is correct
20 Correct 43 ms 22072 KB Output is correct
21 Correct 42 ms 21980 KB Output is correct
22 Correct 84 ms 28784 KB Output is correct
23 Correct 20 ms 17364 KB Output is correct
24 Correct 46 ms 21588 KB Output is correct
25 Correct 9 ms 15444 KB Output is correct
26 Correct 18 ms 17132 KB Output is correct
27 Correct 13 ms 16824 KB Output is correct
28 Correct 249 ms 52148 KB Output is correct
29 Correct 456 ms 77568 KB Output is correct
30 Correct 591 ms 87700 KB Output is correct
31 Correct 602 ms 87404 KB Output is correct
32 Correct 306 ms 61716 KB Output is correct
33 Correct 617 ms 88200 KB Output is correct
34 Correct 603 ms 88204 KB Output is correct
35 Correct 165 ms 41608 KB Output is correct
36 Correct 555 ms 82948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 35392 KB Output is correct
2 Correct 60 ms 47924 KB Output is correct
3 Correct 105 ms 54776 KB Output is correct
4 Correct 93 ms 54956 KB Output is correct
5 Correct 159 ms 66240 KB Output is correct
6 Correct 151 ms 65340 KB Output is correct
7 Correct 160 ms 66216 KB Output is correct
8 Correct 155 ms 66004 KB Output is correct
9 Correct 189 ms 75240 KB Output is correct
10 Correct 120 ms 47392 KB Output is correct
11 Correct 251 ms 79596 KB Output is correct
12 Correct 9 ms 15060 KB Output is correct
13 Correct 8 ms 15060 KB Output is correct
14 Correct 8 ms 15084 KB Output is correct
15 Correct 9 ms 15080 KB Output is correct
16 Correct 9 ms 15120 KB Output is correct
17 Correct 8 ms 15180 KB Output is correct
18 Correct 36 ms 35396 KB Output is correct
19 Correct 34 ms 35444 KB Output is correct
20 Correct 60 ms 48112 KB Output is correct
21 Correct 57 ms 47948 KB Output is correct
22 Correct 208 ms 73940 KB Output is correct
23 Correct 334 ms 93264 KB Output is correct
24 Correct 347 ms 96716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 79 ms 39412 KB Output is correct
2 Correct 98 ms 43584 KB Output is correct
3 Correct 35 ms 35408 KB Output is correct
4 Correct 35 ms 35504 KB Output is correct
5 Correct 430 ms 75544 KB Output is correct
6 Correct 490 ms 72032 KB Output is correct
7 Correct 8 ms 15060 KB Output is correct
8 Correct 151 ms 49044 KB Output is correct
9 Correct 196 ms 56444 KB Output is correct
10 Correct 78 ms 39468 KB Output is correct
11 Correct 96 ms 43520 KB Output is correct
12 Correct 8 ms 15060 KB Output is correct
13 Correct 9 ms 15060 KB Output is correct
14 Correct 7 ms 15060 KB Output is correct
15 Correct 8 ms 15092 KB Output is correct
16 Correct 34 ms 35464 KB Output is correct
17 Correct 35 ms 35492 KB Output is correct
18 Correct 95 ms 43012 KB Output is correct
19 Correct 118 ms 48392 KB Output is correct
20 Correct 93 ms 41432 KB Output is correct
21 Correct 101 ms 45064 KB Output is correct
22 Correct 88 ms 41312 KB Output is correct
23 Correct 102 ms 45040 KB Output is correct
24 Correct 35 ms 35392 KB Output is correct
25 Correct 60 ms 47924 KB Output is correct
26 Correct 105 ms 54776 KB Output is correct
27 Correct 93 ms 54956 KB Output is correct
28 Correct 159 ms 66240 KB Output is correct
29 Correct 151 ms 65340 KB Output is correct
30 Correct 160 ms 66216 KB Output is correct
31 Correct 155 ms 66004 KB Output is correct
32 Correct 8 ms 15060 KB Output is correct
33 Correct 8 ms 15060 KB Output is correct
34 Correct 8 ms 15060 KB Output is correct
35 Correct 8 ms 15060 KB Output is correct
36 Correct 8 ms 15060 KB Output is correct
37 Correct 8 ms 15060 KB Output is correct
38 Correct 8 ms 15060 KB Output is correct
39 Correct 8 ms 15060 KB Output is correct
40 Correct 8 ms 15188 KB Output is correct
41 Correct 10 ms 15556 KB Output is correct
42 Correct 9 ms 15356 KB Output is correct
43 Correct 10 ms 15440 KB Output is correct
44 Correct 9 ms 15188 KB Output is correct
45 Correct 9 ms 15316 KB Output is correct
46 Correct 8 ms 15316 KB Output is correct
47 Correct 10 ms 15540 KB Output is correct
48 Correct 52 ms 23252 KB Output is correct
49 Correct 47 ms 22640 KB Output is correct
50 Correct 48 ms 22912 KB Output is correct
51 Correct 43 ms 22072 KB Output is correct
52 Correct 42 ms 21980 KB Output is correct
53 Correct 84 ms 28784 KB Output is correct
54 Correct 20 ms 17364 KB Output is correct
55 Correct 46 ms 21588 KB Output is correct
56 Correct 9 ms 15444 KB Output is correct
57 Correct 18 ms 17132 KB Output is correct
58 Correct 13 ms 16824 KB Output is correct
59 Correct 249 ms 52148 KB Output is correct
60 Correct 456 ms 77568 KB Output is correct
61 Correct 591 ms 87700 KB Output is correct
62 Correct 602 ms 87404 KB Output is correct
63 Correct 306 ms 61716 KB Output is correct
64 Correct 617 ms 88200 KB Output is correct
65 Correct 603 ms 88204 KB Output is correct
66 Correct 165 ms 41608 KB Output is correct
67 Correct 555 ms 82948 KB Output is correct
68 Correct 189 ms 75240 KB Output is correct
69 Correct 120 ms 47392 KB Output is correct
70 Correct 251 ms 79596 KB Output is correct
71 Correct 9 ms 15060 KB Output is correct
72 Correct 8 ms 15060 KB Output is correct
73 Correct 8 ms 15084 KB Output is correct
74 Correct 9 ms 15080 KB Output is correct
75 Correct 9 ms 15120 KB Output is correct
76 Correct 8 ms 15180 KB Output is correct
77 Correct 36 ms 35396 KB Output is correct
78 Correct 34 ms 35444 KB Output is correct
79 Correct 60 ms 48112 KB Output is correct
80 Correct 57 ms 47948 KB Output is correct
81 Correct 208 ms 73940 KB Output is correct
82 Correct 334 ms 93264 KB Output is correct
83 Correct 347 ms 96716 KB Output is correct
84 Correct 705 ms 119772 KB Output is correct
85 Correct 712 ms 121540 KB Output is correct
86 Correct 570 ms 119844 KB Output is correct
87 Correct 561 ms 124056 KB Output is correct
88 Correct 7 ms 15060 KB Output is correct
89 Correct 571 ms 123904 KB Output is correct
90 Correct 590 ms 117068 KB Output is correct
91 Correct 436 ms 103500 KB Output is correct