답안 #677198

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
677198 2023-01-02T14:27:25 Z Ninja_Kunai 메기 농장 (IOI22_fish) C++17
67 / 100
1000 ms 80972 KB
/**
*    Author :  Nguyen Tuan Vu
*    Created : 01.01.2023
**/

#pragma GCC optimize("O2")
#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#define MASK(x) ((1ll)<<(x))
#define BIT(x, i) (((x)>>(i))&(1))
#define ALL(v)  (v).begin(), (v).end()
#define REP(i, n)  for (int i = 0, _n = (n); i < _n; ++i)
#define FOR(i, a, b)  for (int i = (a), _b = (b); i <= _b; ++i) 
#define FORD(i, b, a)  for (int i = (b), _a = (a); i >= _a; --i)
#define db(val) "["#val" = "<<(val)<<"] "

template <class X, class Y> bool minimize(X &a, Y b) {
    if (a > b) return a = b, true;
    return false;
}
template <class X, class Y> bool maximize(X &a, Y b) {
    if (a < b) return a = b, true;
    return false;
}

using namespace std;

mt19937 jdg(chrono::steady_clock::now().time_since_epoch().count());
int Rand(int l, int r) {return l + jdg() % (r - l + 1);}

const int N = 1e5 + 5;
namespace sub1 {
	bool check(int m, vector <array <int, 3>> fishes) {
		REP(i, m) {
			if (fishes[i][0] & 1) return false;
		}

		return true;
	}

	long long solve(int m, vector <array <int, 3>> fishes) {
		//assert(1 == 0);
		long long ans = 0;
		REP(i, m) ans += fishes[i][2];
		return ans;
	}
}

namespace sub2 {
	bool check(int m, vector <array <int, 3>> fishes) {
		REP(i, m) if (fishes[i][0] > 1) return false;
		return true;
	}

	long long solve(int n, int m, vector <array <int, 3>> fishes) {
		//assert(1 == 0);
		if (n == 2) {
			long long ans[2] = {0};
			REP(i, m) ans[fishes[i][0]] += fishes[i][2];
			return max(ans[0], ans[1]);
		}

		vector <long long> sum[2];
		REP(i, 2) sum[i].resize(n + 7, 0);
		REP(i, m) sum[fishes[i][0]][fishes[i][1]] += fishes[i][2];
		FOR(i, 1, n - 1) REP(j, 2) sum[j][i] = sum[j][i - 1] + sum[j][i];
		long long ans = sum[1][n - 1];
		REP(i, n) maximize(ans, sum[0][i] + sum[1][n - 1] - sum[1][i]);
		return ans;
	}
}

namespace sub3 {
    bool check(int m, vector <array <int, 3>> fishes) {
        REP(i, m) if (fishes[i][1] != 0) return false;
        return true;
    }

    int cost[N];
    long long dp[N];

    long long solve(int n, int m, vector <array <int, 3>> fishes) {
        REP(i, m) cost[fishes[i][0] + 1] = fishes[i][2];
        FOR(i, 1, n) {
            maximize(dp[i], dp[i - 1]);
            if (i >= 2) maximize(dp[i], dp[i - 2] + cost[i - 1]);
            if (i > 3) maximize(dp[i], dp[i - 3] + cost[i - 1] + cost[i - 2]);
        }

        long long ans = 0;
        FOR(i, 1, n) {
            dp[i] = dp[i] + cost[i + 1];
            maximize(ans, dp[i]);
        }

        return ans;
    }
}

const long long INF = 1e18 + 7;
namespace sub6 {
    vector <int> coor[N];
    vector <long long> f[N], g[N];
    vector <pair <int, long long>> sum[N];

    const int M = 3e3 + 5;
    long long sum2[M][M];

    long long get_sum(int n, int i, int x) {
    	if (n <= 3e2) return sum2[i][x];
    	if (sum[i].size() == 0 || x < sum[i][0].first) return 0;
    	if (sum[i].back().first <= x) return sum[i].back().second;

        int pos = upper_bound(ALL(sum[i]), make_pair(x, INF)) - sum[i].begin() - 1;
        if (pos == -1) return 0;
        return sum[i][pos].second;
    }

