//Przemysław Jakub Kozłowski #include <iostream> #include <cstdio> #include <algorithm> #include <map> using namespace std; const int N = 100005, K = 5; typedef long long LL; int n,k; int tab[N+2][K+2]; int wyn[N+2]; int main() { scanf("%d%d", &n, &k); for(int i = 1;i <= k;i++) for(int j = 1;j <= n;j++) scanf("%d", &tab[j][i]); if(true || k == 2) { LL odla = 0, odlb = 0; for(int i = 1;i <= n;i++) { wyn[i] = (tab[i][1]+tab[i][2])/2; odla += abs(tab[i][1]-wyn[i]); odlb += abs(tab[i][2]-wyn[i]); } //cerr << "Odla: " << odla << " Odlb: " << odlb << endl; for(int i = 1;i <= n;i++) { while(max(odla-abs(tab[i][1]-wyn[i])+abs(tab[i][1]-(wyn[i]+1)),odlb-abs(tab[i][2]-wyn[i])+abs(tab[i][2]-(wyn[i]+1))) < max(odla,odlb)) { //cerr << "Odla: " << odla << " odl[b]: " << odlb << endl; odla = odla-abs(tab[i][1]-wyn[i])+abs(tab[i][1]-(wyn[i]+1)); odlb = odlb-abs(tab[i][2]-wyn[i])+abs(tab[i][2]-(wyn[i]+1)); wyn[i]++; } while(max(odla-abs(tab[i][1]-wyn[i])+abs(tab[i][1]-(wyn[i]-1)),odlb-abs(tab[i][2]-wyn[i])+abs(tab[i][2]-(wyn[i]-1))) < max(odla,odlb)) { odla = odla-abs(tab[i][1]-wyn[i])+abs(tab[i][1]-(wyn[i]-1)); odlb = odlb-abs(tab[i][2]-wyn[i])+abs(tab[i][2]-(wyn[i]-1)); wyn[i]--; } } //cerr << "Odl: " << max(odla,odlb) << endl; //printf("%lld\n", max(odla,odlb)); for(int i = 1;i <= n;i++) printf("%d ", wyn[i]); printf("\n"); } 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 | //Przemysław Jakub Kozłowski #include <iostream> #include <cstdio> #include <algorithm> #include <map> using namespace std; const int N = 100005, K = 5; typedef long long LL; int n,k; int tab[N+2][K+2]; int wyn[N+2]; int main() { scanf("%d%d", &n, &k); for(int i = 1;i <= k;i++) for(int j = 1;j <= n;j++) scanf("%d", &tab[j][i]); if(true || k == 2) { LL odla = 0, odlb = 0; for(int i = 1;i <= n;i++) { wyn[i] = (tab[i][1]+tab[i][2])/2; odla += abs(tab[i][1]-wyn[i]); odlb += abs(tab[i][2]-wyn[i]); } //cerr << "Odla: " << odla << " Odlb: " << odlb << endl; for(int i = 1;i <= n;i++) { while(max(odla-abs(tab[i][1]-wyn[i])+abs(tab[i][1]-(wyn[i]+1)),odlb-abs(tab[i][2]-wyn[i])+abs(tab[i][2]-(wyn[i]+1))) < max(odla,odlb)) { //cerr << "Odla: " << odla << " odl[b]: " << odlb << endl; odla = odla-abs(tab[i][1]-wyn[i])+abs(tab[i][1]-(wyn[i]+1)); odlb = odlb-abs(tab[i][2]-wyn[i])+abs(tab[i][2]-(wyn[i]+1)); wyn[i]++; } while(max(odla-abs(tab[i][1]-wyn[i])+abs(tab[i][1]-(wyn[i]-1)),odlb-abs(tab[i][2]-wyn[i])+abs(tab[i][2]-(wyn[i]-1))) < max(odla,odlb)) { odla = odla-abs(tab[i][1]-wyn[i])+abs(tab[i][1]-(wyn[i]-1)); odlb = odlb-abs(tab[i][2]-wyn[i])+abs(tab[i][2]-(wyn[i]-1)); wyn[i]--; } } //cerr << "Odl: " << max(odla,odlb) << endl; //printf("%lld\n", max(odla,odlb)); for(int i = 1;i <= n;i++) printf("%d ", wyn[i]); printf("\n"); } return 0; } |