T3000

Untitled

Mar 19th, 2022
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. using ll = long long;
  5.  
  6. int main()
  7. {
  8.     ll n;
  9.     cin >> n;
  10.     vector<ll> z1(n);
  11.     for (int i; i < n; i++)
  12.     {
  13.         cin >> z1.at(i);
  14.     }
  15.     sort(z1.begin(), z1.end());
  16.     vector<pair<ll, ll>> z2;
  17.     pair<ll, ll> x;
  18.     ll c;
  19.     bool cons = false;
  20.     for (int i = 0; i < n - 1; i++)
  21.     {
  22.         if (z1.at(i) == z1.at(i + 1) - 1)
  23.         {
  24.             if (cons == false)
  25.             {
  26.                 x.first = i;
  27.                 cons = true;
  28.                 c = 2;
  29.             }
  30.             else
  31.             {
  32.                 c += 1;
  33.             }
  34.         }
  35.         else
  36.         {
  37.             if (c > 2)
  38.             {
  39.                 cons = false;
  40.                 x.second = i;
  41.                 z2.push_back(x);
  42.             }
  43.         }
  44.     }
  45.     if (c > 2)
  46.     {
  47.         cons = false;
  48.         x.second = n - 1;
  49.         z2.push_back(x);
  50.     }
  51.     if (z2.size() == 0)
  52.     {
  53.         for (int i = 0; i < n; i++)
  54.         {
  55.             cout << z1.at(i) << " ";
  56.         }
  57.     }
  58.     else
  59.     {
  60.         for (int i = 0, h = 0; i < n; i++, h++)
  61.         {
  62.             if (h == z2.size())
  63.             {
  64.                 h = z2.size() - 1;
  65.             }
  66.             while (i != z2.at(h).first)
  67.             {
  68.                 cout << z1.at(i) << " ";
  69.                 i += 1;
  70.                 if (i == n)
  71.                 {
  72.                     return 0;
  73.                 }
  74.             }
  75.  
  76.             if (i != n - 1)
  77.             {
  78.                 cout << z1.at(z2.at(h).first) << '-' << z1.at(z2.at(h).second) << " ";
  79.                 i += z2.at(h).second - z2.at(h).first;
  80.                 if (h < z2.size() - 1)
  81.                 {
  82.                     h + 1;
  83.                 }
  84.             }
  85.         }
  86.     }
  87.     cout << endl;
  88.     return 0;
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment