Advertisement
vlatkovski

Raspored [државен '10] (nedovrseno)

Oct 2nd, 2017
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5.     int g, n, m; cin >> g >> n >> m;
  6.     multiset<int> gr;
  7.     for (int i = 0; i < g; ++i) {
  8.         int p; cin >> p;
  9.         _test += p;
  10.         gr.insert(p);
  11.     }
  12.     int broj = 0;
  13.     auto f = gr.begin();
  14.     auto b = prev(gr.end());
  15.     int fv = *f, bv = *b;
  16.     while (true) {
  17.         f = gr.begin();
  18.         b = prev(gr.end());
  19.         fv = *f;
  20.         bv = *b;
  21.         if (fv >= n && bv <= m) {
  22.             break;
  23.         }
  24.         bool changed = false;
  25.         if (fv < n) {
  26.             changed = true;
  27.             int chng = n - fv;
  28.             bv = bv - chng;
  29.             fv = n;
  30.             broj += chng;
  31.             gr.erase(f);
  32.             gr.erase(b);
  33.             gr.insert(fv);
  34.             gr.insert(bv);
  35. //          cout << "fv<n chng=" << chng << " | ";
  36. //          int _s = 0;
  37. //          for (auto it : gr) {
  38. //             cout << it << " "; _s += it;
  39. //          }
  40. //          cout << "(" << _s << ")" << endl;
  41.         }
  42.         f = gr.begin();
  43.         b = prev(gr.end());
  44.         fv = *f;
  45.         bv = *b;
  46.         if (bv > m) {
  47.             changed = true;
  48.             int chng = bv - m;
  49.             fv = fv + chng;
  50.             bv = m;
  51.             gr.erase(f);
  52.             gr.erase(b);
  53.             gr.insert(fv);
  54.             gr.insert(bv);
  55. //            cout << "bv>m chng=" << chng << " | ";
  56. //            int _s = 0;
  57. //            for (auto it : gr) {
  58. //                cout << it << " "; _s += it;
  59. //            }
  60. //            cout << "(" << _s << ")" << endl;
  61.         }
  62.         if (!changed) {
  63.             broj = -1;
  64.             break;
  65.         }
  66.     }
  67.     cout << broj << endl;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement