답안 #732712

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
732712 2023-04-29T07:49:17 Z math_rabbit_1028 메기 농장 (IOI22_fish) C++17
100 / 100
688 ms 89980 KB
#include "fish.h"
#include <bits/stdc++.h>
#include <vector>
using namespace std;

long long zero[101010], full[101010];
vector< pair<int, long long> > ylist[101010];
vector< long long > sum[101010], dp[2][101010];

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++) {
        X[i] += 1;
        Y[i] += 1;
        ylist[X[i]].push_back({Y[i], 0});
        ylist[X[i] - 1].push_back({Y[i], 0});
        ylist[X[i] + 1].push_back({Y[i], 0});
    }

    for (int i = 0; i <= N + 1; i++) {
        ylist[i].push_back({0, 0});
        ylist[i].push_back({N, 0});
        sort(ylist[i].begin(), ylist[i].end());
        ylist[i].erase(unique(ylist[i].begin(), ylist[i].end()), ylist[i].end());
    }

    for (int i = 0; i < M; i++) {
        int idx = lower_bound(ylist[X[i]].begin(), ylist[X[i]].end(), make_pair(Y[i], 0LL)) - ylist[X[i]].begin();
        ylist[X[i]][idx].second = W[i];
    }

    for (int i = 0; i <= N + 1; i++) {
        sum[i].push_back(0);
        for (int j = 1; j < ylist[i].size(); j++) {
            sum[i].push_back(sum[i].back() + ylist[i][j].second);
        }
    }

    for (int i = 0; i <= N + 1; i++) {
        for (int j = 0; j < ylist[i].size(); j++) dp[0][i].push_back(0);
        for (int j = 0; j < ylist[i].size(); j++) dp[1][i].push_back(0);
    }
    for (int j = 1; j < ylist[0].size() - 1; j++) dp[0][0][j] = -1e18;
    for (int j = 1; j < ylist[0].size() - 1; j++) dp[1][0][j] = -1e18;
    full[0] = -1e18;
    for (int i = 2; i <= N + 1; i++) {
        vector<int> all;
        for (int t = 0; t < ylist[i - 2].size(); t++) all.push_back(ylist[i - 2][t].first);
        for (int t = 0; t < ylist[i - 1].size(); t++) all.push_back(ylist[i - 1][t].first);
        for (int t = 0; t < ylist[i].size(); t++) all.push_back(ylist[i][t].first);
        sort(all.begin(), all.end());
        all.erase(unique(all.begin(), all.end()), all.end());

        int j = 0, k = 0, l = 0;
        for (int t = 1; t < all.size() - 1; t++) {
            while (ylist[i][j + 1] < make_pair(all[t], 0LL)) j++;
            while (ylist[i - 1][k + 1] < make_pair(all[t], 0LL)) k++;
            while (ylist[i - 2][l + 1] < make_pair(all[t], 0LL)) l++;
            zero[i] = max(zero[i], dp[1][i - 1][k] + sum[i][j]);
        }
        zero[i] = max(zero[i], zero[i - 1]);
        zero[i] = max(zero[i], full[i - 1] + sum[i][ylist[i].size() - 1]);

        j = 0, k = 0, l = 0;
        for (int t = 1; t < all.size() - 1; t++) {
            while (ylist[i][j + 1] < make_pair(all[t], 0LL)) j++;
            while (ylist[i - 1][k + 1] < make_pair(all[t], 0LL)) k++;
            while (ylist[i - 2][l + 1] < make_pair(all[t], 0LL)) l++;
            full[i] = max(full[i], dp[0][i - 1][k] + sum[i - 1][ylist[i - 1].size() - 1] - sum[i - 1][k]);
        }
        full[i] = max(full[i], full[i - 1]);
        full[i] = max(full[i], zero[i - 2] + sum[i - 1][ylist[i - 1].size() - 1]);
        full[i] = max(full[i], full[i - 2] + sum[i - 1][ylist[i - 1].size() - 1]);
        j = 0, k = 0, l = 0;
        for (int t = 1; t < all.size() - 1; t++) {
            while (ylist[i][j + 1] < make_pair(all[t], 0LL)) j++;
            while (ylist[i - 1][k + 1] < make_pair(all[t], 0LL)) k++;
            while (ylist[i - 2][l + 1] < make_pair(all[t], 0LL)) l++;
            full[i] = max(full[i], dp[1][i - 2][l] + sum[i - 1][ylist[i - 1].size() - 1]);
        }

        long long val0 = 0, val1 = 0;
        j = 0, k = 0, l = 0;
        for (int t = 1; t < all.size() - 1; t++) {
            while (ylist[i][j + 1] < make_pair(all[t], 0LL)) j++;
            while (ylist[i - 1][k + 1] < make_pair(all[t], 0LL)) k++;
            while (ylist[i - 2][l + 1] < make_pair(all[t], 0LL)) l++;
            val0 = max(val0, dp[0][i - 1][k] - sum[i - 1][k]);
            val1 = max(val1, dp[1][i - 1][k] + sum[i][j]);
        }
        j = 0, k = 0, l = 0;
        for (int t = 1; t < all.size() - 1; t++) {
            while (ylist[i][j + 1] < make_pair(all[t], 0LL)) j++;
            while (ylist[i - 1][k + 1] < make_pair(all[t], 0LL)) k++;
            while (ylist[i - 2][l + 1] < make_pair(all[t], 0LL)) l++;
            dp[0][i][j] = max(dp[0][i][j], val0 + sum[i - 1][k]);
            dp[1][i][j] = max(dp[1][i][j], val1 - sum[i][j]);
            dp[1][i][j] = max(dp[1][i][j], full[i - 1] + sum[i][ylist[i].size() - 1] - sum[i][j]);
            dp[0][i][j] = max(dp[0][i][j], zero[i - 2] + sum[i - 1][k]);
            dp[0][i][j] = max(dp[0][i][j], full[i - 2] + sum[i - 1][ylist[i - 1].size() - 1]);
        }
        long long val2 = 0;
        j = 0, k = 0, l = 0;
        for (int t = 1; t < all.size() - 1; t++) {
            while (ylist[i][j + 1] < make_pair(all[t], 0LL)) j++;
            while (ylist[i - 1][k + 1] < make_pair(all[t], 0LL)) k++;
            while (ylist[i - 2][l + 1] < make_pair(all[t], 0LL)) l++;
            val2 = max(val2, dp[1][i - 2][l]);
            dp[0][i][j] = max(dp[0][i][j], val2 + sum[i - 1][k]);
        }
        long long val3 = 0;
        j = ylist[i].size() - 1, k = ylist[i - 1].size() - 1, l = ylist[i - 2].size() - 1;
        for (int t = all.size() - 2; t >= 1; t--) {
            while (ylist[i][j] >= make_pair(all[t], 0LL)) j--;
            while (ylist[i - 1][k] >= make_pair(all[t], 0LL)) k--;
            while (ylist[i - 2][l] >= make_pair(all[t], 0LL)) l--;
            val3 = max(val3, dp[1][i - 2][l] + sum[i - 1][k]);
            dp[0][i][j] = max(dp[0][i][j], val3);
        }
    }
    long long res = 0;
    res = zero[N + 1];
    return res;
}

