Advertisement
Asif_Anwar

UVa 10003 - Cutting Sticks

Apr 10th, 2021
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. #define pb push_back
  5. #define FastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  6. #define F first
  7. #define S second
  8. typedef long long ll;
  9. typedef vector< int > vi;
  10. typedef vector< ll > V;
  11. typedef map<int, int > mp;
  12. #define debug cout << -1 << endl;
  13. #define REP(i, a, b) for(int i=a; i<b; i++)
  14. #define f0r(i, n) for (int i = 0; i < n; ++i)
  15. #define fore(a, x) for (auto& a : x)
  16. #define fori(i, a, b) for (int i = (a); i < (b); ++i)
  17. #define pop pop_back
  18. #define sz(a) (int)a.size()
  19. const ll MOD = 1000000007;
  20. const int INF = INT_MAX;
  21. const int maxN = 2001;
  22.  
  23. int dx[] = {-1, 0, 1, -1, 1, -1, 0, 1};
  24. int dy[] = {-1, -1, -1, 0, 0, 1, 1, 1};
  25.  
  26. int l, n;
  27. int dp[55][55];
  28. int cut[55];
  29. int vis[55][55];
  30. int dpF(int x, int y)
  31. {
  32. int& ans = dp[x][y];
  33. if(vis[x][y]!=-1) return ans;
  34. if(y-x==1) return 0;
  35. for(int i=x+1; i<y; i++) {
  36. int cost = cut[y]-cut[x];
  37. ans = min(ans, dpF(x, i)+dpF(i, y)+cost);
  38. vis[x][y] = 1;
  39. }
  40. return ans;
  41. }
  42.  
  43. void solve()
  44. {
  45. while(cin >> l && l) {
  46. cin >> n;
  47. for(int i=1; i<=n; i++) {
  48. cin >> cut[i];
  49. }
  50. cut[0] = 0;
  51. cut[n+1] = l;
  52. memset(vis, -1, sizeof(vis));
  53. memset(dp, 0x3f3f3f, sizeof(dp));
  54. cout << "The minimum cutting is " << dpF(0, n+1) << ".\n";
  55. }
  56. return;
  57. }
  58.  
  59. int main()
  60. {
  61. //FastIO;
  62. //freopen("mowing.in","r",stdin);
  63. //freopen("mowing.out","w",stdout);
  64. int t;
  65. t = 1;
  66. //cin >> t;
  67. //cin.ignore();
  68. while(t--){
  69. solve();
  70. }
  71. return 0;
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement