답안 #993537

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
993537 2024-06-06T00:49:29 Z Clan328 메기 농장 (IOI22_fish) C++17
37 / 100
1000 ms 124352 KB
#include "fish.h"

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define nL '\n'
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;

int ipow(int a, int n) {
   if (n == 0) return 1;
   int x = ipow(a, n/2);
   if (n % 2 == 0) return x*x;
   return x*x*a;
}

template <typename T>
ostream& operator<<(ostream& stream, const vector<T>& v) {
    for (auto elem : v) 
        stream << elem << " ";
    return stream;
}

template <typename T>
istream& operator>>(istream& stream, vector<T>& v){
   for(auto &elem : v)
        stream >> elem;
   return stream;
}

long long smallN(int N, int M, vi X, vi Y,
                      vi W) {
    ll maxY = 0;
    for (int i = 0; i < M; i++) maxY = max(maxY, (ll)Y[i]);
    maxY++;
    vvl mat(N, vl(maxY+1));
    for (int i = 0; i < M; i++) mat[X[i]][Y[i]] = W[i];

    vector<vvl> dp(N, vvl(maxY+1, vl(maxY+1)));
    
    ll _tmp1 = 0;
    for (int j = 0; j <= maxY; j++) {
        if (j > 0) _tmp1 += mat[0][j-1];

        ll _tmp2 = 0;
        for (int k = 0; k <= maxY; k++) {
            if (k <= j) {
                if (k > 0) _tmp2 -= mat[0][k-1];
            } else {
                if (k > 0) _tmp2 += mat[1][k-1];
            }

            dp[1][j][k] = _tmp1+_tmp2;
            // cout << j << " " << k << " " << _tmp1+_tmp2 << nL;
        }
    }
    
    for (int i = 2; i < N; i++) {
        ll tmp1 = 0;
        for (int j = 0; j <= maxY; j++) {
            if (j > 0) tmp1 += mat[i-1][j-1];

            ll tmp2 = 0;
            for (int k = 0; k <= maxY; k++) {
                if (k <= j) {
                    if (k > 0) tmp2 -= mat[i-1][k-1];
                } else {
                    if (k > 0) tmp2 += mat[i][k-1];
                }

                ll tmp3 = 0;
                for (int l = 0; l <= maxY;l++) {
                    if (l > k && l <= j) {
                        if (l > 0) tmp3 -= mat[i-1][l-1];
                    }

                    dp[i][j][k] = max(dp[i][j][k], dp[i-1][k][l]+tmp1+tmp2+tmp3);
                }
            }
        }
    }

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

    return res;
}

#define int long long
vector<vpll> col;

ll sum(ll x, ll y) {
    ll res = 0;
    for (auto [y_, w] : col[x]) {
        if (y_ > y) break;
        res += w;
    }
    return res;
}
#undef int

long long max_weights(int N, int M, vi X, vi Y,
                      vi W) {
    #define int long long
    ll maxY = 0;
    for (int i = 0; i < M; i++) maxY = max(maxY, (ll)Y[i]);
    maxY++;
    // vvl mat(N, vl(maxY+1));
    // for (int i = 0; i < M; i++) mat[X[i]][Y[i]] = W[i];

    if (N <= 300 && maxY <= 10) return smallN(N, M, X, Y, W);

    col = vector<vpll>(N);
    for (int i = 0; i < M; i++) col[X[i]].pb({Y[i]+1, W[i]});

    vector<vvl> dp(N, vvl(6, vl(6)));
    vvi pos(N, {0});
    for (int i = 0; i < M; i++) {
        if (X[i]+1 < N) pos[X[i]+1].pb(Y[i]+1);
        if (X[i]-1 >= 0) pos[X[i]-1].pb(Y[i]+1);
    }

    for (int i = 0; i < N; i++) {
        sort(all(pos[i]));
        sort(all(col[i]));
    }

    for (int j = 0; j < pos[1].size(); j++) {
        for (int k = 0; k < pos[0].size(); k++) {
            dp[1][j][k] = max(0LL, sum(0, pos[1][j])-sum(0, pos[0][k]))+max(0LL, sum(1, pos[0][k])-sum(1, pos[1][j]));
        }
    }
    
    for (int i = 2; i < N; i++) {
        for (int j = 0; j < pos[i].size(); j++) {
            for (int k = 0; k < pos[i-1].size(); k++) {
                for (int l = 0; l < pos[i-2].size();l++) {
                    ll res = max(0LL, sum(i, pos[i-1][k])-sum(i, pos[i][j]))+max(0LL, sum(i-1, pos[i][j])-max(sum(i-1, pos[i-2][l]),sum(i-1, pos[i-1][k])));

                    dp[i][j][k] = max(dp[i][j][k], dp[i-1][k][l]+res);
                    // cout << i << ": " << pos[i-2][l] << " " << pos[i-1][k] << " " << pos[i][j] << " " << res << nL;
                }
            }
        }
    }

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

    #undef int
    return res;
}














Compilation message

fish.cpp: In function 'long long int max_weights(int, int, vi, vi, vi)':
fish.cpp:145:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  145 |     for (int j = 0; j < pos[1].size(); j++) {
      |                     ~~^~~~~~~~~~~~~~~
fish.cpp:146:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  146 |         for (int k = 0; k < pos[0].size(); k++) {
      |                         ~~^~~~~~~~~~~~~~~
fish.cpp:152:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  152 |         for (int j = 0; j < pos[i].size(); j++) {
      |                         ~~^~~~~~~~~~~~~~~
fish.cpp:153:31: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  153 |             for (int k = 0; k < pos[i-1].size(); k++) {
      |                             ~~^~~~~~~~~~~~~~~~~
fish.cpp:154:35: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  154 |                 for (int l = 0; l < pos[i-2].size();l++) {
      |                                 ~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 104 ms 124352 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Execution timed out 1058 ms 65396 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 70 ms 63828 KB Output is correct
2 Correct 54 ms 63832 KB Output is correct
3 Correct 85 ms 60240 KB Output is correct
4 Correct 79 ms 65616 KB Output is correct
5 Correct 122 ms 69192 KB Output is correct
6 Correct 113 ms 69204 KB Output is correct
7 Correct 119 ms 69204 KB Output is correct
8 Correct 115 ms 69276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 440 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 440 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Runtime error 1 ms 860 KB Execution killed with signal 11
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 440 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Runtime error 1 ms 860 KB Execution killed with signal 11
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 70 ms 63828 KB Output is correct
2 Correct 54 ms 63832 KB Output is correct
3 Correct 85 ms 60240 KB Output is correct
4 Correct 79 ms 65616 KB Output is correct
5 Correct 122 ms 69192 KB Output is correct
6 Correct 113 ms 69204 KB Output is correct
7 Correct 119 ms 69204 KB Output is correct
8 Correct 115 ms 69276 KB Output is correct
9 Correct 132 ms 69204 KB Output is correct
10 Correct 135 ms 38224 KB Output is correct
11 Correct 256 ms 76004 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 53 ms 63720 KB Output is correct
19 Correct 54 ms 63568 KB Output is correct
20 Correct 59 ms 63828 KB Output is correct
21 Correct 55 ms 63832 KB Output is correct
22 Correct 127 ms 69212 KB Output is correct
23 Correct 260 ms 79420 KB Output is correct
24 Correct 247 ms 80108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 104 ms 124352 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -