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