Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <cstdio>
- #include <cstdlib>
- using namespace std;
- struct sort_net
- {
- int n;
- vector<vector<pair<int, int> > > sn;
- sort_net()
- {
- }
- sort_net(int n, vector<vector<pair<int, int> > > sn)
- {
- this->n = n;
- this->sn = sn;
- }
- bool check(vector<int> &v)
- {
- for (int i = 0; i < sn.size(); i++)
- {
- for (int j = 0; j < sn[i].size(); j++)
- {
- if (v[sn[i][j].first] > v[sn[i][j].second])
- swap(v[sn[i][j].first], v[sn[i][j].second]);
- }
- }
- for (int i = 1; i < v.size(); i++)
- if (v[i] < v[i - 1])
- return false;
- return true;
- }
- bool is_sorting()
- {
- vector<int> t(n);
- for (int i = 0; i < (1 << n); i++)
- {
- for (int j = 0; j < n; j++)
- t[j] = (i >> j) & 1;
- if (!check(t))
- return false;
- }
- return true;
- }
- void print()
- {
- int comps = 0;
- int layers = 0;
- for (int i = 0; i < sn.size(); i++)
- {
- comps += sn[i].size();
- if (sn[i].size() > 0)
- layers++;
- }
- cout << n << " " << comps << " " << layers << endl;
- for (int i = 0; i < sn.size(); i++)
- {
- if (sn[i].size() == 0)
- continue;
- printf("%d ", sn[i].size());
- for (int j = 0; j < sn[i].size(); j++)
- printf("%d %d ", sn[i][j].first + 1, sn[i][j].second + 1);
- printf("\n");
- }
- }
- };
- vector<vector<pair<int, int> > > sn;
- int build_merger(vector<int> v, int d)
- {
- if (v.size() <= 1)
- return d;
- if (v.size() == 2)
- {
- sn[d].push_back(make_pair(v[0], v[1]));
- return d + 1;
- }
- vector<int> odd;
- vector<int> even;
- int sz = v.size();
- for (int i = 0; i < sz / 2; i++)
- {
- if (i % 2 == 0)
- even.push_back(v[i]);
- else
- odd.push_back(v[i]);
- }
- for (int i = sz / 2; i < sz; i++)
- {
- if (i % 2 == 0)
- even.push_back(v[i]);
- else
- odd.push_back(v[i]);
- }
- int d1 = build_merger(odd, d);
- int d2 = build_merger(even, d);
- int md = max(d1, d2);
- for (int i = 1; i < v.size(); i += 2)
- {
- if (i + 1 < v.size())
- {
- sn[md].push_back(make_pair(v[i], v[i + 1]));
- }
- }
- return md + 1;
- }
- int build_sorter(int l, int r, int d)//âåðíåò ãëóáèíó
- {
- if (l + 1 > r)
- return d;
- if (l + 1 == r)
- {
- sn[d].push_back(make_pair(l, r));
- return d + 1;
- }
- int sz = r - l + 1;
- int d1 = build_sorter(l, l + sz / 2 - 1, d);
- int d2 = build_sorter(l + sz / 2, r, d);
- int md = max(d1, d2);
- vector<int> v;
- for (int i = l; i <= r; i++)
- v.push_back(i);
- int rd = build_merger(v, md);
- return rd;
- }
- void build_net(int n)
- {
- sn.clear();
- sn.resize(20);
- int p2 = 1;
- while (p2 < n)
- p2 *= 2;
- build_sorter(0, p2 - 1, 0);
- while (sn.size() > 0 && sn.back().empty())
- sn.pop_back();
- for (int i = 0; i < sn.size(); i++)
- {
- vector<pair<int, int> > filt;
- for (int j = 0; j < sn[i].size(); j++)
- {
- if (sn[i][j].first < n && sn[i][j].second < n)
- filt.push_back(sn[i][j]);
- }
- sn[i] = filt;
- }
- sort_net(n, sn).print();
- }
- bool test(int n)
- {
- sn.clear();
- sn.resize(20);
- int p2 = 1;
- while (p2 < n)
- p2 *= 2;
- build_sorter(0, p2 - 1, 0);
- while (sn.size() > 0 && sn.back().empty())
- sn.pop_back();
- for (int i = 0; i < sn.size(); i++)
- {
- vector<pair<int, int> > filt;
- for (int j = 0; j < sn[i].size(); j++)
- {
- if (sn[i][j].first < n && sn[i][j].second < n)
- filt.push_back(sn[i][j]);
- }
- sn[i] = filt;
- }
- sort_net checker(n, sn);
- if (checker.is_sorting())
- return true;
- else
- return false;
- }
- int main()
- {
- /*
- for (int i = 1; i <= 16; i++)
- {
- if (!test(i))
- cerr << "i = " << i << " : PICHALKA" << endl;
- }
- return 0;
- sn.resize(20);
- */
- freopen("netbuild.in", "r", stdin);
- freopen("netbuild.out", "w", stdout);
- int n;
- cin >> n;
- build_net(n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement