제출 #1050871

#제출 시각아이디문제언어결과실행 시간메모리
1050871coin_Nafta (COI15_nafta)C++14
34 / 100
1026 ms71248 KiB
#include <bits/stdc++.h>
#define int long long
using namespace std;
 
const int maxcol = 2003;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};
vector<vector<int>> id(maxcol, vector<int>(maxcol, 0)), cost(maxcol, vector<int>(maxcol, 0));
 
void calculatecostfunction(int col){
    for (int end = 1; end <= col; end++){
        for (int start = end; start >= 0; start--){
            cost[start][end] = cost[start+1][end] + id[start+1][end];
        }
    }
}
 
int l, r, totalcost = 0;
void dfs(int x, int y, vector<vector<char>> &board, int n, int m){
    totalcost += board[y][x] - '0';
    board[y][x] = '.';
    l = min(l, x);
    r = max(r, x);
    for (int i = 0; i < 4; i++){
        int tx = x + dx[i], ty = y + dy[i];
        if (tx <= 0 || ty <= 0 || tx > m || ty > n) continue;
        if (board[ty][tx] != '.'){
            dfs(tx, ty, board, n, m);
        }
    }
}
 
vector<int> dp_pre, dp_cur;

signed main(){
    cin.tie(0) -> sync_with_stdio(0);
    int n, m;
    cin >> n >> m;
    vector<vector<char>> board(n+1, vector<char>(m+1));
    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= m; j++){
            cin >> board[i][j];
        }
    }
    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= m; j++){
            if (board[i][j] != '.'){
                l = m + 1, r = 0, totalcost = 0;
                dfs(j, i, board, n, m);
                for (int ptr = l; ptr <= r; ptr++){
                    id[l][ptr] += totalcost;
                }
            }
        }
    }
    calculatecostfunction(m);
    
    dp_cur.assign(m+1, 0);
    dp_pre.assign(m+1, 0);
    for (int i = 1; i <= m; i++){
        dp_cur[i] = cost[0][i];
    }
    int mx = 0;
    for (int i = 1; i <= m; i++){
        mx = max(mx, dp_cur[i]);
    }
    cout << mx << endl;
    for (int i = 2; i <= m; i++){
        dp_pre = dp_cur;
        dp_cur.assign(m+1, 0);
        for (int j = 1; j <= m; j++){
            for (int k = 1; k <= j; k++){
                dp_cur[j] = max(dp_cur[j], cost[k][j] + dp_pre[k]);
            }
        }
        mx = 0;
        for (int j = 1; j <= m; j++){
            mx = max(mx, dp_cur[j]);
        }
        cout << mx << endl;
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...