|
| 1 | +#include<bits/stdc++.h> |
| 2 | +using namespace std; |
| 3 | +#define ll long long |
| 4 | +#define ld long double |
| 5 | +#define rep(i,a,b) for(ll i=a;i<b;i++) |
| 6 | +#define repp(i,a,b) for(ll i=a;i<=b;i++) |
| 7 | +#define rrep(i,a,b) for(ll i=a;i>=b;i--) |
| 8 | +#define endl "\n" |
| 9 | + |
| 10 | +ll dp[1001][1001]; |
| 11 | +ll MCM(ll a[], ll i, ll j) { |
| 12 | + /* |
| 13 | + The complete logic |
| 14 | + Take the array: 40 20 30 10 30 |
| 15 | + Feel where to apply MCM. It should be applied where you feel you really don't |
| 16 | + know what the answer is but feel like I have to partition at each point and check |
| 17 | + which of them is the minimum. |
| 18 | + The basic format of all MCM's is similar. Base condition needs to be thought about. |
| 19 | + For the base condition think of the first invalid input. That will be it. |
| 20 | + Now talking about the for loop. |
| 21 | + In this we check for each k from i to j-1. |
| 22 | + Selecting i and j at the starting is also very crucial. It can be selected by taking |
| 23 | + an example and trial and error. |
| 24 | + In this case if you initially take i to be 0, we get an invalid matrix as, |
| 25 | + general matrix Ai=a[i-1]*a[i]. So i needs to be 1. Similarly j=n-1. |
| 26 | + In the loop we partition at each k and find the smallest answer. |
| 27 | + Now how does the last a[i - 1] * a[k] * a[j] thing come. |
| 28 | + This comes because say we have, ((AB)(CD)). |
| 29 | + After solving the inner braces we get (EF). It's answer should also be added to the |
| 30 | + final ans. |
| 31 | + How to find it? |
| 32 | + ->Observe A starts at index i and ends at k, C starts at k+1 and ends at j. |
| 33 | + So the dimensions are preserved for these entities... Think on this. |
| 34 | + You'll get the hang of it. |
| 35 | + It's OP;) |
| 36 | + |
| 37 | + */ |
| 38 | + if (i >= j) |
| 39 | + return 0LL; |
| 40 | + if (dp[i][j] != -1) |
| 41 | + return dp[i][j]; |
| 42 | + ll ans = LLONG_MAX; |
| 43 | + for (ll k = i; k <= j - 1; k++) { |
| 44 | + ll tempAns; |
| 45 | + tempAns = MCM(a, i, k) + MCM(a, k + 1, j) + a[i - 1] * a[k] * a[j]; |
| 46 | + ans = min(ans, tempAns); |
| 47 | + } |
| 48 | + return dp[i][j] = ans; |
| 49 | +} |
| 50 | +int main() { |
| 51 | + /* |
| 52 | + MATRIX CHAIN MULTIPLICATION( aka MCM) |
| 53 | + Given: |
| 54 | + An integer n denoting the size of the array |
| 55 | + a[i] is the ith array element. |
| 56 | + Let array be: |
| 57 | + 40 20 30 10 30 |
| 58 | + It represents n-1 2D matrices viz. |
| 59 | + A1= 40*20 |
| 60 | + A2=20*30 |
| 61 | + A3=30*10 |
| 62 | + A4=10*30 |
| 63 | + In short Ai= a[i-1]*a[i] size matrix. |
| 64 | + We need to given the minimum cost of multiplying these matrices. |
| 65 | + Cost Eg: |
| 66 | + 40*20 and 20*30 matrix are to be multiplied |
| 67 | + -> Cost= 40*20*30 |
| 68 | + The cost differs by how you multiply the matrices |
| 69 | + Eg: |
| 70 | + Let the matrices be ABCD. |
| 71 | + ((AB)(CD)) can have different cost than ((ABC)D). |
| 72 | + */ |
| 73 | + ll n; cin >> n; |
| 74 | + ll a[n]; |
| 75 | + rep(i, 0, n) cin >> a[i]; |
| 76 | + ll i = 1, j = n - 1; |
| 77 | + memset(dp, -1, sizeof(dp)); |
| 78 | + cout << MCM(a, i, j); |
| 79 | + |
| 80 | + return 0; |
| 81 | +} |
0 commit comments