답안 #670040

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
670040 2022-12-07T21:38:26 Z CatalinT 메기 농장 (IOI22_fish) C++17
18 / 100
120 ms 22216 KB
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <vector>
#include <numeric>
#include <algorithm>
#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
#include <functional>
#include <queue>
#include <deque>
#include <stack>
#include <cassert>
#include <iomanip>
#include <cmath>
#include <bitset>
 
using namespace std;

using int64 = long long;



int64 max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
    vector<int64> sum_cols(N + 2);
    vector<vector<pair<int, int>>> vec_cols(N + 2);

    for (int i = 0; i < M; i++)
        X[i]++;

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

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

    vector<int64> dp(N + 2, 0);

    for (int i = 2; i <= N + 1; i++) {
        dp[i] = dp[i-1];

        // i - 2 ... i, take [i-2], [i]
        dp[i] = max(dp[i], (i - 3 >= 0 ? dp[i - 3] : 0) + sum_cols[i-2] + sum_cols[i]);
    
        // i - 4 ... i, take [i-4], [i-2], [i]
        if (i - 4 >= 0) {
            dp[i] = max(dp[i],
                (i - 5 >= 0 ? dp[i - 5] : 0) + sum_cols[i-4] + sum_cols[i-2] + sum_cols[i]);           
        }

        // i - 3 .. i
        if (i - 3 >= 0) {
            int64 prev = (i - 4 >= 0 ? dp[i - 4] : 0); 
            // Take all of [i], [i-3]
            dp[i] = max(dp[i], prev + sum_cols[i] + sum_cols[i-3]);
        
            auto build_stair = [&] (int a, int b, int f) {
                int64 tmp = prev + sum_cols[f];

                // adj on a
                // building over b


                int at = -1;

                int64 gained = 0;
                int64 lost = sum_cols[b];

                int64 res = tmp + gained + lost;

                int idx = 0;

                for (auto el : vec_cols[a]) {
                    gained += el.second;
                    // lost
                    while (idx < vec_cols[b].size() && vec_cols[b][idx].first <= el.first) {
                        lost -= vec_cols[b][idx].second;
                        idx++;
                    }

                    res = max(res, tmp + gained + lost);
                }

                return res;
            };

            dp[i] = max(dp[i], build_stair(i - 3, i - 2, i));
            dp[i] = max(dp[i], build_stair(i, i - 1, i - 3));
        }
    }

    return dp[N + 1];
}

// int main() {
//     // cerr << max_weights(
//     //     5, 
//     //     4,
//     //     vector<int>{0, 1, 4, 3},
//     //     vector<int>{2, 1, 4, 3},
//     //     vector<int>{5, 2, 1, 3}
//     // ) << "\n";
//     // cerr << max_weights(
//     //     4, 
//     //     4,
//     //     vector<int>{3, 0, 1, 2},
//     //     vector<int>{3, 0, 2, 2},
//     //     vector<int>{100, 50, 1000, 10}
//     // ) << "\n";
//     return 0;
// }

Compilation message

fish.cpp: In lambda function:
fish.cpp:83:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |                     while (idx < vec_cols[b].size() && vec_cols[b][idx].first <= el.first) {
      |                            ~~~~^~~~~~~~~~~~~~~~~~~~
fish.cpp:71:21: warning: unused variable 'at' [-Wunused-variable]
   71 |                 int at = -1;
      |                     ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 7744 KB Output is correct
2 Correct 40 ms 9128 KB Output is correct
3 Correct 4 ms 4180 KB Output is correct
4 Correct 3 ms 4180 KB Output is correct
5 Correct 115 ms 20160 KB Output is correct
6 Correct 120 ms 22216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 67 ms 11808 KB Output is correct
3 Correct 77 ms 14132 KB Output is correct
4 Correct 32 ms 7824 KB Output is correct
5 Correct 41 ms 9152 KB Output is correct
6 Correct 0 ms 300 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 4 ms 4180 KB Output is correct
11 Correct 3 ms 4180 KB Output is correct
12 Correct 32 ms 7844 KB Output is correct
13 Correct 39 ms 9116 KB Output is correct
14 Correct 32 ms 7896 KB Output is correct
15 Correct 37 ms 8752 KB Output is correct
16 Correct 35 ms 7888 KB Output is correct
17 Correct 36 ms 8788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 4232 KB Output is correct
2 Correct 3 ms 4180 KB Output is correct
3 Correct 24 ms 7636 KB Output is correct
4 Correct 16 ms 6736 KB Output is correct
5 Correct 42 ms 11312 KB Output is correct
6 Correct 36 ms 10544 KB Output is correct
7 Correct 38 ms 11212 KB Output is correct
8 Correct 47 ms 11320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB 1st lines differ - on the 1st token, expected: '3', found: '2'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB 1st lines differ - on the 1st token, expected: '3', found: '2'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB 1st lines differ - on the 1st token, expected: '3', found: '2'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 4232 KB Output is correct
2 Correct 3 ms 4180 KB Output is correct
3 Correct 24 ms 7636 KB Output is correct
4 Correct 16 ms 6736 KB Output is correct
5 Correct 42 ms 11312 KB Output is correct
6 Correct 36 ms 10544 KB Output is correct
7 Correct 38 ms 11212 KB Output is correct
8 Correct 47 ms 11320 KB Output is correct
9 Incorrect 39 ms 10936 KB 1st lines differ - on the 1st token, expected: '99999', found: '74999'
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 7744 KB Output is correct
2 Correct 40 ms 9128 KB Output is correct
3 Correct 4 ms 4180 KB Output is correct
4 Correct 3 ms 4180 KB Output is correct
5 Correct 115 ms 20160 KB Output is correct
6 Correct 120 ms 22216 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 67 ms 11808 KB Output is correct
9 Correct 77 ms 14132 KB Output is correct
10 Correct 32 ms 7824 KB Output is correct
11 Correct 41 ms 9152 KB Output is correct
12 Correct 0 ms 300 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 4 ms 4180 KB Output is correct
17 Correct 3 ms 4180 KB Output is correct
18 Correct 32 ms 7844 KB Output is correct
19 Correct 39 ms 9116 KB Output is correct
20 Correct 32 ms 7896 KB Output is correct
21 Correct 37 ms 8752 KB Output is correct
22 Correct 35 ms 7888 KB Output is correct
23 Correct 36 ms 8788 KB Output is correct
24 Correct 5 ms 4232 KB Output is correct
25 Correct 3 ms 4180 KB Output is correct
26 Correct 24 ms 7636 KB Output is correct
27 Correct 16 ms 6736 KB Output is correct
28 Correct 42 ms 11312 KB Output is correct
29 Correct 36 ms 10544 KB Output is correct
30 Correct 38 ms 11212 KB Output is correct
31 Correct 47 ms 11320 KB Output is correct
32 Incorrect 1 ms 212 KB 1st lines differ - on the 1st token, expected: '3', found: '2'
33 Halted 0 ms 0 KB -