Submission #677200

# Submission time Handle Problem Language Result Execution time Memory
677200 2023-01-02T14:31:21 Z Ninja_Kunai Catfish Farm (IOI22_fish) C++17
67 / 100
1000 ms 81088 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 <= 3e3) 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;
}
# Verdict Execution time Memory Grader output
1 Correct 28 ms 13464 KB Output is correct
2 Correct 32 ms 14352 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 93 ms 23784 KB Output is correct
6 Correct 107 ms 23724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 53 ms 18684 KB Output is correct
3 Correct 65 ms 20588 KB Output is correct
4 Correct 29 ms 13488 KB Output is correct
5 Correct 32 ms 14348 KB Output is correct
6 Correct 6 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 6 ms 9684 KB Output is correct
10 Correct 6 ms 9620 KB Output is correct
11 Correct 6 ms 9684 KB Output is correct
12 Correct 29 ms 14836 KB Output is correct
13 Correct 35 ms 15848 KB Output is correct
14 Correct 30 ms 14848 KB Output is correct
15 Correct 35 ms 15476 KB Output is correct
16 Correct 29 ms 14880 KB Output is correct
17 Correct 32 ms 15472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9708 KB Output is correct
2 Correct 8 ms 10540 KB Output is correct
3 Correct 23 ms 13180 KB Output is correct
4 Correct 16 ms 12420 KB Output is correct
5 Correct 34 ms 15476 KB Output is correct
6 Correct 28 ms 15552 KB Output is correct
7 Correct 32 ms 15488 KB Output is correct
8 Correct 34 ms 15464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9712 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 6 ms 9716 KB Output is correct
5 Correct 7 ms 9684 KB Output is correct
6 Correct 7 ms 9684 KB Output is correct
7 Correct 6 ms 9644 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
9 Correct 7 ms 10580 KB Output is correct
10 Correct 8 ms 11860 KB Output is correct
11 Correct 8 ms 10580 KB Output is correct
12 Correct 9 ms 11732 KB Output is correct
13 Correct 7 ms 10068 KB Output is correct
14 Correct 8 ms 11732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9712 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 6 ms 9716 KB Output is correct
5 Correct 7 ms 9684 KB Output is correct
6 Correct 7 ms 9684 KB Output is correct
7 Correct 6 ms 9644 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
9 Correct 7 ms 10580 KB Output is correct
10 Correct 8 ms 11860 KB Output is correct
11 Correct 8 ms 10580 KB Output is correct
12 Correct 9 ms 11732 KB Output is correct
13 Correct 7 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 10316 KB Output is correct
17 Correct 148 ms 16112 KB Output is correct
18 Correct 137 ms 16780 KB Output is correct
19 Correct 101 ms 16580 KB Output is correct
20 Correct 80 ms 16444 KB Output is correct
21 Correct 80 ms 16484 KB Output is correct
22 Correct 248 ms 21220 KB Output is correct
23 Correct 20 ms 12712 KB Output is correct
24 Correct 86 ms 14956 KB Output is correct
25 Correct 7 ms 11732 KB Output is correct
26 Correct 18 ms 12592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9712 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 6 ms 9716 KB Output is correct
5 Correct 7 ms 9684 KB Output is correct
6 Correct 7 ms 9684 KB Output is correct
7 Correct 6 ms 9644 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
9 Correct 7 ms 10580 KB Output is correct
10 Correct 8 ms 11860 KB Output is correct
11 Correct 8 ms 10580 KB Output is correct
12 Correct 9 ms 11732 KB Output is correct
13 Correct 7 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 10316 KB Output is correct
17 Correct 148 ms 16112 KB Output is correct
18 Correct 137 ms 16780 KB Output is correct
19 Correct 101 ms 16580 KB Output is correct
20 Correct 80 ms 16444 KB Output is correct
21 Correct 80 ms 16484 KB Output is correct
22 Correct 248 ms 21220 KB Output is correct
23 Correct 20 ms 12712 KB Output is correct
24 Correct 86 ms 14956 KB Output is correct
25 Correct 7 ms 11732 KB Output is correct
26 Correct 18 ms 12592 KB Output is correct
27 Correct 55 ms 81088 KB Output is correct
28 Execution timed out 1073 ms 39764 KB Time limit exceeded
29 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9708 KB Output is correct
2 Correct 8 ms 10540 KB Output is correct
3 Correct 23 ms 13180 KB Output is correct
4 Correct 16 ms 12420 KB Output is correct
5 Correct 34 ms 15476 KB Output is correct
6 Correct 28 ms 15552 KB Output is correct
7 Correct 32 ms 15488 KB Output is correct
8 Correct 34 ms 15464 KB Output is correct
9 Correct 144 ms 36260 KB Output is correct
10 Correct 92 ms 27748 KB Output is correct
11 Correct 203 ms 45808 KB Output is correct
12 Correct 6 ms 9684 KB Output is correct
13 Correct 6 ms 9708 KB Output is correct
14 Correct 5 ms 9708 KB Output is correct
15 Correct 6 ms 9684 KB Output is correct
16 Correct 6 ms 9684 KB Output is correct
17 Correct 6 ms 9684 KB Output is correct
18 Correct 6 ms 9684 KB Output is correct
19 Correct 6 ms 9684 KB Output is correct
20 Correct 7 ms 10500 KB Output is correct
21 Correct 31 ms 25228 KB Output is correct
22 Correct 157 ms 35372 KB Output is correct
23 Correct 276 ms 47512 KB Output is correct
24 Correct 250 ms 48048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 13464 KB Output is correct
2 Correct 32 ms 14352 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 93 ms 23784 KB Output is correct
6 Correct 107 ms 23724 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 53 ms 18684 KB Output is correct
9 Correct 65 ms 20588 KB Output is correct
10 Correct 29 ms 13488 KB Output is correct
11 Correct 32 ms 14348 KB Output is correct
12 Correct 6 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 6 ms 9684 KB Output is correct
16 Correct 6 ms 9620 KB Output is correct
17 Correct 6 ms 9684 KB Output is correct
18 Correct 29 ms 14836 KB Output is correct
19 Correct 35 ms 15848 KB Output is correct
20 Correct 30 ms 14848 KB Output is correct
21 Correct 35 ms 15476 KB Output is correct
22 Correct 29 ms 14880 KB Output is correct
23 Correct 32 ms 15472 KB Output is correct
24 Correct 6 ms 9708 KB Output is correct
25 Correct 8 ms 10540 KB Output is correct
26 Correct 23 ms 13180 KB Output is correct
27 Correct 16 ms 12420 KB Output is correct
28 Correct 34 ms 15476 KB Output is correct
29 Correct 28 ms 15552 KB Output is correct
30 Correct 32 ms 15488 KB Output is correct
31 Correct 34 ms 15464 KB Output is correct
32 Correct 6 ms 9684 KB Output is correct
33 Correct 6 ms 9712 KB Output is correct
34 Correct 5 ms 9684 KB Output is correct
35 Correct 6 ms 9716 KB Output is correct
36 Correct 7 ms 9684 KB Output is correct
37 Correct 7 ms 9684 KB Output is correct
38 Correct 6 ms 9644 KB Output is correct
39 Correct 6 ms 9684 KB Output is correct
40 Correct 7 ms 10580 KB Output is correct
41 Correct 8 ms 11860 KB Output is correct
42 Correct 8 ms 10580 KB Output is correct
43 Correct 9 ms 11732 KB Output is correct
44 Correct 7 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 10316 KB Output is correct
48 Correct 148 ms 16112 KB Output is correct
49 Correct 137 ms 16780 KB Output is correct
50 Correct 101 ms 16580 KB Output is correct
51 Correct 80 ms 16444 KB Output is correct
52 Correct 80 ms 16484 KB Output is correct
53 Correct 248 ms 21220 KB Output is correct
54 Correct 20 ms 12712 KB Output is correct
55 Correct 86 ms 14956 KB Output is correct
56 Correct 7 ms 11732 KB Output is correct
57 Correct 18 ms 12592 KB Output is correct
58 Correct 55 ms 81088 KB Output is correct
59 Execution timed out 1073 ms 39764 KB Time limit exceeded
60 Halted 0 ms 0 KB -