0

how to speed up my program?

my task: 1<=k<=n<=10, time 1 sec Print all partitions of an n-element set into k unordered sets. Partitions can be output in any order. Within a partition, sets can be displayed in any order. Within the set, numbers must be displayed in ascending order. Follow the format from the example.

example: example

my solve:

#include <iostream>
#include <vector>
//#pragma GCC optimize ("O3")

using namespace std;
int n;

int num(vector<vector<int>> sets) {
  int res = 0;
  for (int i = 0; i < sets.size(); i++)
    for (int j = 0; j < sets[i].size(); j++)
      if (sets[i][j] != 0)
        res++;
  return res;
}

bool testSets(vector<vector<int>> sets) {
  for (int i = 0; i < sets.size(); i++) {
    if (sets[i].size() == 0) {
      return false;
    } else {
      for (int j = 0; j < sets[i].size() - 1; j++)
        if (sets[i][j] >= sets[i][j + 1])
          return false;
    }
  }
  if (num(sets) == n)
    return true;
  else
    return false;
}

void out(vector<vector<int>> a) {
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < a[i].size(); j++)
      cout << a[i][j] << " ";
    cout << endl;
  }
}

void func(vector<vector<int>> sets, vector<int> a, vector<bool> used) {
  if (num(sets) == n) {
    if (testSets(sets)) {
      out(sets);
      cout << endl;
    } else {
      return;
    }
  } else {
    int i, x;
    for (i = 0; i < used.size(); i++) {
      if (used[i] == false)
        break;
    }
    if (i == used.size())
      i--;

    for (x = 0; x < sets.size(); x++) {
      if (sets[x].size() == 0)
        break;
    }
    if (x == sets.size())
      x--;

    used[i] = true;
    for (int j = 0; j <= x; j++) {
      sets[j].push_back(a[i]);
      //out(sets);
      func(sets, a, used);
      sets[j].pop_back();
    }
    used[i] = false;
  }
}

int main() {
  int k;
  cin >> n >> k;
  vector<int> a(n);
  vector<vector<int>> sets;
  vector<bool> used(n, false);

  for (int i = 0; i < a.size(); i++)
    a[i] = i + 1;
  sets.resize(k);

  func(sets, a, used);

  return 0;
}```

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
taburetca
  • 31
  • 6
  • Did you profile it? Where does it spend most of the time? – Ted Lyngmo Jan 05 '22 at 00:19
  • 2
    At the start of `func` you call `num` which loops over all elements every time. That seems like a bad idea. – Charles Jan 05 '22 at 00:53
  • @TedLyngmo no, I don't have access to tests, and I could not come up with a test that would make her work for a long time. So i don't know when it works slow, but i now, that in some test it too slow – taburetca Jan 05 '22 at 01:42
  • 2
    @taburetca You are passing `std::vector` by value everywhere. You should pass them by either reference or const reference, not by value. The second thing has to do with your post -- post the data here, not on an external site, and not as an image. – PaulMcKenzie Jan 05 '22 at 02:02

1 Answers1

0

Thanks every one

removed the num function, added the sum variable to func, which increase by 1 when pushing, and decrease it by 1 when pop

taburetca
  • 31
  • 6
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 05 '22 at 14:33