Skip to content

Instantly share code, notes, and snippets.

@henrybear327
Created April 10, 2018 13:16
Show Gist options
  • Save henrybear327/53610debb12f30d3215b05e333145966 to your computer and use it in GitHub Desktop.
Save henrybear327/53610debb12f30d3215b05e333145966 to your computer and use it in GitHub Desktop.
uva10003.cpp
#include <bits/stdc++.h>
using namespace std;
int dp[55][55];
int dfs(int inp[], int n, int l, int r) // (l, r)
{
if (r - l <= 1)
return 0;
if (dp[l][r] != -1)
return dp[l][r];
int mn = INT_MAX;
for (int i = l + 1; i < r; i++) {
// 0 ...... l
mn = min(mn, inp[r] - inp[l] + dfs(inp, n, l, i) + dfs(inp, n, i, r));
}
return dp[l][r] = mn;
}
int main()
{
int l;
while (scanf("%d", &l) == 1 && l) {
int n;
scanf("%d", &n);
int inp[n + 2];
// 0 ...... l
inp[0] = 0;
inp[n + 1] = l;
for (int i = 1; i <= n; i++)
scanf("%d", &inp[i]);
memset(dp, -1, sizeof(dp));
printf("The minimum cutting is %d.\n", dfs(inp, n + 2, 0, n + 1));
}
return 0;
}
@Alb3rt0Williams
Copy link

Alb3rt0Williams commented Apr 14, 2018

What do you want to do with this? I don't understand this code. Can you help me to understand it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment