Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include<vector>
  4. using namespace std;
  5. #define ll long long
  6. #define ii long long
  7. #define vec vector<ll>
  8. #define vec_p vector<pair<ll,ll>>
  9. int main() {
  10.     ll n, m;
  11.     cin >> n >> m;
  12.     vec_p A;
  13.     for (ii j = 0; j < n; j++)
  14.         for (ii i = 0; i < m; i++) {
  15.             ll b;
  16.             cin >> b;
  17.             A.push_back({ b, j });
  18.         }
  19.     sort(A.begin(), A.end());
  20.     ll cur = 0;
  21.     vec used(n, 0);
  22.     ll l = 0;
  23.     while (cur != n) {
  24.         if (!used[A[l].second]) cur++;
  25.         used[A[l].second]++;
  26.         l++;
  27.     }
  28.     ll r = 0;
  29.     ll min_el = A[l-1].first-A[r].first;
  30.     ll r_ans = r;
  31.     ll l_ans = l - 1;
  32.     l--;
  33.     ll delta = 1e16;
  34.     ll res = n;
  35.     while (l < n*m) {
  36.  
  37.         while (r <= l && res >= n) {
  38.             used[A[r].second]--;
  39.             if (used[A[r].second] < 1)
  40.                 res--;
  41.             r++;
  42.             if (res == n) {
  43.                 if (abs(A[r].first - A[l].first) < min_el) {
  44.                     min_el = abs(A[r].first - A[l].first);
  45.                     r_ans = r; l_ans = l;
  46.                 }
  47.             }
  48.         }
  49.         if (res == n) {
  50.             if (l < n * m) {
  51.                 if (abs(A[r].first - A[l].first) < min_el) {
  52.                     min_el = abs(A[r].first - A[l].first);
  53.                     //if (min_el == 0)l = n * m - 1;
  54.                     r_ans = r; l_ans = l;
  55.                 }
  56.                 /*if (abs(A[r].first - A[l].first) == min_el && A[r].first < delta) {
  57.                     min_el = abs(A[r].first - A[l].first);
  58.                     if (min_el == 0)l = n * m - 1;
  59.                     r_ans = r; l_ans = l;
  60.                 }*/
  61.             }
  62.         }
  63.         if (l+1 == n * m) break;
  64.         while (l < n*m-1 && res < n) {
  65.             ++l;
  66.             if (used[A[l].second] == 0)
  67.                 res++;
  68.             used[A[l].second]++;
  69.         }
  70.         if (res == n) {
  71.             if (abs(A[r].first - A[l].first) < min_el) {
  72.                 min_el = abs(A[r].first - A[l].first);
  73.                 //if (min_el == 0)l = n * m - 1;
  74.                 r_ans = r; l_ans = l;
  75.             }
  76.         }
  77.     }
  78.     used.assign(n, 0);
  79.     cur = 0;
  80.     l = r_ans;
  81.     if (l_ans == r_ans) {
  82.         cout << A[l_ans].first;
  83.         return 0;
  84.     }
  85.     while (cur != n) {
  86.         if (!used[A[l].second]) { cout << A[l].first << ' '; cur++; }
  87.         used[A[l].second]++; l++;
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement