#include <cstdio> #include <iostream> #include <cstring> #include <vector> #include <algorithm> using namespace std; int ciagi[5][100000]; long long srednie[32]; long long srednie2[32]; long long rozw[100000]; long long pot[100000]; int main() { int n, k; scanf("%d%d", &n, &k); for (int i = 0; i < k; i++) { for (int j = 0; j < n; j++) { scanf("%d", &ciagi[i][j]); } } vector<pair<long long, long long> > elems; for (int i = 0; i < n; i++) { if (i == 0) { for (int j = 0; j < (1 << k); j++) { int c = 0; long long srednia = 0; for (int m = 0; m < k; m++) { if (j && (1 << m)) { srednia += ciagi[m][i]; c++; } } srednie[j] = 0; if (!c) continue; srednia /= c; srednie[j] = srednia; } long long d = 0; long long elem = 0; long long best = 1LL << 60; for (int j = 0; j < (1 << k); j++) { long long worst = -(1LL << 60); for (int o = 0; o < k; o++) { d = abs(ciagi[o][0] - srednie[j]); worst = max(worst, d); } if (worst < best) { best = worst; elem = srednie[j]; } } elems.push_back(make_pair(-1, elem)); } else { for (int j = 0; j < (1 << k); j++) { int c = 0; long long srednia = 0; for (int m = 0; m < k; m++) { if (j && (1 << m)) { srednia += ciagi[m][i]; c++; } } srednie2[j] = 0; if (!c) continue; srednia /= c; srednie2[j] = srednia; } long long elem1, elem2; long long best = 1LL << 60; for (int j = 0; j < (1 << k); j++) { for (int m = 0; m < (1 << k); m++) { long long worst = -(1LL << 60); for (int o = 0; o < k; o++) { long long d = abs(ciagi[o][i - 1] - srednie[j]); d += abs(ciagi[o][i] - srednie2[m]); worst = max(worst, d); } if (worst < best) { best = worst; elem1 = srednie[j]; elem2 = srednie2[m]; } } } memcpy(srednie, srednie2,sizeof(srednie)); elems.push_back(make_pair(elem1, elem2)); } } /*for (int i = 0; i < elems.size(); i++) { printf("%d %d\n", elems[i].first, elems[i].second); }*/ long long best = (1LL << 60); if (elems.size() == 1) { printf("%lld\n", elems[0].second); } else { long long worst = -(1LL << 60); for (int i = 0; i < k; i++) { long long d = 0; for (int j = 0; j + 1 < n; j++) { d += abs(ciagi[i][j] - (elems[j].second + elems[j + 1].first + 1) / 2); } d += abs(ciagi[i][n - 1] - elems[n - 1].second); worst = max(worst, d); } if (worst < best) { best = worst; for (int j = 0; j + 1 < n; j++) { rozw[j] = (elems[j].second + elems[j + 1].first + 1) / 2; } rozw[n - 1] = elems[n - 1].second; } //losuj for (int L = 0; L < 10; L++) { long long worst = -(1LL << 60); for (int j = 0; j + 1 < n; j++) { pot[j] = rand() % 2 ? elems[j].second : elems[j + 1].first; } pot[n - 1] = elems[n - 1].second; for (int i = 0; i < k; i++) { long long d = 0; for (int j = 0; j < n; j++) { d += abs(ciagi[i][j] - pot[j]); } worst = max(worst, d); } if (worst < best) { best = worst; memcpy(rozw, pot, sizeof(rozw)); } } } //best = (1LL << 60); for (int i = 0; i < n; i++) { printf("%d ", rozw[i]); } /* long long d = 0; long long worst = -(1LL << 60); for (int i = 0; i < k; i++) { long long d = 0; for (int j = 0; j < n; j++) { //if (k == 2) //rozw[j] = (ciagi[0][j] + ciagi[1][j] + 1) / 2; d += abs(ciagi[i][j] - rozw[j]); } worst = max(worst, d); } if (worst < best) { best = worst; } printf("%lld\n", best);*/ return 0; }
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 | #include <cstdio> #include <iostream> #include <cstring> #include <vector> #include <algorithm> using namespace std; int ciagi[5][100000]; long long srednie[32]; long long srednie2[32]; long long rozw[100000]; long long pot[100000]; int main() { int n, k; scanf("%d%d", &n, &k); for (int i = 0; i < k; i++) { for (int j = 0; j < n; j++) { scanf("%d", &ciagi[i][j]); } } vector<pair<long long, long long> > elems; for (int i = 0; i < n; i++) { if (i == 0) { for (int j = 0; j < (1 << k); j++) { int c = 0; long long srednia = 0; for (int m = 0; m < k; m++) { if (j && (1 << m)) { srednia += ciagi[m][i]; c++; } } srednie[j] = 0; if (!c) continue; srednia /= c; srednie[j] = srednia; } long long d = 0; long long elem = 0; long long best = 1LL << 60; for (int j = 0; j < (1 << k); j++) { long long worst = -(1LL << 60); for (int o = 0; o < k; o++) { d = abs(ciagi[o][0] - srednie[j]); worst = max(worst, d); } if (worst < best) { best = worst; elem = srednie[j]; } } elems.push_back(make_pair(-1, elem)); } else { for (int j = 0; j < (1 << k); j++) { int c = 0; long long srednia = 0; for (int m = 0; m < k; m++) { if (j && (1 << m)) { srednia += ciagi[m][i]; c++; } } srednie2[j] = 0; if (!c) continue; srednia /= c; srednie2[j] = srednia; } long long elem1, elem2; long long best = 1LL << 60; for (int j = 0; j < (1 << k); j++) { for (int m = 0; m < (1 << k); m++) { long long worst = -(1LL << 60); for (int o = 0; o < k; o++) { long long d = abs(ciagi[o][i - 1] - srednie[j]); d += abs(ciagi[o][i] - srednie2[m]); worst = max(worst, d); } if (worst < best) { best = worst; elem1 = srednie[j]; elem2 = srednie2[m]; } } } memcpy(srednie, srednie2,sizeof(srednie)); elems.push_back(make_pair(elem1, elem2)); } } /*for (int i = 0; i < elems.size(); i++) { printf("%d %d\n", elems[i].first, elems[i].second); }*/ long long best = (1LL << 60); if (elems.size() == 1) { printf("%lld\n", elems[0].second); } else { long long worst = -(1LL << 60); for (int i = 0; i < k; i++) { long long d = 0; for (int j = 0; j + 1 < n; j++) { d += abs(ciagi[i][j] - (elems[j].second + elems[j + 1].first + 1) / 2); } d += abs(ciagi[i][n - 1] - elems[n - 1].second); worst = max(worst, d); } if (worst < best) { best = worst; for (int j = 0; j + 1 < n; j++) { rozw[j] = (elems[j].second + elems[j + 1].first + 1) / 2; } rozw[n - 1] = elems[n - 1].second; } //losuj for (int L = 0; L < 10; L++) { long long worst = -(1LL << 60); for (int j = 0; j + 1 < n; j++) { pot[j] = rand() % 2 ? elems[j].second : elems[j + 1].first; } pot[n - 1] = elems[n - 1].second; for (int i = 0; i < k; i++) { long long d = 0; for (int j = 0; j < n; j++) { d += abs(ciagi[i][j] - pot[j]); } worst = max(worst, d); } if (worst < best) { best = worst; memcpy(rozw, pot, sizeof(rozw)); } } } //best = (1LL << 60); for (int i = 0; i < n; i++) { printf("%d ", rozw[i]); } /* long long d = 0; long long worst = -(1LL << 60); for (int i = 0; i < k; i++) { long long d = 0; for (int j = 0; j < n; j++) { //if (k == 2) //rozw[j] = (ciagi[0][j] + ciagi[1][j] + 1) / 2; d += abs(ciagi[i][j] - rozw[j]); } worst = max(worst, d); } if (worst < best) { best = worst; } printf("%lld\n", best);*/ return 0; } |