Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include<vector>
- using namespace std;
- #define ll long long
- #define ii long long
- #define vec vector<ll>
- #define vec_p vector<pair<ll,ll>>
- int main() {
- ll n, m;
- cin >> n >> m;
- vec_p A;
- for (ii j = 0; j < n; j++)
- for (ii i = 0; i < m; i++) {
- ll b;
- cin >> b;
- A.push_back({ b, j });
- }
- sort(A.begin(), A.end());
- ll cur = 0;
- vec used(n, 0);
- ll l = 0;
- while (cur != n) {
- if (!used[A[l].second]) cur++;
- used[A[l].second]++;
- l++;
- }
- ll r = 0;
- ll min_el = A[l-1].first-A[r].first;
- ll r_ans = r;
- ll l_ans = l - 1;
- l--;
- ll delta = 1e16;
- ll res = n;
- while (l < n*m) {
- while (r <= l && res >= n) {
- used[A[r].second]--;
- if (used[A[r].second] < 1)
- res--;
- r++;
- if (res == n) {
- if (abs(A[r].first - A[l].first) < min_el) {
- min_el = abs(A[r].first - A[l].first);
- r_ans = r; l_ans = l;
- }
- }
- }
- if (res == n) {
- if (l < n * m) {
- if (abs(A[r].first - A[l].first) < min_el) {
- min_el = abs(A[r].first - A[l].first);
- //if (min_el == 0)l = n * m - 1;
- r_ans = r; l_ans = l;
- }
- /*if (abs(A[r].first - A[l].first) == min_el && A[r].first < delta) {
- min_el = abs(A[r].first - A[l].first);
- if (min_el == 0)l = n * m - 1;
- r_ans = r; l_ans = l;
- }*/
- }
- }
- if (l+1 == n * m) break;
- while (l < n*m-1 && res < n) {
- ++l;
- if (used[A[l].second] == 0)
- res++;
- used[A[l].second]++;
- }
- if (res == n) {
- if (abs(A[r].first - A[l].first) < min_el) {
- min_el = abs(A[r].first - A[l].first);
- //if (min_el == 0)l = n * m - 1;
- r_ans = r; l_ans = l;
- }
- }
- }
- used.assign(n, 0);
- cur = 0;
- l = r_ans;
- if (l_ans == r_ans) {
- cout << A[l_ans].first;
- return 0;
- }
- while (cur != n) {
- if (!used[A[l].second]) { cout << A[l].first << ' '; cur++; }
- used[A[l].second]++; l++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement