답안 #705215

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
705215 2023-03-03T15:17:02 Z danikoynov 메기 농장 (IOI22_fish) C++17
61 / 100
708 ms 331192 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];
ll dp[smalln][smalln][2];
ll val[smalln][smalln], pref[smalln][smalln];

ll get_sum(int col, int left, int right)
{
    ll sum = pref[col][right];
    if (left > 0)
        sum = sum - pref[col][left - 1];
    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 < M; i ++)
        val[X[i]][Y[i]] = W[i];

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

    for (int i = 0; i < N; i ++)
    {
        pref[i][0] = val[i][0];
        for (int j = 1; j <= N; j ++)
        {
            pref[i][j] = pref[i][j - 1] + val[i][j];
        }
    }
    for (int i = 1; i < N; i ++)
    {

        dp[i][0][1] = dp[i - 1][0][1];
        for (int j = 1; j <= N; j ++)
        {
            dp[i][j][1] = max(dp[i][j - 1][1] + val[i - 1][j - 1], dp[i - 1][j][1]);
        }
        dp[i][N][0] = max(dp[i - 1][N][0], dp[i - 1][N][1]);
        for (int j = N - 1; j >= 0; j --)
        {
            dp[i][j][0] = max(dp[i][j + 1][0] + val[i][j], max(dp[i - 1][j][0], dp[i - 1][j][1]));
        }

        if (i > 1)
        {

            ll best = max(dp[i - 2][0][0], dp[i - 2][0][1]);
            for (int j = 1; j <= N; j ++)
            {
                dp[i][j][1] = max(best + get_sum(i - 1, 0, j - 1), dp[i][j][1]);
                best = max(best, max(dp[i - 2][j][0], dp[i - 2][j][1]));
            }
            best = max(dp[i - 2][N][0], dp[i - 2][N][1]) + get_sum(i - 1, 0, N - 1);
            for (int j = N; j > 0; j --)
            {
                dp[i][j][1] = max(best, dp[i][j][1]);
                best = max(best, max(dp[i - 2][j][0], dp[i - 2][j][1]) + get_sum(i - 1, 0, j - 1));
            }
        }

        for (int j = 0; j <= N; j ++)
        {
            if (state_set[i].find(j) == state_set[i].end())
                dp[i][j][0] = dp[i][j][1] = 0;
            //else
              // cout << i << " " << j << " " << dp[i][j][0] << endl;
        }
    }

    /**cout << "-----------" << endl;
    for (int i = 0; i < N; i ++, cout << endl)
        for (int j = 0; j <= N; j ++)
        cout << dp[i][j][0] << " ";
    cout << "-----------" << endl;
    for (int i = 0; i < N; i ++, cout << endl)
        for (int j = 0; j <= N; j ++)
        cout << dp[i][j][1] << " ";*/
    ll ans = 0;
    for (int j = 0; j <= N; 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:87: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]
   87 |         for (int i = 0; i < col[0].size(); i ++)
      |                         ~~^~~~~~~~~~~~~~~
fish.cpp:92: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]
   92 |             while(to < col[1].size() && col[1][to].first <= col[0][i].first)
      |                   ~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 81 ms 34836 KB Output is correct
2 Correct 99 ms 38852 KB Output is correct
3 Correct 36 ms 30784 KB Output is correct
4 Correct 36 ms 30724 KB Output is correct
5 Correct 428 ms 70772 KB Output is correct
6 Correct 500 ms 67308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 10452 KB Output is correct
2 Correct 151 ms 44424 KB Output is correct
3 Correct 194 ms 51812 KB Output is correct
4 Correct 82 ms 34748 KB Output is correct
5 Correct 102 ms 38820 KB Output is correct
6 Correct 7 ms 10432 KB Output is correct
7 Correct 5 ms 10384 KB Output is correct
8 Correct 6 ms 10452 KB Output is correct
9 Correct 5 ms 10452 KB Output is correct
10 Correct 37 ms 30764 KB Output is correct
11 Correct 37 ms 30776 KB Output is correct
12 Correct 94 ms 38360 KB Output is correct
13 Correct 126 ms 43708 KB Output is correct
14 Correct 95 ms 36640 KB Output is correct
15 Correct 107 ms 40440 KB Output is correct
16 Correct 103 ms 36780 KB Output is correct
17 Correct 105 ms 40388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 30768 KB Output is correct
2 Runtime error 62 ms 62368 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 10452 KB Output is correct
2 Correct 5 ms 10580 KB Output is correct
3 Correct 5 ms 10452 KB Output is correct
4 Correct 5 ms 10452 KB Output is correct
5 Correct 5 ms 10488 KB Output is correct
6 Correct 6 ms 10452 KB Output is correct
7 Correct 6 ms 10492 KB Output is correct
8 Correct 7 ms 10452 KB Output is correct
9 Correct 7 ms 12884 KB Output is correct
10 Correct 11 ms 16468 KB Output is correct
11 Correct 9 ms 12924 KB Output is correct
12 Correct 11 ms 16420 KB Output is correct
13 Correct 7 ms 11476 KB Output is correct
14 Correct 10 ms 16340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 10452 KB Output is correct
2 Correct 5 ms 10580 KB Output is correct
3 Correct 5 ms 10452 KB Output is correct
4 Correct 5 ms 10452 KB Output is correct
5 Correct 5 ms 10488 KB Output is correct
6 Correct 6 ms 10452 KB Output is correct
7 Correct 6 ms 10492 KB Output is correct
8 Correct 7 ms 10452 KB Output is correct
9 Correct 7 ms 12884 KB Output is correct
10 Correct 11 ms 16468 KB Output is correct
11 Correct 9 ms 12924 KB Output is correct
12 Correct 11 ms 16420 KB Output is correct
13 Correct 7 ms 11476 KB Output is correct
14 Correct 10 ms 16340 KB Output is correct
15 Correct 10 ms 16332 KB Output is correct
16 Correct 7 ms 11604 KB Output is correct
17 Correct 35 ms 20756 KB Output is correct
18 Correct 34 ms 20428 KB Output is correct
19 Correct 37 ms 21076 KB Output is correct
20 Correct 33 ms 20596 KB Output is correct
21 Correct 34 ms 20552 KB Output is correct
22 Correct 59 ms 24908 KB Output is correct
23 Correct 17 ms 18012 KB Output is correct
24 Correct 29 ms 20328 KB Output is correct
25 Correct 11 ms 16308 KB Output is correct
26 Correct 15 ms 17492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 10452 KB Output is correct
2 Correct 5 ms 10580 KB Output is correct
3 Correct 5 ms 10452 KB Output is correct
4 Correct 5 ms 10452 KB Output is correct
5 Correct 5 ms 10488 KB Output is correct
6 Correct 6 ms 10452 KB Output is correct
7 Correct 6 ms 10492 KB Output is correct
8 Correct 7 ms 10452 KB Output is correct
9 Correct 7 ms 12884 KB Output is correct
10 Correct 11 ms 16468 KB Output is correct
11 Correct 9 ms 12924 KB Output is correct
12 Correct 11 ms 16420 KB Output is correct
13 Correct 7 ms 11476 KB Output is correct
14 Correct 10 ms 16340 KB Output is correct
15 Correct 10 ms 16332 KB Output is correct
16 Correct 7 ms 11604 KB Output is correct
17 Correct 35 ms 20756 KB Output is correct
18 Correct 34 ms 20428 KB Output is correct
19 Correct 37 ms 21076 KB Output is correct
20 Correct 33 ms 20596 KB Output is correct
21 Correct 34 ms 20552 KB Output is correct
22 Correct 59 ms 24908 KB Output is correct
23 Correct 17 ms 18012 KB Output is correct
24 Correct 29 ms 20328 KB Output is correct
25 Correct 11 ms 16308 KB Output is correct
26 Correct 15 ms 17492 KB Output is correct
27 Correct 230 ms 235556 KB Output is correct
28 Correct 192 ms 54216 KB Output is correct
29 Correct 532 ms 260400 KB Output is correct
30 Correct 702 ms 306296 KB Output is correct
31 Correct 679 ms 306732 KB Output is correct
32 Correct 249 ms 54336 KB Output is correct
33 Correct 708 ms 331056 KB Output is correct
34 Correct 704 ms 331192 KB Output is correct
35 Correct 366 ms 251920 KB Output is correct
36 Correct 621 ms 274264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 30768 KB Output is correct
2 Runtime error 62 ms 62368 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 81 ms 34836 KB Output is correct
2 Correct 99 ms 38852 KB Output is correct
3 Correct 36 ms 30784 KB Output is correct
4 Correct 36 ms 30724 KB Output is correct
5 Correct 428 ms 70772 KB Output is correct
6 Correct 500 ms 67308 KB Output is correct
7 Correct 6 ms 10452 KB Output is correct
8 Correct 151 ms 44424 KB Output is correct
9 Correct 194 ms 51812 KB Output is correct
10 Correct 82 ms 34748 KB Output is correct
11 Correct 102 ms 38820 KB Output is correct
12 Correct 7 ms 10432 KB Output is correct
13 Correct 5 ms 10384 KB Output is correct
14 Correct 6 ms 10452 KB Output is correct
15 Correct 5 ms 10452 KB Output is correct
16 Correct 37 ms 30764 KB Output is correct
17 Correct 37 ms 30776 KB Output is correct
18 Correct 94 ms 38360 KB Output is correct
19 Correct 126 ms 43708 KB Output is correct
20 Correct 95 ms 36640 KB Output is correct
21 Correct 107 ms 40440 KB Output is correct
22 Correct 103 ms 36780 KB Output is correct
23 Correct 105 ms 40388 KB Output is correct
24 Correct 35 ms 30768 KB Output is correct
25 Runtime error 62 ms 62368 KB Execution killed with signal 11
26 Halted 0 ms 0 KB -