Submission #947996

# Submission time Handle Problem Language Result Execution time Memory
947996 2024-03-17T11:31:32 Z BhavayGoyal Nafta (COI15_nafta) C++14
100 / 100
416 ms 152604 KB
#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template<class T> using oset = 
            tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define ll long long
#define ld long double
#define ar array
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int, int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define endl "\n"

const int MOD = 1e9+7;
const int inf = 1e9;
const ll linf = 1e18;

const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());


// -------------------------------------------------- Main Code --------------------------------------------------

const int N = 2005;

char arr[N][N];
int n, m, Cost[N][N], dp[N][N];
set<int> temp;

int dfs(int i, int j) {
    temp.insert(j);
    int cost = arr[i][j]-'0';
    arr[i][j] = '.';
    for (int x = 0; x < 4; x++) {
        int ni = d4i[x]+i, nj = d4j[x]+j;
        if (ni >= 1 && ni <= n && nj >= 1 && nj <= m && arr[ni][nj] != '.') {
            cost += dfs(ni, nj);
        }
    }
    return cost;
}

void dnc(int k, int l, int r, int optL, int optR) {
    if (l > r) return;

    int mid = (l+r)/2;
    pii best = {0, l};

    for (int i = optL; i <= min(mid, optR); i++) {
        int cost = dp[i][k-1] + Cost[i][mid];
        best = max(best, {cost, i});
    }

    dp[mid][k] = best.f;
    if (l == r) return;
    dnc(k, l, mid, optL, best.s);
    dnc(k, mid+1, r, best.s, optR);
}

void sol() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> arr[i][j];
        }
    }

    vii cost1(m+1, vi(m+1));
    for (int j = 1; j <= m; j++) {
        for (int i = 1; i <= n; i++) {
            if (arr[i][j] == '.') continue;
            temp.clear();
            int cost = dfs(i, j);
            for (auto &x : temp) {
                // starting from j, x.s is a part of it
                cost1[j][x] += cost;
            }
        }
    }

    for (int i = 1; i <= m; i++) for (int j = 1; j <= m; j++) cost1[i][j] += cost1[i-1][j];
    for (int i = 0; i <= m; i++) for (int j = 0; j <= m; j++) Cost[i][j] = cost1[j][j] - cost1[i][j];

    for (int k = 1; k <= m; k++) {
        // for (int i = 0; i <= m; i++) {
        //     for (int j = i; j >= 0; j--) {
        //         // current drill on i, last drill on j
        //         dp[i][k] = max(dp[i][k], dp[j][k-1] + Cost[j][i]);
        //     }
        // }
        dnc(k, 0, m, 0, m);

        int ans = 0; 
        for (int i = 0; i <= m; i++) ans = max(ans, dp[i][k]);
        cout << ans << endl;
    }
}

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t = 1;
    // cin >> t; 
    while (t--) {
        sol();
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 7 ms 8540 KB Output is correct
8 Correct 7 ms 8536 KB Output is correct
9 Correct 9 ms 10588 KB Output is correct
10 Correct 7 ms 8540 KB Output is correct
11 Correct 7 ms 8540 KB Output is correct
12 Correct 6 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 7 ms 8540 KB Output is correct
8 Correct 7 ms 8536 KB Output is correct
9 Correct 9 ms 10588 KB Output is correct
10 Correct 7 ms 8540 KB Output is correct
11 Correct 7 ms 8540 KB Output is correct
12 Correct 6 ms 8540 KB Output is correct
13 Correct 296 ms 51436 KB Output is correct
14 Correct 372 ms 55532 KB Output is correct
15 Correct 416 ms 152604 KB Output is correct
16 Correct 264 ms 55280 KB Output is correct
17 Correct 290 ms 55788 KB Output is correct
18 Correct 284 ms 55396 KB Output is correct