    long long solve(int n, int m, vector <array <int, 3>> fishes) {
        REP(i, m) {
            if (fishes[i][0] + 1 > 1) coor[fishes[i][0]].push_back(fishes[i][1] + 1);
            if (fishes[i][0] + 1 < n) coor[fishes[i][0] + 2].push_back(fishes[i][1] + 1);

            if (n <= 3e3) sum2[fishes[i][0] + 1][fishes[i][1] + 1] = fishes[i][2];
            sum[fishes[i][0] + 1].push_back({fishes[i][1] + 1, fishes[i][2]});
        }

        if (n <= 3e3) {
        	FOR(i, 1, n) FOR(j, 1, n) sum2[i][j] += sum2[i][j - 1];
        }

        FOR(i, 1, n) {
            coor[i].push_back(0);
            sort (ALL(coor[i]));
            coor[i].erase(unique(ALL(coor[i])), coor[i].end());
            f[i].resize(coor[i].size() + 5, -1);
            g[i].resize(coor[i].size() + 5, -1);

            sort (ALL(sum[i]));
            FOR(j, 1, (int) sum[i].size() - 1) sum[i][j].second += sum[i][j - 1].second;
        }

        // f : roi
        // g : chua 
        REP(i, coor[1].size()) g[1][i] = 0;

        FOR(i, 2, n) {
            REP(k, coor[i - 1].size()) if (f[i - 1][k] != -1 || g[i - 1][k] != -1) {
                REP(j, coor[i].size()) if (coor[i - 1][k] <= coor[i][j]) {
                    // f
                    if (f[i - 1][k] != -1) maximize(g[i][j], f[i - 1][k]);

                    // g
                    if (g[i - 1][k] != -1) {
                        long long R = get_sum(n, i - 1, coor[i][j]);
                        long long L = get_sum(n, i - 1, coor[i - 1][k]);

                        maximize(g[i][j], g[i - 1][k] + R - L);
                    }
                }
                else {
                    // update f
                    maximize(f[i][j], max(f[i - 1][k], g[i - 1][k]) + get_sum(n, i, coor[i - 1][k]) - get_sum(n, i, coor[i][j]));

                    // update g
                    maximize(g[i][j], max(f[i - 1][k], g[i - 1][k]));
                }
            }

            if (i > 2) {
                REP(k, coor[i - 2].size()) if (f[i - 2][k] != -1 || g[i - 2][k] != -1) {
                    REP(j, coor[i].size()) {
                        maximize(g[i][j], max(f[i - 2][k], g[i - 2][k]) + get_sum(n, i - 1, max(coor[i - 2][k], coor[i][j])));
                    }
                }
            }

            if (i > 3) {
                REP(k, coor[i - 3].size()) if (f[i - 3][k] != -1 || g[i - 3][k] != -1) {
                    REP(j, coor[i].size()) {
                        maximize(g[i][j], max(f[i - 3][k], g[i - 3][k]) + get_sum(n, i - 1, coor[i][j]) + get_sum(n, i - 2, coor[i - 3][k]));
                    }
                }               
            }
        }

        long long ans = 0;
        FOR(i, 1, n) REP(j, coor[i].size()) maximize(ans, max(f[i][j], g[i][j]) + get_sum(n, i + 1, coor[i][j]));

        return ans;
    }
};

long long max_weights(int n, int m, vector <int> X, vector <int> Y, vector <int> W) {
	vector <array <int, 3>> fishes;
	fishes.resize(m + 7);
	REP(i, m) fishes[i] = {X[i], Y[i], W[i]};

	if (sub1::check(m, fishes)) return sub1::solve(m, fishes);
	else if (sub2::check(m, fishes)) return sub2::solve(n, m, fishes);
	else if (sub3::check(m, fishes)) return sub3::solve(n, m, fishes);
	else return sub6::solve(n, m, fishes);
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 13500 KB Output is correct
2 Correct 38 ms 14360 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 93 ms 23732 KB Output is correct
6 Correct 94 ms 23736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 58 ms 18672 KB Output is correct
3 Correct 66 ms 20588 KB Output is correct
4 Correct 29 ms 13480 KB Output is correct
5 Correct 33 ms 14352 KB Output is correct
6 Correct 6 ms 9620 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 9684 KB Output is correct
10 Correct 7 ms 9684 KB Output is correct
11 Correct 5 ms 9684 KB Output is correct
12 Correct 30 ms 14836 KB Output is correct
13 Correct 34 ms 15860 KB Output is correct
14 Correct 29 ms 14864 KB Output is correct
15 Correct 33 ms 15512 KB Output is correct
16 Correct 29 ms 14812 KB Output is correct
17 Correct 32 ms 15472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 7 ms 10468 KB Output is correct
3 Correct 23 ms 13184 KB Output is correct
4 Correct 18 ms 12420 KB Output is correct
5 Correct 33 ms 15544 KB Output is correct
6 Correct 28 ms 15448 KB Output is correct
7 Correct 36 ms 15480 KB Output is correct
8 Correct 32 ms 15472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 7 ms 9684 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 7 ms 9684 KB Output is correct
5 Correct 6 ms 9664 KB Output is correct
6 Correct 5 ms 9716 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
9 Correct 8 ms 10460 KB Output is correct
10 Correct 8 ms 11860 KB Output is correct
11 Correct 7 ms 10580 KB Output is correct
12 Correct 8 ms 11904 KB Output is correct
13 Correct 6 ms 10068 KB Output is correct
14 Correct 8 ms 11732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 7 ms 9684 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 7 ms 9684 KB Output is correct
5 Correct 6 ms 9664 KB Output is correct
6 Correct 5 ms 9716 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
9 Correct 8 ms 10460 KB Output is correct
10 Correct 8 ms 11860 KB Output is correct
11 Correct 7 ms 10580 KB Output is correct
12 Correct 8 ms 11904 KB Output is correct
13 Correct 6 ms 10068 KB Output is correct
14 Correct 8 ms 11732 KB Output is correct
15 Correct 8 ms 11604 KB Output is correct
16 Correct 8 ms 10244 KB Output is correct
17 Correct 173 ms 15948 KB Output is correct
18 Correct 134 ms 17420 KB Output is correct
19 Correct 117 ms 17300 KB Output is correct
20 Correct 82 ms 17116 KB Output is correct
21 Correct 81 ms 17128 KB Output is correct
22 Correct 256 ms 22600 KB Output is correct
23 Correct 21 ms 12788 KB Output is correct
24 Correct 98 ms 15300 KB Output is correct
25 Correct 8 ms 11788 KB Output is correct
26 Correct 20 ms 12732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 7 ms 9684 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 7 ms 9684 KB Output is correct
5 Correct 6 ms 9664 KB Output is correct
6 Correct 5 ms 9716 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
9 Correct 8 ms 10460 KB Output is correct
10 Correct 8 ms 11860 KB Output is correct
11 Correct 7 ms 10580 KB Output is correct
12 Correct 8 ms 11904 KB Output is correct
13 Correct 6 ms 10068 KB Output is correct
14 Correct 8 ms 11732 KB Output is correct
15 Correct 8 ms 11604 KB Output is correct
16 Correct 8 ms 10244 KB Output is correct
17 Correct 173 ms 15948 KB Output is correct
18 Correct 134 ms 17420 KB Output is correct
19 Correct 117 ms 17300 KB Output is correct
20 Correct 82 ms 17116 KB Output is correct
21 Correct 81 ms 17128 KB Output is correct
22 Correct 256 ms 22600 KB Output is correct
23 Correct 21 ms 12788 KB Output is correct
24 Correct 98 ms 15300 KB Output is correct
25 Correct 8 ms 11788 KB Output is correct
26 Correct 20 ms 12732 KB Output is correct
27 Correct 52 ms 80972 KB Output is correct
28 Execution timed out 1082 ms 43312 KB Time limit exceeded
29 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 7 ms 10468 KB Output is correct
3 Correct 23 ms 13184 KB Output is correct
4 Correct 18 ms 12420 KB Output is correct
5 Correct 33 ms 15544 KB Output is correct
6 Correct 28 ms 15448 KB Output is correct
7 Correct 36 ms 15480 KB Output is correct
8 Correct 32 ms 15472 KB Output is correct
9 Correct 130 ms 36408 KB Output is correct
10 Correct 110 ms 27712 KB Output is correct
11 Correct 199 ms 45708 KB Output is correct
12 Correct 5 ms 9596 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 5 ms 9684 KB Output is correct
15 Correct 5 ms 9712 KB Output is correct
16 Correct 5 ms 9620 KB Output is correct
17 Correct 6 ms 9684 KB Output is correct
18 Correct 7 ms 9684 KB Output is correct
19 Correct 7 ms 9708 KB Output is correct
20 Correct 8 ms 10452 KB Output is correct
21 Correct 36 ms 25280 KB Output is correct
22 Correct 147 ms 35296 KB Output is correct
23 Correct 263 ms 47524 KB Output is correct
24 Correct 322 ms 48004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 13500 KB Output is correct
2 Correct 38 ms 14360 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 93 ms 23732 KB Output is correct
6 Correct 94 ms 23736 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 58 ms 18672 KB Output is correct
9 Correct 66 ms 20588 KB Output is correct
10 Correct 29 ms 13480 KB Output is correct
11 Correct 33 ms 14352 KB Output is correct
12 Correct 6 ms 9620 KB Output is correct
13 Correct 6 ms 9684 KB Output is correct
14 Correct 6 ms 9684 KB Output is correct
15 Correct 6 ms 9684 KB Output is correct
16 Correct 7 ms 9684 KB Output is correct
17 Correct 5 ms 9684 KB Output is correct
18 Correct 30 ms 14836 KB Output is correct
19 Correct 34 ms 15860 KB Output is correct
20 Correct 29 ms 14864 KB Output is correct
21 Correct 33 ms 15512 KB Output is correct
22 Correct 29 ms 14812 KB Output is correct
23 Correct 32 ms 15472 KB Output is correct
24 Correct 6 ms 9684 KB Output is correct
25 Correct 7 ms 10468 KB Output is correct
26 Correct 23 ms 13184 KB Output is correct
27 Correct 18 ms 12420 KB Output is correct
28 Correct 33 ms 15544 KB Output is correct
29 Correct 28 ms 15448 KB Output is correct
30 Correct 36 ms 15480 KB Output is correct
31 Correct 32 ms 15472 KB Output is correct
32 Correct 5 ms 9684 KB Output is correct
33 Correct 7 ms 9684 KB Output is correct
34 Correct 6 ms 9684 KB Output is correct
35 Correct 7 ms 9684 KB Output is correct
36 Correct 6 ms 9664 KB Output is correct
37 Correct 5 ms 9716 KB Output is correct
38 Correct 6 ms 9684 KB Output is correct
39 Correct 6 ms 9684 KB Output is correct
40 Correct 8 ms 10460 KB Output is correct
41 Correct 8 ms 11860 KB Output is correct
42 Correct 7 ms 10580 KB Output is correct
43 Correct 8 ms 11904 KB Output is correct
44 Correct 6 ms 10068 KB Output is correct
45 Correct 8 ms 11732 KB Output is correct
46 Correct 8 ms 11604 KB Output is correct
47 Correct 8 ms 10244 KB Output is correct
48 Correct 173 ms 15948 KB Output is correct
49 Correct 134 ms 17420 KB Output is correct
50 Correct 117 ms 17300 KB Output is correct
51 Correct 82 ms 17116 KB Output is correct
52 Correct 81 ms 17128 KB Output is correct
53 Correct 256 ms 22600 KB Output is correct
54 Correct 21 ms 12788 KB Output is correct
55 Correct 98 ms 15300 KB Output is correct
56 Correct 8 ms 11788 KB Output is correct
57 Correct 20 ms 12732 KB Output is correct
58 Correct 52 ms 80972 KB Output is correct
59 Execution timed out 1082 ms 43312 KB Time limit exceeded
60 Halted 0 ms 0 KB -