1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <cstdio>
#include <iostream>
#include <set>
#include <algorithm>
#include <iomanip>
#include <cassert>

#define REP(i, n) for(int i = 0; i < n; i++)
#define FWD(i, a, b) for(int i = a; i < b; i++)
#define ALL(u) (u).begin(), (u).end()

using namespace std;

typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long LL;
typedef uint uint;

const int INF = 1000000000;

class Sol {
    public:
    int n, k;
    VVI input;
    LL radius;

    LL get_dist(VI& a, VI& b) {
        LL res = 0;
        REP(i, n) {
            res += llabs(a[i] - b[i]);
        }
        return res;
    }

    int int_cmp(int first, int second) {
        if (first == second) return 0;
        if (first < second) return 1;
        return -1;
    }

    void midpoint2(VI& a, VI& b, VVI& res) {
        res.clear();
        assert((int)a.size() == n);
        assert((int)b.size() == n);
        VI mp(a.size());
        int adv = 0;
        assert((-4) / 2 == -2);
        assert((-5) / 2 == -2);
        int alt_i = -1, alt_val = 0;
        REP(i, (int)a.size()) {
            if (abs(a[i] - b[i]) % 2 == 0) {
                mp[i] = (a[i] + b[i]) / 2;
            } else if (adv == 0) {
                alt_i = i;
                alt_val = (a[i] + int_cmp(a[i], b[i]) + b[i]) / 2;
                mp[i] = (a[i] + int_cmp(b[i], a[i]) + b[i]) / 2;
                adv = 1;
            } else {
                mp[i] = (a[i] + int_cmp(a[i], b[i]) + b[i]) / 2;
                adv = 0;
            }
        }
        if (adv == 0) {
            res.push_back(mp);
        } else {
            assert(alt_i != -1);
            res.push_back(mp);
            mp[alt_i] = alt_val;
            res.push_back(mp);
        }
        LL target_dist = ceildiv(get_dist(a, b), 2);
        for (auto& elem : res) { //TODO : remove expensive asserts
            assert(get_dist(elem, a) <= target_dist and get_dist(elem, b) <= target_dist);
        }
    }

    LL ceildiv(LL a, LL b) {
        return (a + b - 1) / b;
    }

    bool cmp_sign(int a, int b) {
        assert(a != 0 and b != 0);
        if (get_sign(a) == get_sign(b)) return true;
        return false;
    }

    int get_sign(int val) {
        assert(val != 0);
        return val / abs(val);
    }

    int move_to(int cur, int target, int val) {
        assert(val > 0);
        assert(cur != target);
        return cur + get_sign(target - cur) * val;
    }

    void manage_coord(vector<int> positions[], int coord, int aval, int bval, int cval, int hval) {
        if (aval == bval) return;
        if (hval == cval) return;
        if (cmp_sign(cval - hval, aval - bval) and hval != aval) {
            positions[0].push_back(coord);
        } else if ((not cmp_sign(cval - hval, aval - bval)) and hval != bval) {
            positions[1].push_back(coord);
        }
    }

    bool move_center_to(VI& point) {
        vector<int> positions[2];
        VI& a = input[0], &b = input[1], &c = input[2];
        for(int i = 0; i < n; i++) {
            manage_coord(positions, i, a[i], b[i], c[i], point[i]);
        }
        while(positions[0].size() > 0 and positions[1].size() > 0) {
            //printf("positions[0]: %d, positions[1]: %d\n", positions[0].size(), positions[1].size());
            int posa = positions[0].back();
            positions[0].pop_back();
            int posb = positions[1].back();
            positions[1].pop_back();
            int step_size = min(abs(point[posa] - c[posa]), abs(point[posb] - c[posb]));
            step_size = min(step_size, min(abs(point[posa] - a[posa]), abs(point[posb] - b[posb])));
            //printf("step_size: %d\n", step_size);
            point[posa] = move_to(point[posa], c[posa], step_size);
            point[posb] = move_to(point[posb], c[posb], step_size);
            manage_coord(positions, posa, a[posa], b[posa], c[posa], point[posa]);
            manage_coord(positions, posb, a[posb], b[posb], c[posb], point[posb]);
        }
        return get_dist(point, c) <= radius;
    }

    VI process3() {
        VI result(n);
        VVI start_points;
        midpoint2(input[0], input[1], start_points);
        for (VI& hyper_plane_point : start_points) {
            if (move_center_to(hyper_plane_point)) {
                return hyper_plane_point;
            }
        }
        assert(false);
        return VI();
    }

    void sol() {
        scanf("%d %d", &n, &k);
        input.resize(k);
        REP(i, k) {
            input[i].resize(n);
            REP(j, n) {
                scanf("%d ", &input[i][j]);
            }
        }
        if (k == 2) {
            VVI res;
            midpoint2(input[0], input[1], res);
            for (auto val : res[0]) {
                printf("%d ", val);
            }
            printf("\n");
            return;
        }
        LL d01 = get_dist(input[0], input[1]);
        LL d02 = get_dist(input[0], input[2]);
        LL d12 = get_dist(input[1], input[2]);
        radius = ceildiv(max(d01, max(d02, d12)), 2);
        assert(k == 3);
        VI result;
        if (d01 >= d02 and d01 >= d12) {
            result = process3();
        } else if (d02 >= d01 and d02 >= d12) {
            swap(input[2], input[1]);
            result = process3();
        } else {
            swap(input[0], input[2]);
            result = process3();
        }
        for (auto val : result) {
            printf("%d ", val);
        }
        printf("\n");

        /*
        LL radius = ceildiv(far_dist, 2);
        LL sign[] = {1, -1};
        printf("radius expected: %lld\n", radius);
        for(int i = 0; i < k; i++) {
            for(int j = 0; j < n; j++) {
                for(int s = 0; s < 2; s++) {
                    VI p = input[i];
                    p[j] += sign[s] * radius;
                    bool ok = true;
                    printf("candidate:\n");
                    for(int l = 0; l < k; l++) {
                        printf("ex dist: %lld\n", get_dist(input[l], p));
                        if (get_dist(input[l], p) > radius) {
                            ok = false;
                            break;
                        }
                    }
                    if (ok) {
                        printf("TAK\n");
                        return;
                    }
                }
            }
        }
        assert(false);
        */
    }
};

int main() {
    Sol s;
    s.sol();
    return 0;
}