pranavsindura

UVA 662 Fast Food

Jan 18th, 2021
833
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.56 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define ll long long int
  3. #define ld long double
  4. #define pi pair<int,int>
  5. #define all(x) x.begin(), x.end()
  6. #define allr(x) x.rbegin(), x.rend()
  7. #define sz(x) ((int)x.size())
  8. #define ln(x) ((int)x.length())
  9. #define mp make_pair
  10. #define pb push_back
  11. #define ff first
  12. #define ss second
  13. #define endl '\n'
  14. #define dbg(x) cout<<#x<<" "<<x<<endl
  15. #define clr(x,v) memset(x, v, sizeof(x))
  16. #define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
  17. using namespace std;
  18. const double eps = 1e-9;
  19. const double PI = acos(-1.0);
  20. const ll mod = 1e9 + 7;
  21. const int MAXN = 1e6 + 5;
  22. int dp[35][205];
  23. int pos[205];
  24. int cost[205][205];
  25.  
  26. int solve(int k, int n)
  27. {
  28.     if(~dp[k][n])
  29.         return dp[k][n];
  30.     int &ret = dp[k][n];
  31.     if(k == 1)
  32.     {
  33.         return ret = cost[1][n];
  34.     }
  35.     else
  36.     {
  37.         ret = 1e7;
  38.         for(int j = k - 1; j <= n - 1; j++)
  39.         {
  40.             ret = min(ret, solve(k - 1, j) + cost[j + 1][n]);
  41.         }
  42.         return ret;
  43.     }
  44. }
  45. vector<pi> path;
  46. void trace(int k, int n)
  47. {
  48.     if(k == 1)
  49.     {
  50.         path.pb({1, n});
  51.     }
  52.     else
  53.     {
  54.         int ans = solve(k, n);
  55.         for(int j = k - 1; j <= n - 1; j++)
  56.         {
  57.             int me = solve(k - 1, j) + cost[j + 1][n];
  58.             if(me == ans)
  59.             {
  60.                 path.pb({j + 1, n});
  61.                 trace(k - 1, j);
  62.                 return;
  63.             }
  64.         }
  65.     }
  66. }
  67.  
  68. void cp()
  69. {
  70.     int n, k;
  71.     int chain = 1;
  72.     while(cin >> n >> k && n + k)
  73.     {
  74.         // Position on Number Line
  75.         // int pos[n+1];
  76.         for(int i = 0; i < n; i++)
  77.             cin >> pos[i + 1];
  78.  
  79.         // cost[i][j]
  80.         // If i want to build a warehouse that covers all restaurants in range [i, j],
  81.         // I should build the warehouse at their median at pos[(i+j)/2]
  82.         // cost[i][j] is the sum of the distances of all restaurants in range [i, j] from pos[(i+j)/2]
  83.         // int cost[n + 1][n + 1];
  84.         clr(cost, 0);
  85.         for(int i = 1; i <= n; i++)
  86.         {
  87.             for(int j = i; j <= n; j++)
  88.             {
  89.                 for(int m = i; m <= j; m++)
  90.                 {
  91.                     cost[i][j] += abs(pos[m] - pos[(i + j) / 2]);
  92.                 }
  93.             }
  94.         }
  95.  
  96.         // dp[m][i]
  97.         // Summing up the costs
  98.         // Making the mth warehouse such that all restaurants from 1 to i get covered
  99.         // Transition -
  100.         // Let us find what is the cost of making m-1 restaurants
  101.         // There can be many as you build m-1 restaurants in all ranges from [m-1, i-1]
  102.         // m-1 can be built consecutively covering only first m-1 restaurants
  103.         // m-1 can be built covering first m restaurants ....
  104.         // m-1 can be built covering first i-2 restaurants....
  105.         // m-1 can be built covering first i-1 restaurants....
  106.         // We thus want to pick the minimum
  107.         // Now let's say we built m-1 restaurants upto position j => [m-1, i-1]
  108.         // Now we will add a new restaurant at position i, What will be the cost?
  109.         // dp[m-1][j] + cost[j+1][i]
  110.         // cost of covering restaurants from [j+1, i] is added
  111.         // Base Case
  112.         // If we make only 1 warehouse covering first i restaurants
  113.         // Its dp[1][i] = cost[1][i]
  114.         // int dp[k + 1][n + 1];
  115.         clr(dp, -1);
  116.         // for(int i = 1; i <= n; i++)
  117.         //     dp[1][i] = cost[1][i];
  118.         // for(int m = 2; m <= k; m++)
  119.         // {
  120.         //     for(int i = m; i <= n; i++)
  121.         //     {
  122.         //         dp[m][i] = 1e7;
  123.         //         for(int j = m - 1; j <= i - 1; j++)
  124.         //         {
  125.         //             dp[m][i] = min(dp[m][i], dp[m - 1][j] + cost[j + 1][i]);
  126.         //         }
  127.         //     }
  128.         // }
  129.         // cout << dp[k][n] << endl;
  130.         int ans = solve(k, n);
  131.         path.clear();
  132.         trace(k, n);
  133.         cout << "Chain " << chain++ << endl;
  134.         reverse(all(path));
  135.         for(int i = 0; i < sz(path); i++)
  136.         {
  137.             cout << "Depot " << i + 1 << " at restaurant " << (path[i].ff + path[i].ss) / 2;
  138.  
  139.             if(path[i].ff == path[i].ss)
  140.                 cout << " serves restaurant " << path[i].ff << endl;
  141.             else
  142.                 cout << " serves restaurants " << path[i].ff << " to " << path[i].ss << endl;
  143.         }
  144.         cout << "Total distance sum = " << ans << endl;
  145.         cout << endl;
  146.     }
  147.  
  148. }
  149.  
  150. int main()
  151. {
  152.     FASTIO;
  153.     int t;
  154.     t = 1;
  155.     // cin >> t;
  156.     while(t--)
  157.     {
  158.         cp();
  159.     }
  160.     return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment