summaryrefslogtreecommitdiffstats
path: root/01-algorithmic_toolbox/02-greedy_algorithms/05-different_summands/different_summands.cpp
blob: fe3686696f5cecae2c78d1f415c8ed761bd0ba8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <vector>

using std::vector;

vector<int> optimal_summands(int n) {
  vector<int> summands;
  int s = 0;
  for (int i = 1; ; i++) {
      s += i;
      if (s == n) {
          summands.push_back(i);
          break;
      }
      if (s > n) {
          s -= (i + i - 1);
          summands[summands.size() - 1] = (n - s);
          break;
      }
      summands.push_back(i);
  }
  return summands;
}

int main() {
  int n;
  std::cin >> n;
  vector<int> summands = optimal_summands(n);
  std::cout << summands.size() << '\n';
  for (size_t i = 0; i < summands.size(); ++i) {
    std::cout << summands[i] << ' ';
  }
  std::cout << '\n';
}