fork download
  1.  
  2. #include <iostream>
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5.  
  6. vector < vector < int > > finalVec;
  7.  
  8. void printArray(int p[], int n)
  9. {
  10. vector < int > vec;
  11. for (int i = 0; i < n; i++)
  12. vec.push_back(p[i]);
  13. finalVec.push_back(vec);
  14. return;
  15. }
  16.  
  17. void printAllUniqueParts(int n)
  18. {
  19. int p[n]; // An array to store a partition
  20. int k = 0; // Index of last element in a partition
  21. p[k] = n; // Initialize first partition as number itself
  22.  
  23. // This loop first prints current partition, then generates next
  24. // partition. The loop stops when the current partition has all 1s
  25. while (true)
  26. {
  27. // print current partition
  28. printArray(p, k+1);
  29.  
  30. // Generate next partition
  31.  
  32. // Find the rightmost non-one value in p[]. Also, update the
  33. // rem_val so that we know how much value can be accommodated
  34. int rem_val = 0;
  35. while (k >= 0 && p[k] == 1)
  36. {
  37. rem_val += p[k];
  38. k--;
  39. }
  40.  
  41. // if k < 0, all the values are 1 so there are no more partitions
  42. if (k < 0) return;
  43.  
  44. // Decrease the p[k] found above and adjust the rem_val
  45. p[k]--;
  46. rem_val++;
  47.  
  48.  
  49. // If rem_val is more, then the sorted order is violeted. Divide
  50. // rem_val in differnt values of size p[k] and copy these values at
  51. // different positions after p[k]
  52. while (rem_val > p[k])
  53. {
  54. p[k+1] = p[k];
  55. rem_val = rem_val - p[k];
  56. k++;
  57. }
  58.  
  59. // Copy rem_val to next position and increment position
  60. p[k+1] = rem_val;
  61. k++;
  62. }
  63. }
  64.  
  65.  
  66. int main() {
  67. // your code goes here
  68. int n; cin >> n;
  69.  
  70. printAllUniqueParts(n);
  71. cout << "size : " << finalVec.size() << endl;
  72. multiset < int > :: iterator it;
  73.  
  74. int ANS = 0;
  75. vector < int > ansVec;
  76. for (int i = 0; i < finalVec.size(); i++) {
  77. multiset < int > mySet;
  78.  
  79. vector < int > temp = finalVec[i];
  80.  
  81. for (int ii = 0; ii < temp.size(); ii++) {
  82. mySet.insert(temp[ii]);
  83. }
  84.  
  85. int t = n + 4;
  86. int cnt = 0;
  87. while (t --) {
  88. multiset < int > newSet;
  89. newSet.insert(mySet.size());
  90.  
  91. for (it = mySet.begin(); it != mySet.end(); it++) {
  92. if(*it > 1) {
  93. newSet.insert((*it) - 1);
  94. }
  95. }
  96.  
  97. if(newSet == mySet) {
  98. if(cnt > ANS) {
  99. ANS = cnt;
  100. ansVec = temp;
  101. }
  102. break;
  103. }
  104.  
  105. cnt++;
  106. mySet = newSet;
  107. }
  108. }
  109.  
  110. cout << ANS << endl;
  111. for (int i = 0; i < ansVec.size(); i++) cout << ansVec[i] << ' ';
  112. cout << endl;
  113.  
  114. return 0;
  115. }
  116.  
  117.  
  118.  
Success #stdin #stdout 2.31s 11312KB
stdin
45 p[k]--;
stdout
size : 89134
48
38 7