Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using ll = long long;
- int main()
- {
- ll n;
- cin >> n;
- vector<ll> z1(n);
- for (int i; i < n; i++)
- {
- cin >> z1.at(i);
- }
- sort(z1.begin(), z1.end());
- vector<pair<ll, ll>> z2;
- pair<ll, ll> x;
- ll c;
- bool cons = false;
- for (int i = 0; i < n - 1; i++)
- {
- if (z1.at(i) == z1.at(i + 1) - 1)
- {
- if (cons == false)
- {
- x.first = i;
- cons = true;
- c = 2;
- }
- else
- {
- c += 1;
- }
- }
- else
- {
- if (c > 2)
- {
- cons = false;
- x.second = i;
- z2.push_back(x);
- }
- }
- }
- if (c > 2)
- {
- cons = false;
- x.second = n - 1;
- z2.push_back(x);
- }
- if (z2.size() == 0)
- {
- for (int i = 0; i < n; i++)
- {
- cout << z1.at(i) << " ";
- }
- }
- else
- {
- for (int i = 0, h = 0; i < n; i++, h++)
- {
- if (h == z2.size())
- {
- h = z2.size() - 1;
- }
- while (i != z2.at(h).first)
- {
- cout << z1.at(i) << " ";
- i += 1;
- if (i == n)
- {
- return 0;
- }
- }
- if (i != n - 1)
- {
- cout << z1.at(z2.at(h).first) << '-' << z1.at(z2.at(h).second) << " ";
- i += z2.at(h).second - z2.at(h).first;
- if (h < z2.size() - 1)
- {
- h + 1;
- }
- }
- }
- }
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment