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