#include<cstdio> #include<algorithm> #include<iostream> #include<fstream> #include<iomanip> #include<vector> #include<map> #include<set> #include<stack> #define SC(n) scanf("%d", &n) #define SC2(n, m) scanf("%d %d", &n, &m) #define SCC(c) scanf(" %c", &c) #define SCS(b) scanf("%s", b) #define REP(i, n) for(int i = 0; i < (n); ++i) #define FORE(i, a, b) for(int i = (a); i <= (b); ++i) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define FORIT(i, c, typ) for(typ::iterator i = c.begin(); i != c.end(); ++i) #define PR(n) printf("%d ", (int) (n)) #define PRN(n) printf("%d\n", (int) (n)) #define elif else if #define pb push_back #define mp make_pair #define xx first #define yy second #define all(v) v.begin(), v.end() #define itr iterator #define dbg if(0) #define ansD if(0) #define ansSeq if(1) #define prd dbg printf using namespace std; typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pi; typedef vector<pi> vpi; typedef pair<int, pi> pii; typedef vector<pii> vpii; typedef vector<vpi> vvpi; typedef map<vi, int> mvii; const int NMAX = 100*1000 + 7, PMAX = 200; const int MASKMAX[6] = {0,0,0,9,177147,1000*1000}; const ll INF = (1LL<<60); int n, k, perk; int a[5][NMAX], b[NMAX]; char reversed[NMAX]; ll out = INF; ll score[5], scores[PMAX][5][5], interval[PMAX][5]; vi bestmask, mask, pers[PMAX], positions[PMAX]; mvii address, shift; ll countD(int* t1, int* t2) { ll ans = 0; REP(i, n) ans += abs(t1[i] - t2[i]); return ans; } void decipher(vi & ans, int mm, int i0) { REP(i1, perk) if(i1 != i0) { ans[i1] = mm % k; mm /= k; } } int main() { SC2(n,k); REP(j,k) REP(i, n) SC(a[j][i]); if(k == 2) { out = countD(a[0], a[1]); out = (out + 1) / 2; ll sum = out; REP(i, n) { if(a[0][i] < a[1][i]) { b[i] = min((ll) a[1][i], (ll) a[0][i] + sum); sum -= b[i] - a[0][i]; } else { b[i] = max((ll) a[1][i], (ll) a[0][i] - sum); sum -= a[0][i] - b[i]; } } } else { vi per; REP(j, k) per.pb(j); do { if(per[0] < per[k-1]) { pers[perk] = per; address[per] = perk++; } } while (next_permutation(all(per))); prd("perk = %d\n", perk); REP(i, n) { vi per; vpi ord; REP(j, k) ord.pb(mp(a[j][i], j)); sort(all(ord)); REP(j, k) per.pb(ord[j].yy); if(per[0] > per[k-1]) { reversed[i] = 1; reverse(all(per)); REP(j, k) a[j][i] = -a[j][i]; } int ip = address[per]; prd("%d --> %d\n", i, ip); positions[ip].pb(i); REP(j, k) REP(h, k) scores[ip][h][j] += abs(a[j][i] - a[per[h]][i]); REP(j, k-1) interval[ip][j] += a[per[j+1]][i] - a[per[j]][i]; } dbg REP(ip, perk) { printf("%d:\n", ip); REP(i, k) printf("%d ", pers[ip][i]); printf("\n"); REP(i,k-1) cout << interval[ip][i] << " "; printf("\n"); } mask.resize(perk); REP(ip, perk) { per = pers[ip]; REP(im, MASKMAX[k]) { decipher(mask, im, ip); REP(ij, k - 1) { REP(j, k) score[j] = 0; // to odpowiada oryginalnym numerom mask[ip] = ij; dbg { REP(jp, perk) if(jp == ip) printf("(%d) ", mask[jp]); else printf("%d ", mask[jp]); cout << endl; } REP(ip2, perk) REP(j, k) score[j] += scores[ip2][mask[ip2]][j]; mask[ip] += k; // teraz szyfrujemy to miejsce ll dminus = -INF, dplus = -INF, d = 0, ogr = interval[ip][ij]; REP(j, k) if(j <= ij) dplus = max(dplus, score[per[j]]); else dminus = max(dminus, score[per[j]]); if(dminus > dplus) d = min(ogr, (dminus - dplus) / 2); ll result = max(dplus, dminus - d); if(result < out) { dbg REP(j,k) cout << score[j] << " "; dbg cout << " --> " << result << " " << d << endl; out = result; bestmask = mask; shift[mask] = d; } } } } REP(ip, perk) { vi per = pers[ip]; int ij; if(bestmask[ip] < k) { ij = per[bestmask[ip]]; FORIT(it, positions[ip], vi) b[*it] = a[ij][*it]; } else { ij = per[bestmask[ip] - k]; int ij2 = per[bestmask[ip] - k + 1]; ll d = shift[bestmask]; FORIT(it, positions[ip], vi) { b[*it] = min((ll) a[ij][*it] + d, (ll) a[ij2][*it]); d -= b[*it] - a[ij][*it]; } } } } ansD cout << out << endl; dbg { ll tmp = 0; REP(j, k) tmp = max(tmp, countD(a[j], b)); if(tmp == out) cout << "OK" << endl; else cout << "realD = " << tmp << endl; } ansSeq REP(i, n) if(reversed[i]) printf("%d ", -b[i]); else printf("%d ", b[i]); 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 212 213 214 215 216 217 218 219 220 221 222 | #include<cstdio> #include<algorithm> #include<iostream> #include<fstream> #include<iomanip> #include<vector> #include<map> #include<set> #include<stack> #define SC(n) scanf("%d", &n) #define SC2(n, m) scanf("%d %d", &n, &m) #define SCC(c) scanf(" %c", &c) #define SCS(b) scanf("%s", b) #define REP(i, n) for(int i = 0; i < (n); ++i) #define FORE(i, a, b) for(int i = (a); i <= (b); ++i) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define FORIT(i, c, typ) for(typ::iterator i = c.begin(); i != c.end(); ++i) #define PR(n) printf("%d ", (int) (n)) #define PRN(n) printf("%d\n", (int) (n)) #define elif else if #define pb push_back #define mp make_pair #define xx first #define yy second #define all(v) v.begin(), v.end() #define itr iterator #define dbg if(0) #define ansD if(0) #define ansSeq if(1) #define prd dbg printf using namespace std; typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pi; typedef vector<pi> vpi; typedef pair<int, pi> pii; typedef vector<pii> vpii; typedef vector<vpi> vvpi; typedef map<vi, int> mvii; const int NMAX = 100*1000 + 7, PMAX = 200; const int MASKMAX[6] = {0,0,0,9,177147,1000*1000}; const ll INF = (1LL<<60); int n, k, perk; int a[5][NMAX], b[NMAX]; char reversed[NMAX]; ll out = INF; ll score[5], scores[PMAX][5][5], interval[PMAX][5]; vi bestmask, mask, pers[PMAX], positions[PMAX]; mvii address, shift; ll countD(int* t1, int* t2) { ll ans = 0; REP(i, n) ans += abs(t1[i] - t2[i]); return ans; } void decipher(vi & ans, int mm, int i0) { REP(i1, perk) if(i1 != i0) { ans[i1] = mm % k; mm /= k; } } int main() { SC2(n,k); REP(j,k) REP(i, n) SC(a[j][i]); if(k == 2) { out = countD(a[0], a[1]); out = (out + 1) / 2; ll sum = out; REP(i, n) { if(a[0][i] < a[1][i]) { b[i] = min((ll) a[1][i], (ll) a[0][i] + sum); sum -= b[i] - a[0][i]; } else { b[i] = max((ll) a[1][i], (ll) a[0][i] - sum); sum -= a[0][i] - b[i]; } } } else { vi per; REP(j, k) per.pb(j); do { if(per[0] < per[k-1]) { pers[perk] = per; address[per] = perk++; } } while (next_permutation(all(per))); prd("perk = %d\n", perk); REP(i, n) { vi per; vpi ord; REP(j, k) ord.pb(mp(a[j][i], j)); sort(all(ord)); REP(j, k) per.pb(ord[j].yy); if(per[0] > per[k-1]) { reversed[i] = 1; reverse(all(per)); REP(j, k) a[j][i] = -a[j][i]; } int ip = address[per]; prd("%d --> %d\n", i, ip); positions[ip].pb(i); REP(j, k) REP(h, k) scores[ip][h][j] += abs(a[j][i] - a[per[h]][i]); REP(j, k-1) interval[ip][j] += a[per[j+1]][i] - a[per[j]][i]; } dbg REP(ip, perk) { printf("%d:\n", ip); REP(i, k) printf("%d ", pers[ip][i]); printf("\n"); REP(i,k-1) cout << interval[ip][i] << " "; printf("\n"); } mask.resize(perk); REP(ip, perk) { per = pers[ip]; REP(im, MASKMAX[k]) { decipher(mask, im, ip); REP(ij, k - 1) { REP(j, k) score[j] = 0; // to odpowiada oryginalnym numerom mask[ip] = ij; dbg { REP(jp, perk) if(jp == ip) printf("(%d) ", mask[jp]); else printf("%d ", mask[jp]); cout << endl; } REP(ip2, perk) REP(j, k) score[j] += scores[ip2][mask[ip2]][j]; mask[ip] += k; // teraz szyfrujemy to miejsce ll dminus = -INF, dplus = -INF, d = 0, ogr = interval[ip][ij]; REP(j, k) if(j <= ij) dplus = max(dplus, score[per[j]]); else dminus = max(dminus, score[per[j]]); if(dminus > dplus) d = min(ogr, (dminus - dplus) / 2); ll result = max(dplus, dminus - d); if(result < out) { dbg REP(j,k) cout << score[j] << " "; dbg cout << " --> " << result << " " << d << endl; out = result; bestmask = mask; shift[mask] = d; } } } } REP(ip, perk) { vi per = pers[ip]; int ij; if(bestmask[ip] < k) { ij = per[bestmask[ip]]; FORIT(it, positions[ip], vi) b[*it] = a[ij][*it]; } else { ij = per[bestmask[ip] - k]; int ij2 = per[bestmask[ip] - k + 1]; ll d = shift[bestmask]; FORIT(it, positions[ip], vi) { b[*it] = min((ll) a[ij][*it] + d, (ll) a[ij2][*it]); d -= b[*it] - a[ij][*it]; } } } } ansD cout << out << endl; dbg { ll tmp = 0; REP(j, k) tmp = max(tmp, countD(a[j], b)); if(tmp == out) cout << "OK" << endl; else cout << "realD = " << tmp << endl; } ansSeq REP(i, n) if(reversed[i]) printf("%d ", -b[i]); else printf("%d ", b[i]); return 0; } |