// Artur Kraska, II UWr #include <algorithm> #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <vector> #include <cmath> #include <list> #include <set> #include <map> #define forr(i, n) for(int i=0; i<n; i++) #define FOREACH(iter, coll) for(typeof(coll.begin()) iter = coll.begin(); iter != coll.end(); ++iter) #define FOREACHR(iter, coll) for(typeof(coll.rbegin()) iter = coll.rbegin(); iter != coll.rend(); ++iter) #define lbound(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;}) #define testy() int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests) #define CLEAR(tab) memset(tab, 0, sizeof(tab)) #define CONTAIN(el, coll) (coll.find(el) != coll.end()) #define FOR(i, a, b) for(int i=a; i<=b; i++) #define FORD(i, a, b) for(int i=a; i>=b; i--) #define MP make_pair #define PB push_back #define deb(X) X; #define M 1000000007 #define INF 1000000007 using namespace std; int n, k, tab[6][100007], w[100007]; long long odl[6]; int mediana(int a, int b, int c) { if(a > b) swap(a, b); if(c <= a) return a; if(b <= c) return b; return c; } int main() { scanf("%d %d", &n, &k); //k = 3; forr(j, k) forr(i, n) { scanf("%d", &tab[j][i]); } if(k == 2) { int b = 0; forr(i, n) if(abs(tab[0][i] - tab[1][i])&1) { w[i] = ((tab[0][i] + tab[1][i] + (b^(tab[0][i] < tab[1][i])))>>1); b ^= 1; } else w[i] = ((tab[0][i] + tab[1][i])>>1); } else if(k == 3) { forr(i, n) w[i] = mediana(tab[0][i], tab[1][i], tab[2][i]); forr(i, 3) { forr(j, n) odl[i] += abs(tab[i][j] - w[j]); } int nr = 0; FOR(i, 1, 2) if(odl[i] > odl[nr]) nr = i; long long ile = (odl[nr] - max(odl[(nr+1)%3], odl[(nr+2)%3]))/2; //cout << odl[nr] - ile << endl; for(int i=0; i<n && ile > 0; i++) { int d = abs(tab[nr][i] - w[i]); if(d <= ile) { w[i] = tab[nr][i]; ile -= d; } else { if(tab[nr][i] < w[i]) w[i] -= ile; else w[i] += ile; ile = 0; } } } else if(k == 4) { } else { } forr(i, n) printf("%d ", w[i]); printf("\n"); /* long long maks = 0, wynik; forr(i, k) { wynik = 0; forr(j, n) wynik += abs(tab[i][j] - w[j]); maks = max(maks, wynik); } cout << maks << endl; */ 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 | // Artur Kraska, II UWr #include <algorithm> #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <vector> #include <cmath> #include <list> #include <set> #include <map> #define forr(i, n) for(int i=0; i<n; i++) #define FOREACH(iter, coll) for(typeof(coll.begin()) iter = coll.begin(); iter != coll.end(); ++iter) #define FOREACHR(iter, coll) for(typeof(coll.rbegin()) iter = coll.rbegin(); iter != coll.rend(); ++iter) #define lbound(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;}) #define testy() int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests) #define CLEAR(tab) memset(tab, 0, sizeof(tab)) #define CONTAIN(el, coll) (coll.find(el) != coll.end()) #define FOR(i, a, b) for(int i=a; i<=b; i++) #define FORD(i, a, b) for(int i=a; i>=b; i--) #define MP make_pair #define PB push_back #define deb(X) X; #define M 1000000007 #define INF 1000000007 using namespace std; int n, k, tab[6][100007], w[100007]; long long odl[6]; int mediana(int a, int b, int c) { if(a > b) swap(a, b); if(c <= a) return a; if(b <= c) return b; return c; } int main() { scanf("%d %d", &n, &k); //k = 3; forr(j, k) forr(i, n) { scanf("%d", &tab[j][i]); } if(k == 2) { int b = 0; forr(i, n) if(abs(tab[0][i] - tab[1][i])&1) { w[i] = ((tab[0][i] + tab[1][i] + (b^(tab[0][i] < tab[1][i])))>>1); b ^= 1; } else w[i] = ((tab[0][i] + tab[1][i])>>1); } else if(k == 3) { forr(i, n) w[i] = mediana(tab[0][i], tab[1][i], tab[2][i]); forr(i, 3) { forr(j, n) odl[i] += abs(tab[i][j] - w[j]); } int nr = 0; FOR(i, 1, 2) if(odl[i] > odl[nr]) nr = i; long long ile = (odl[nr] - max(odl[(nr+1)%3], odl[(nr+2)%3]))/2; //cout << odl[nr] - ile << endl; for(int i=0; i<n && ile > 0; i++) { int d = abs(tab[nr][i] - w[i]); if(d <= ile) { w[i] = tab[nr][i]; ile -= d; } else { if(tab[nr][i] < w[i]) w[i] -= ile; else w[i] += ile; ile = 0; } } } else if(k == 4) { } else { } forr(i, n) printf("%d ", w[i]); printf("\n"); /* long long maks = 0, wynik; forr(i, k) { wynik = 0; forr(j, n) wynik += abs(tab[i][j] - w[j]); maks = max(maks, wynik); } cout << maks << endl; */ return 0; } |