Georgiy1108

Untitled

Feb 9th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int INF = (int)1e9;
  6.  
  7. vector<pair<int, int>> a, b, c;
  8. vector<int> ans(3);
  9. int n, m, k, mn = INF, mx = 0;
  10.    
  11. int f(int a, int b, int c)
  12. {
  13.     return abs(a - b) + abs(a - c) + abs(b - c);
  14. }
  15.  
  16. void check(int i1, int i2, int i3)
  17. {
  18.     int cur = f(a[i1].first, b[i2].first, c[i3].first);
  19.     if(cur < mn)
  20.     {
  21.         ans = {a[i1].second, b[i2].second, c[i3].second};
  22.         mn = cur;
  23.         mx = a[i1].first + b[i2].first + c[i3].first;
  24.     }
  25.     else if(cur == mn && a[i1].first + b[i2].first + c[i3].first > mx)
  26.     {
  27.         ans = {a[i1].second, b[i2].second, c[i3].second};
  28.         mx = a[i1].first + b[i2].first + c[i3].first;
  29.     }
  30. }
  31.  
  32. main()
  33. {
  34.     ios::sync_with_stdio(0);
  35.     cin.tie(0);
  36.     cin >> n >> m >> k;
  37.     a.resize(n);
  38.     b.resize(m);
  39.     c.resize(k);
  40.     for(int i = 0; i < n; i++)
  41.     {
  42.         cin >> a[i].first;
  43.         a[i].second = i;
  44.     }
  45.     for(int i = 0; i < m; i++)
  46.     {
  47.         cin >> b[i].first;
  48.         b[i].second = i;
  49.     }
  50.     for(int i = 0; i < k; i++)
  51.     {
  52.         cin >> c[i].first;
  53.         c[i].second = i;
  54.     }
  55.     sort(a.begin(), a.end());
  56.     sort(b.begin(), b.end());
  57.     sort(c.begin(), c.end());
  58.     int i1 = 0, i2 = 0, i3 = 0;
  59.     while(true)
  60.     {
  61.         if(i1 == n - 1 && i2 == m - 1 && i3 == k - 1)
  62.             break;
  63.         check(i1, i2, i3);
  64.        
  65.         if(i1 != n - 1)
  66.             check(i1 + 1, i2, i3);
  67.        
  68.         if(i2 != m - 1)
  69.             check(i1, i2 + 1, i3);
  70.        
  71.         if(i3 != k - 1)
  72.             check(i1, i2, i3 + 1);
  73.        
  74.         if(i1 == n - 1 && i2 == m - 1)
  75.             ++i3;
  76.         else if(i1 == n - 1 && i3 == k - 1)
  77.             ++i2;
  78.         else if(i2 == m - 1 && i3 == k - 1)
  79.             ++i1;
  80.         else if(i1 == n - 1)
  81.         {
  82.             if(b[i2].first < c[i3].first)
  83.                 ++i2;
  84.             else
  85.                 ++i3;
  86.         }
  87.         else if(i2 == m - 1)
  88.         {
  89.             if(a[i1].first < c[i3].first)
  90.                 ++i1;
  91.             else
  92.                 ++i3;
  93.         }
  94.         else if(i3 == k - 1)
  95.         {
  96.             if(a[i1].first < b[i2].first)
  97.                 ++i1;
  98.             else
  99.                 ++i2;
  100.         }
  101.         else
  102.         {
  103.             int minim = min({a[i1].first, b[i2].first, c[i3].first});
  104.             if(a[i1].first == minim)
  105.                 ++i1;
  106.             else if(b[i2].first == minim)
  107.                 ++i2;
  108.             else
  109.                 ++i3;
  110.         }
  111.     }
  112.     for(auto i : ans)
  113.         cout << i + 1 << " ";
  114. }
Add Comment
Please, Sign In to add comment