Compilation message

fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:33:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |         for (int j = 1; j < ylist[i].size(); j++) {
      |                         ~~^~~~~~~~~~~~~~~~~
fish.cpp:39:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |         for (int j = 0; j < ylist[i].size(); j++) dp[0][i].push_back(0);
      |                         ~~^~~~~~~~~~~~~~~~~
fish.cpp:40:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |         for (int j = 0; j < ylist[i].size(); j++) dp[1][i].push_back(0);
      |                         ~~^~~~~~~~~~~~~~~~~
fish.cpp:42:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |     for (int j = 1; j < ylist[0].size() - 1; j++) dp[0][0][j] = -1e18;
      |                     ~~^~~~~~~~~~~~~~~~~~~~~
fish.cpp:43:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |     for (int j = 1; j < ylist[0].size() - 1; j++) dp[1][0][j] = -1e18;
      |                     ~~^~~~~~~~~~~~~~~~~~~~~
fish.cpp:47:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |         for (int t = 0; t < ylist[i - 2].size(); t++) all.push_back(ylist[i - 2][t].first);
      |                         ~~^~~~~~~~~~~~~~~~~~~~~
fish.cpp:48:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |         for (int t = 0; t < ylist[i - 1].size(); t++) all.push_back(ylist[i - 1][t].first);
      |                         ~~^~~~~~~~~~~~~~~~~~~~~
fish.cpp:49:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |         for (int t = 0; t < ylist[i].size(); t++) all.push_back(ylist[i][t].first);
      |                         ~~^~~~~~~~~~~~~~~~~
fish.cpp:54:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |         for (int t = 1; t < all.size() - 1; t++) {
      |                         ~~^~~~~~~~~~~~~~~~
fish.cpp:64:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |         for (int t = 1; t < all.size() - 1; t++) {
      |                         ~~^~~~~~~~~~~~~~~~
fish.cpp:74:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |         for (int t = 1; t < all.size() - 1; t++) {
      |                         ~~^~~~~~~~~~~~~~~~
fish.cpp:83:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |         for (int t = 1; t < all.size() - 1; t++) {
      |                         ~~^~~~~~~~~~~~~~~~
fish.cpp:91:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |         for (int t = 1; t < all.size() - 1; t++) {
      |                         ~~^~~~~~~~~~~~~~~~
fish.cpp:103:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  103 |         for (int t = 1; t < all.size() - 1; t++) {
      |                         ~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 179 ms 36080 KB Output is correct
2 Correct 172 ms 41380 KB Output is correct
3 Correct 56 ms 25420 KB Output is correct
4 Correct 57 ms 25372 KB Output is correct
5 Correct 539 ms 71592 KB Output is correct
6 Correct 687 ms 83956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 241 ms 45412 KB Output is correct
3 Correct 307 ms 50916 KB Output is correct
4 Correct 148 ms 36096 KB Output is correct
5 Correct 259 ms 41356 KB Output is correct
6 Correct 8 ms 9684 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9804 KB Output is correct
10 Correct 61 ms 25376 KB Output is correct
11 Correct 61 ms 25328 KB Output is correct
12 Correct 192 ms 36228 KB Output is correct
13 Correct 223 ms 41400 KB Output is correct
14 Correct 165 ms 36656 KB Output is correct
15 Correct 159 ms 37176 KB Output is correct
16 Correct 166 ms 36716 KB Output is correct
17 Correct 176 ms 38940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 25424 KB Output is correct
2 Correct 58 ms 25424 KB Output is correct
3 Correct 131 ms 32688 KB Output is correct
4 Correct 99 ms 31824 KB Output is correct
5 Correct 180 ms 42244 KB Output is correct
6 Correct 176 ms 42312 KB Output is correct
7 Correct 178 ms 42204 KB Output is correct
8 Correct 179 ms 42256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 8 ms 9740 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
9 Correct 6 ms 9812 KB Output is correct
10 Correct 7 ms 10068 KB Output is correct
11 Correct 6 ms 9956 KB Output is correct
12 Correct 9 ms 9940 KB Output is correct
13 Correct 6 ms 9812 KB Output is correct
14 Correct 7 ms 9940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 8 ms 9740 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
9 Correct 6 ms 9812 KB Output is correct
10 Correct 7 ms 10068 KB Output is correct
11 Correct 6 ms 9956 KB Output is correct
12 Correct 9 ms 9940 KB Output is correct
13 Correct 6 ms 9812 KB Output is correct
14 Correct 7 ms 9940 KB Output is correct
15 Correct 6 ms 9812 KB Output is correct
16 Correct 8 ms 10040 KB Output is correct
17 Correct 43 ms 16348 KB Output is correct
18 Correct 39 ms 15676 KB Output is correct
19 Correct 41 ms 15436 KB Output is correct
20 Correct 37 ms 15308 KB Output is correct
21 Correct 35 ms 15080 KB Output is correct
22 Correct 71 ms 20524 KB Output is correct
23 Correct 21 ms 11564 KB Output is correct
24 Correct 44 ms 14776 KB Output is correct
25 Correct 7 ms 10040 KB Output is correct
26 Correct 15 ms 11348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 8 ms 9740 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
9 Correct 6 ms 9812 KB Output is correct
10 Correct 7 ms 10068 KB Output is correct
11 Correct 6 ms 9956 KB Output is correct
12 Correct 9 ms 9940 KB Output is correct
13 Correct 6 ms 9812 KB Output is correct
14 Correct 7 ms 9940 KB Output is correct
15 Correct 6 ms 9812 KB Output is correct
16 Correct 8 ms 10040 KB Output is correct
17 Correct 43 ms 16348 KB Output is correct
18 Correct 39 ms 15676 KB Output is correct
19 Correct 41 ms 15436 KB Output is correct
20 Correct 37 ms 15308 KB Output is correct
21 Correct 35 ms 15080 KB Output is correct
22 Correct 71 ms 20524 KB Output is correct
23 Correct 21 ms 11564 KB Output is correct
24 Correct 44 ms 14776 KB Output is correct
25 Correct 7 ms 10040 KB Output is correct
26 Correct 15 ms 11348 KB Output is correct
27 Correct 11 ms 10964 KB Output is correct
28 Correct 175 ms 35532 KB Output is correct
29 Correct 308 ms 48060 KB Output is correct
30 Correct 467 ms 77320 KB Output is correct
31 Correct 461 ms 77300 KB Output is correct
32 Correct 237 ms 48060 KB Output is correct
33 Correct 424 ms 78128 KB Output is correct
34 Correct 455 ms 78012 KB Output is correct
35 Correct 139 ms 30560 KB Output is correct
36 Correct 399 ms 64228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 25424 KB Output is correct
2 Correct 58 ms 25424 KB Output is correct
3 Correct 131 ms 32688 KB Output is correct
4 Correct 99 ms 31824 KB Output is correct
5 Correct 180 ms 42244 KB Output is correct
6 Correct 176 ms 42312 KB Output is correct
7 Correct 178 ms 42204 KB Output is correct
8 Correct 179 ms 42256 KB Output is correct
9 Correct 219 ms 51856 KB Output is correct
10 Correct 122 ms 27232 KB Output is correct
11 Correct 285 ms 44704 KB Output is correct
12 Correct 5 ms 9684 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 6 ms 9684 KB Output is correct
15 Correct 6 ms 9764 KB Output is correct
16 Correct 6 ms 9684 KB Output is correct
17 Correct 6 ms 9684 KB Output is correct
18 Correct 60 ms 25416 KB Output is correct
19 Correct 56 ms 25344 KB Output is correct
20 Correct 54 ms 25424 KB Output is correct
21 Correct 65 ms 25432 KB Output is correct
22 Correct 296 ms 45172 KB Output is correct
23 Correct 306 ms 53752 KB Output is correct
24 Correct 342 ms 58200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 179 ms 36080 KB Output is correct
2 Correct 172 ms 41380 KB Output is correct
3 Correct 56 ms 25420 KB Output is correct
4 Correct 57 ms 25372 KB Output is correct
5 Correct 539 ms 71592 KB Output is correct
6 Correct 687 ms 83956 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 241 ms 45412 KB Output is correct
9 Correct 307 ms 50916 KB Output is correct
10 Correct 148 ms 36096 KB Output is correct
11 Correct 259 ms 41356 KB Output is correct
12 Correct 8 ms 9684 KB Output is correct
13 Correct 6 ms 9684 KB Output is correct
14 Correct 5 ms 9684 KB Output is correct
15 Correct 5 ms 9804 KB Output is correct
16 Correct 61 ms 25376 KB Output is correct
17 Correct 61 ms 25328 KB Output is correct
18 Correct 192 ms 36228 KB Output is correct
19 Correct 223 ms 41400 KB Output is correct
20 Correct 165 ms 36656 KB Output is correct
21 Correct 159 ms 37176 KB Output is correct
22 Correct 166 ms 36716 KB Output is correct
23 Correct 176 ms 38940 KB Output is correct
24 Correct 57 ms 25424 KB Output is correct
25 Correct 58 ms 25424 KB Output is correct
26 Correct 131 ms 32688 KB Output is correct
27 Correct 99 ms 31824 KB Output is correct
28 Correct 180 ms 42244 KB Output is correct
29 Correct 176 ms 42312 KB Output is correct
30 Correct 178 ms 42204 KB Output is correct
31 Correct 179 ms 42256 KB Output is correct
32 Correct 6 ms 9684 KB Output is correct
33 Correct 5 ms 9684 KB Output is correct
34 Correct 5 ms 9684 KB Output is correct
35 Correct 8 ms 9740 KB Output is correct
36 Correct 6 ms 9684 KB Output is correct
37 Correct 6 ms 9684 KB Output is correct
38 Correct 6 ms 9684 KB Output is correct
39 Correct 6 ms 9684 KB Output is correct
40 Correct 6 ms 9812 KB Output is correct
41 Correct 7 ms 10068 KB Output is correct
42 Correct 6 ms 9956 KB Output is correct
43 Correct 9 ms 9940 KB Output is correct
44 Correct 6 ms 9812 KB Output is correct
45 Correct 7 ms 9940 KB Output is correct
46 Correct 6 ms 9812 KB Output is correct
47 Correct 8 ms 10040 KB Output is correct
48 Correct 43 ms 16348 KB Output is correct
49 Correct 39 ms 15676 KB Output is correct
50 Correct 41 ms 15436 KB Output is correct
51 Correct 37 ms 15308 KB Output is correct
52 Correct 35 ms 15080 KB Output is correct
53 Correct 71 ms 20524 KB Output is correct
54 Correct 21 ms 11564 KB Output is correct
55 Correct 44 ms 14776 KB Output is correct
56 Correct 7 ms 10040 KB Output is correct
57 Correct 15 ms 11348 KB Output is correct
58 Correct 11 ms 10964 KB Output is correct
59 Correct 175 ms 35532 KB Output is correct
60 Correct 308 ms 48060 KB Output is correct
61 Correct 467 ms 77320 KB Output is correct
62 Correct 461 ms 77300 KB Output is correct
63 Correct 237 ms 48060 KB Output is correct
64 Correct 424 ms 78128 KB Output is correct
65 Correct 455 ms 78012 KB Output is correct
66 Correct 139 ms 30560 KB Output is correct
67 Correct 399 ms 64228 KB Output is correct
68 Correct 219 ms 51856 KB Output is correct
69 Correct 122 ms 27232 KB Output is correct
70 Correct 285 ms 44704 KB Output is correct
71 Correct 5 ms 9684 KB Output is correct
72 Correct 5 ms 9684 KB Output is correct
73 Correct 6 ms 9684 KB Output is correct
74 Correct 6 ms 9764 KB Output is correct
75 Correct 6 ms 9684 KB Output is correct
76 Correct 6 ms 9684 KB Output is correct
77 Correct 60 ms 25416 KB Output is correct
78 Correct 56 ms 25344 KB Output is correct
79 Correct 54 ms 25424 KB Output is correct
80 Correct 65 ms 25432 KB Output is correct
81 Correct 296 ms 45172 KB Output is correct
82 Correct 306 ms 53752 KB Output is correct
83 Correct 342 ms 58200 KB Output is correct
84 Correct 464 ms 71200 KB Output is correct
85 Correct 492 ms 72376 KB Output is correct
86 Correct 619 ms 86936 KB Output is correct
87 Correct 688 ms 89980 KB Output is correct
88 Correct 5 ms 9796 KB Output is correct
89 Correct 662 ms 89932 KB Output is correct
90 Correct 471 ms 80616 KB Output is correct
91 Correct 359 ms 71564 KB Output is correct