Kali_prasad

aesthetic minimum values

Sep 15th, 2025 (edited)
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. https://www.desiqna.in/11238/aesthetic-permutation-triology-codenation-dynamic-programming
  2.  
  3. Perfect ๐Ÿ‘
  4. Let me give you a **cleaner C++ implementation** with clear comments, that solves the same problem in a much easier-to-follow way.
  5.  
  6. ---
  7.  
  8. ### ๐Ÿ”น Key idea (simplified)
  9.  
  10. * Sort the array `A`.
  11. * We must split the sorted array into `B` subsequences.
  12. * Each subsequence contributes cost = (last - first) of that subsequence.
  13. * So the problem reduces to **partitioning the sorted array into `B` subsequences optimally**.
  14.  
  15. We can solve this with **DP** in a neat way.
  16.  
  17. ---
  18.  
  19. ### ๐Ÿ”น Simplified C++ solution
  20.  
  21. ```cpp
  22. #include <bits/stdc++.h>
  23. using namespace std;
  24. typedef long long ll;
  25.  
  26. int main() {
  27. ios::sync_with_stdio(false);
  28. cin.tie(nullptr);
  29.  
  30. int N, B;
  31. cin >> N >> B;
  32.  
  33. vector<ll> A(N);
  34. for (int i = 0; i < N; i++) cin >> A[i];
  35.  
  36. // Step 1: Sort array
  37. sort(A.begin(), A.end());
  38.  
  39. // Step 2: Precompute prefix differences
  40. vector<ll> diff(N+1, 0);
  41. for (int i = 1; i < N; i++) {
  42. diff[i+1] = diff[i] + (A[i] - A[i-1]);
  43. }
  44.  
  45. // Step 3: Group sizes
  46. int small = N / B; // smaller group size
  47. int large = small + 1; // larger group size
  48. int countLarge = N % B; // number of groups with 'large' size
  49. int countSmall = B - countLarge;
  50.  
  51. // Step 4: DP
  52. // dp[i][j] = min cost using i small groups and j large groups
  53. vector<vector<ll>> dp(countSmall+1, vector<ll>(countLarge+1, 1e18));
  54. dp[0][0] = 0;
  55.  
  56. for (int i = 0; i <= countSmall; i++) {
  57. for (int j = 0; j <= countLarge; j++) {
  58. int used = i*small + j*large;
  59. if (used >= N) continue;
  60.  
  61. // Take one more small group
  62. if (i < countSmall) {
  63. int l = used;
  64. int r = used + small - 1;
  65. ll cost = A[r] - A[l];
  66. dp[i+1][j] = min(dp[i+1][j], dp[i][j] + cost);
  67. }
  68.  
  69. // Take one more large group
  70. if (j < countLarge) {
  71. int l = used;
  72. int r = used + large - 1;
  73. ll cost = A[r] - A[l];
  74. dp[i][j+1] = min(dp[i][j+1], dp[i][j] + cost);
  75. }
  76. }
  77. }
  78.  
  79. cout << dp[countSmall][countLarge] << "\n";
  80. return 0;
  81. }
  82. ```
  83.  
  84. ---
  85.  
  86. ### ๐Ÿ”น Why this is simpler
  87.  
  88. 1. Sorts array first.
  89. 2. Splits into `B` groups.
  90. 3. Uses **clear DP table** โ†’ each entry means "minimum cost using X small groups and Y large groups".
  91. 4. Transition is easy: add next group of size `small` or `large`.
  92.  
  93. ---
  94.  
  95. โœ… This produces the same result as your sirโ€™s code but is **much easier to read and understand**.
  96.  
  97. Do you want me to also **draw a step-by-step dry run** for one of the sample test cases (like `A = [4,3,4,3,2,5], B = 3`) so itโ€™s crystal clear?
  98.  
Advertisement
Add Comment
Please, Sign In to add comment