#include <cstdio> #include <iostream> #include <algorithm> #include <set> #include <map> #include <stack> #include <list> #include <queue> #include <deque> #include <cctype> #include <string> #include <vector> #include <sstream> #include <iterator> #include <numeric> #include <cmath> using namespace std; typedef vector <int > VI; typedef vector < VI > VVI; typedef long long LL; typedef vector < LL > VLL; typedef vector < double > VD; typedef vector < string > VS; typedef pair<int,int> PII; typedef vector <PII> VPII; typedef istringstream ISS; #define ALL(x) x.begin(),x.end() #define REP(i,n) for (int i=0; i<(n); ++i) #define FOR(var,pocz,koniec) for (int var=(pocz); var<=(koniec); ++var) #define FORD(var,pocz,koniec) for (int var=(pocz); var>=(koniec); --var) #define FOREACH(it, X) for(__typeof((X).begin()) it = (X).begin(); it != (X).end(); ++it) #define PB push_back #define PF push_front #define MP(a,b) make_pair(a,b) #define ST first #define ND second #define SIZE(x) (int)x.size() const int N = 110000; int v[10][N]; int mini[N]; int maxi[N]; LL koszt[N]; int wynik[N]; int zapisz[N]; int n, k; int dasie3(LL d) { koszt[0] = koszt[1] = koszt[2] = 0; int p[3]; REP(i,n) { if (v[0][i] == mini[i]) { p[0] = 0; if (v[1][i] <= v[2][i]) { p[1] = 1; p[2] = 2; } else { p[1] = 2; p[2] = 1; } } else if (v[1][i] == mini[i]) { p[0] = 1; if (v[0][i] <= v[2][i]) { p[1] = 0; p[2] = 2; } else { p[1] = 2; p[2] = 0; } } else { p[0] = 2; if (v[0][i] <= v[1][i]) { p[1] = 0; p[2] = 1; } else { p[1] = 1; p[2] = 0; } } int sr = v[p[1]][i]; if (koszt[p[0]] + abs(v[p[0]][i] - sr) <= d && koszt[p[2]] + abs(v[p[2]][i] - sr) <= d) { wynik[i] = sr; } else if (koszt[p[0]] + abs(v[p[0]][i] - sr) > d) { int ile = d - koszt[p[0]]; if (v[p[0]][i] <= sr) { wynik[i] = v[p[0]][i] + ile; } else { wynik[i] = v[p[0]][i] - ile; } } else { int ile = d - koszt[p[2]]; if (v[p[2]][i] <= sr) { wynik[i] = v[p[2]][i] + ile; } else { wynik[i] = v[p[2]][i] - ile; } } REP(ii,3) koszt[ii] += abs(v[ii][i] - wynik[i]); } REP(ii,3) if (koszt[ii] > d) return 0; REP(i,n) zapisz[i] = wynik[i]; return 1; } int main(){ scanf("%d %d", &n, &k); k = 3; REP(i,k) REP(j,n) scanf("%d",v[i]+j); REP(j,n) mini[j] = maxi[j] = v[0][j]; FOR(i,1,k-1) REP(j,n) { if (v[i][j] < mini[j]) mini[j] = v[i][j]; if (v[i][j] > maxi[j]) maxi[j] = v[i][j]; } if (k == 2) { REP(j,n) { if (abs(v[0][j] - v[1][j]) % 2 == 0) { wynik[j] = (v[0][j]+v[1][j]) / 2; } else { if (koszt[0] <= koszt[1]) { koszt[0]++; wynik[j] = v[0][j] + (v[1][j] - v[0][j]) / 2; } else { koszt[1]++; int pom = v[1][j] - v[0][j]; if (pom < 0) pom--; else pom++; wynik[j] = v[0][j] + pom/2; } } } REP(j,n) zapisz[j] = wynik[j]; } else if (k == 3) { LL r = (LL)n * 2001 * 1001 * 1001; LL l = 0; while (l < r) { LL m = (l+r) / 2; if (dasie3(m)) r = m; else l = m+1; } // printf("%lld\n", l); } REP(j,n) printf("%d ",zapisz[j]); puts(""); 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 | #include <cstdio> #include <iostream> #include <algorithm> #include <set> #include <map> #include <stack> #include <list> #include <queue> #include <deque> #include <cctype> #include <string> #include <vector> #include <sstream> #include <iterator> #include <numeric> #include <cmath> using namespace std; typedef vector <int > VI; typedef vector < VI > VVI; typedef long long LL; typedef vector < LL > VLL; typedef vector < double > VD; typedef vector < string > VS; typedef pair<int,int> PII; typedef vector <PII> VPII; typedef istringstream ISS; #define ALL(x) x.begin(),x.end() #define REP(i,n) for (int i=0; i<(n); ++i) #define FOR(var,pocz,koniec) for (int var=(pocz); var<=(koniec); ++var) #define FORD(var,pocz,koniec) for (int var=(pocz); var>=(koniec); --var) #define FOREACH(it, X) for(__typeof((X).begin()) it = (X).begin(); it != (X).end(); ++it) #define PB push_back #define PF push_front #define MP(a,b) make_pair(a,b) #define ST first #define ND second #define SIZE(x) (int)x.size() const int N = 110000; int v[10][N]; int mini[N]; int maxi[N]; LL koszt[N]; int wynik[N]; int zapisz[N]; int n, k; int dasie3(LL d) { koszt[0] = koszt[1] = koszt[2] = 0; int p[3]; REP(i,n) { if (v[0][i] == mini[i]) { p[0] = 0; if (v[1][i] <= v[2][i]) { p[1] = 1; p[2] = 2; } else { p[1] = 2; p[2] = 1; } } else if (v[1][i] == mini[i]) { p[0] = 1; if (v[0][i] <= v[2][i]) { p[1] = 0; p[2] = 2; } else { p[1] = 2; p[2] = 0; } } else { p[0] = 2; if (v[0][i] <= v[1][i]) { p[1] = 0; p[2] = 1; } else { p[1] = 1; p[2] = 0; } } int sr = v[p[1]][i]; if (koszt[p[0]] + abs(v[p[0]][i] - sr) <= d && koszt[p[2]] + abs(v[p[2]][i] - sr) <= d) { wynik[i] = sr; } else if (koszt[p[0]] + abs(v[p[0]][i] - sr) > d) { int ile = d - koszt[p[0]]; if (v[p[0]][i] <= sr) { wynik[i] = v[p[0]][i] + ile; } else { wynik[i] = v[p[0]][i] - ile; } } else { int ile = d - koszt[p[2]]; if (v[p[2]][i] <= sr) { wynik[i] = v[p[2]][i] + ile; } else { wynik[i] = v[p[2]][i] - ile; } } REP(ii,3) koszt[ii] += abs(v[ii][i] - wynik[i]); } REP(ii,3) if (koszt[ii] > d) return 0; REP(i,n) zapisz[i] = wynik[i]; return 1; } int main(){ scanf("%d %d", &n, &k); k = 3; REP(i,k) REP(j,n) scanf("%d",v[i]+j); REP(j,n) mini[j] = maxi[j] = v[0][j]; FOR(i,1,k-1) REP(j,n) { if (v[i][j] < mini[j]) mini[j] = v[i][j]; if (v[i][j] > maxi[j]) maxi[j] = v[i][j]; } if (k == 2) { REP(j,n) { if (abs(v[0][j] - v[1][j]) % 2 == 0) { wynik[j] = (v[0][j]+v[1][j]) / 2; } else { if (koszt[0] <= koszt[1]) { koszt[0]++; wynik[j] = v[0][j] + (v[1][j] - v[0][j]) / 2; } else { koszt[1]++; int pom = v[1][j] - v[0][j]; if (pom < 0) pom--; else pom++; wynik[j] = v[0][j] + pom/2; } } } REP(j,n) zapisz[j] = wynik[j]; } else if (k == 3) { LL r = (LL)n * 2001 * 1001 * 1001; LL l = 0; while (l < r) { LL m = (l+r) / 2; if (dasie3(m)) r = m; else l = m+1; } // printf("%lld\n", l); } REP(j,n) printf("%d ",zapisz[j]); puts(""); return 0; } |