#include <stdio.h> #include <algorithm> #include <vector> using namespace std; typedef long long LL; const LL MaxN = 100005, Infty = 1010101010, MaxK = 5; LL seq[MaxK][MaxN]; int N, K; LL longest=0; int a, b; void input(){ scanf("%d%d", &N, &K); for(LL i = 0; i < K; i++){ for(LL j = 0; j < N; j++){ scanf("%lld", &seq[i][j]); } } } void process2(){ LL delta = 0; LL incr = 0; for(LL i = 0; i < N; i++){ LL val = (seq[0][i] + seq[1][i]) / 2; delta += abs(seq[0][i]-val); delta -= abs(seq[1][i]-val); if(delta > 1){ if(seq[0][i] > val) val++; else val--; delta -= 2; } else if(delta < -1){ if(seq[1][i] > val) val++; else val--; delta += 2; } /*LL sum = (seq[0][i] + seq[1][i]); if(sum % 2 == 0) printf("%lld ", sum / 2); else if(seq[0][i] < seq[1][i]){ printf("%lld ", sum / 2 + incr); incr = 1-incr; } else { incr = 1-incr; printf("%lld ", sum / 2 + incr); }*/ printf("%lld ", val); } printf("\n"); } LL get_dist(LL i, LL j){ LL dist = 0; for(LL k = 0; k < N; k++){ dist += abs(seq[i][k]-seq[j][k]); } return dist; } LL result[MaxN]; void process3(){ int c = 0; if(a == 0){ c = 3-b; } LL dst = get_dist(a, b); if(get_dist(a,c) < get_dist(b,c)) swap(a,b); LL lft = dst / 2; copy(seq[a], seq[a]+N, result); for(LL i = 0; i < N; i++){ if(seq[a][i] > seq[b][i] && seq[a][i] > seq[c][i]){ LL dff = min(seq[a][i]-seq[b][i], seq[a][i]-seq[c][i]); dff = min((LL)dff, lft); result[i] -= dff; lft -= dff; } else if(seq[a][i] < seq[b][i] && seq[a][i] < seq[c][i]){ LL dff = min(seq[b][i]-seq[a][i], seq[c][i]-seq[a][i]); dff = min((LL)dff, lft); result[i] += dff; lft -= dff; } } for(LL i = 0; i < N; i++) printf("%lld ", result[i]); printf("\n"); } int main(){ input(); /*LL r = 0; for(int i = 0; i < N; i++){ r += abs(seq[0][i] - seq[1][i]); } printf("%lld\n", r); return 0;*/ for(LL i = 0; i < K; i++){ for(LL j = i+1; j < K; j++){ LL dist = get_dist(i,j); if(dist > longest){ longest = dist; a = i; b = j; } } } if(K == 2){ process2(); } else if(K == 3){ process3(); } else { printf("DLUZSZE ZYCIE KAZDEJ PRALKI TO CALGON\n"); } }
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 | #include <stdio.h> #include <algorithm> #include <vector> using namespace std; typedef long long LL; const LL MaxN = 100005, Infty = 1010101010, MaxK = 5; LL seq[MaxK][MaxN]; int N, K; LL longest=0; int a, b; void input(){ scanf("%d%d", &N, &K); for(LL i = 0; i < K; i++){ for(LL j = 0; j < N; j++){ scanf("%lld", &seq[i][j]); } } } void process2(){ LL delta = 0; LL incr = 0; for(LL i = 0; i < N; i++){ LL val = (seq[0][i] + seq[1][i]) / 2; delta += abs(seq[0][i]-val); delta -= abs(seq[1][i]-val); if(delta > 1){ if(seq[0][i] > val) val++; else val--; delta -= 2; } else if(delta < -1){ if(seq[1][i] > val) val++; else val--; delta += 2; } /*LL sum = (seq[0][i] + seq[1][i]); if(sum % 2 == 0) printf("%lld ", sum / 2); else if(seq[0][i] < seq[1][i]){ printf("%lld ", sum / 2 + incr); incr = 1-incr; } else { incr = 1-incr; printf("%lld ", sum / 2 + incr); }*/ printf("%lld ", val); } printf("\n"); } LL get_dist(LL i, LL j){ LL dist = 0; for(LL k = 0; k < N; k++){ dist += abs(seq[i][k]-seq[j][k]); } return dist; } LL result[MaxN]; void process3(){ int c = 0; if(a == 0){ c = 3-b; } LL dst = get_dist(a, b); if(get_dist(a,c) < get_dist(b,c)) swap(a,b); LL lft = dst / 2; copy(seq[a], seq[a]+N, result); for(LL i = 0; i < N; i++){ if(seq[a][i] > seq[b][i] && seq[a][i] > seq[c][i]){ LL dff = min(seq[a][i]-seq[b][i], seq[a][i]-seq[c][i]); dff = min((LL)dff, lft); result[i] -= dff; lft -= dff; } else if(seq[a][i] < seq[b][i] && seq[a][i] < seq[c][i]){ LL dff = min(seq[b][i]-seq[a][i], seq[c][i]-seq[a][i]); dff = min((LL)dff, lft); result[i] += dff; lft -= dff; } } for(LL i = 0; i < N; i++) printf("%lld ", result[i]); printf("\n"); } int main(){ input(); /*LL r = 0; for(int i = 0; i < N; i++){ r += abs(seq[0][i] - seq[1][i]); } printf("%lld\n", r); return 0;*/ for(LL i = 0; i < K; i++){ for(LL j = i+1; j < K; j++){ LL dist = get_dist(i,j); if(dist > longest){ longest = dist; a = i; b = j; } } } if(K == 2){ process2(); } else if(K == 3){ process3(); } else { printf("DLUZSZE ZYCIE KAZDEJ PRALKI TO CALGON\n"); } } |