Submission #239379

# Submission time Handle Problem Language Result Execution time Memory
239379 2020-06-15T12:59:42 Z DavidDamian Raisins (IOI09_raisins) C++11
100 / 100
219 ms 15736 KB
#include <bits/stdc++.h>
using namespace std;
int n,m;
///Dynamic Programming
///Bottom-Up
///Determine the minimum number of raisins needed to pay (sums)
int A[55][55];
int sum[55][55];
int memo[55][55][55][55];
int getSum(int i,int j,int a,int b)
{
    int total=sum[j][b]-sum[j][a-1]-sum[i-1][b]+sum[i-1][a-1];
    return total;
}
int MinCost(int i,int j,int a,int b)
{
    if(i==j && a==b)
        return 0;
    if(memo[i][j][a][b]==-1){
        int minimum=INT_MAX;
        for(int i_nxt=i;i_nxt<j;i_nxt++){
            minimum=min(minimum,MinCost(i,i_nxt,a,b)
                        +MinCost(i_nxt+1,j,a,b)
                        +getSum(i,j,a,b));
        }
        for(int a_nxt=a;a_nxt<b;a_nxt++){
            minimum=min(minimum,MinCost(i,j,a,a_nxt)
                        +MinCost(i,j,a_nxt+1,b)
                        +getSum(i,j,a,b));
        }
        memo[i][j][a][b]=minimum;
    }
    return memo[i][j][a][b];
}
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>A[i][j];
            sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+A[i][j];
        }
    }
    for(int szRows=1;szRows<=n;szRows++){
        for(int szCols=1;szCols<=m;szCols++){
            for(int i=1;i<=n-szRows+1;i++){
                int j=i+szRows-1;
                for(int a=1;a<=m-szCols+1;a++){
                    int b=a+szCols-1;
                    if(i==j && a==b){
                        memo[i][j][a][b]=0;
                        continue;
                    }
                    memo[i][j][a][b]=INT_MAX;
                    for(int nxt_i=i;nxt_i<j;nxt_i++){
                        memo[i][j][a][b]=min(memo[i][j][a][b],
                                             memo[i][nxt_i][a][b]
                                             +memo[nxt_i+1][j][a][b]
                                             +getSum(i,j,a,b));
                    }
                    for(int nxt_a=a;nxt_a<b;nxt_a++){
                        memo[i][j][a][b]=min(memo[i][j][a][b],
                                             memo[i][j][a][nxt_a]
                                             +memo[i][j][nxt_a+1][b]
                                             +getSum(i,j,a,b));
                    }
                }
            }
        }
    }
    cout<<memo[1][n][1][m]<<'\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 4 ms 512 KB Output is correct
6 Correct 5 ms 768 KB Output is correct
7 Correct 5 ms 1024 KB Output is correct
8 Correct 6 ms 1920 KB Output is correct
9 Correct 8 ms 2944 KB Output is correct
10 Correct 9 ms 3456 KB Output is correct
11 Correct 8 ms 2560 KB Output is correct
12 Correct 19 ms 6016 KB Output is correct
13 Correct 30 ms 7544 KB Output is correct
14 Correct 10 ms 2944 KB Output is correct
15 Correct 41 ms 8568 KB Output is correct
16 Correct 9 ms 5248 KB Output is correct
17 Correct 21 ms 7936 KB Output is correct
18 Correct 114 ms 13816 KB Output is correct
19 Correct 171 ms 14456 KB Output is correct
20 Correct 219 ms 15736 KB Output is correct