Author : Dipu Kumar Mohanto
CSE, Batch - 6
BRUR.
Problem Statement : 10003 - Cutting Sticks
Source : UVA Online Judge
Category : Dynamic Programing
Algorithm : Matrix Chain Multiplication
Verdict : Accepted
- #include <bits/stdc++.h>
-
- using namespace std;
-
- #define ll long long
-
- static const int maxn = 1000 + 5;
-
- ll dp[maxn][maxn];
- bool memoize[maxn][maxn];
- int cutt[maxn];
- int cutPoints;
-
- ll solve(int start, int endd)
- {
- ll &ret = dp[start][endd];
- bool &mem = memoize[start][endd];
- if (mem == 1) return ret;
- mem = 1;
- bool canCut = false;
- ll mini = 123456789;
- for (int i = 0; i < cutPoints; i++)
- {
- if (cutt[i] > start && cutt[i] < endd)
- {
- ll go = (endd - start) + solve(start, cutt[i]) + solve(cutt[i], endd);
- if (go < mini) mini = go;
- canCut = true;
- }
- }
- if (canCut == false) mini = 0;
- return ret = mini;
- }
-
- int main()
- {
- int length;
- while (scanf("%d", &length)==1)
- {
- if (length == 0) break;
- scanf("%d", &cutPoints);
- for (int i = 0; i < cutPoints; i++) scanf("%d", &cutt[i]);
- memset(memoize, 0, sizeof memoize);
- ll ans = solve(0, length);
- printf("The minimum cutting is %lld.\n", ans);
- }
- }
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.