// Krzysztof Małysa #include <bits/stdc++.h> using namespace std; #define FOR(i,a,n) for (int i = (a), i##__ = (n); i <= i##__; ++i) #define REP(i,n) FOR(i,0,n-1) #define FORD(i,a,n) for (int i = (a), i##__ = (n); i >= i##__; --i) #define ALL(x) x.begin(), x.end() #define EB emplace_back #define ST first #define ND second #define OO(A) template<class... T> ostream& operator<<(ostream& os, const A<T...>& x) { return __o(os, ALL(x)); } #define SZ(x) ((int)x.size()) typedef long long LL; typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<PII> VPII; template<class A, class B> ostream& operator<<(ostream&, const pair<A, B>&); template<class I> ostream& __o(ostream&, I, I); template<class T, size_t N> ostream& operator<<(ostream& os, const array<T, N>& x) { return __o(os, ALL(x)); } OO(vector) OO(deque) OO(set) OO(multiset) OO(map) OO(multimap) template<class A, class B> ostream& operator<<(ostream& os, const pair<A, B>& p) { return os << "(" << p.ST << ", " << p.ND << ")"; } template<class I> ostream& __o(ostream& os, I a, I b) { os << "{"; for (; a != b;) os << *a++, cerr << (a == b ? "" : " "); return os << "}"; } template<class I> ostream& __d(ostream& os, I a, I b) { os << "{\n"; for (I c = a; a != b; ++a) os << " " << distance(c, a) << ": " << *a << endl; return os << "}"; } template<class... T> void __e(T&&... a) { int t[] = {(cerr << forward<T>(a), 0)...}; (void)t; cerr << endl; } template<class A, class B> void mini(A& a, B&& b) { if (b < a) a = b; } template<class A, class B> void maxi(A& a, B&& b) { if (b > a) a = b; } int ceil2(int x) { return 1 << (sizeof(x) * 8 - __builtin_clz(x - 1)); } #ifdef DEBUG # define D(...) __VA_ARGS__ #else # define D(...) #endif #define LOG(x) D(cerr << #x ": " << x) #define LOGN(x) D(LOG(x) << endl) #define DUMP(x) D(cerr << #x ": ", __d(cerr, ALL(x)) << endl) #define E(...) D(__e(__VA_ARGS__)) #define endl '\n' constexpr char nl = '\n'; // End of templates #include "cielib.h" static inline bool czyCieplo(VI& v) { return czyCieplo(v.data()); } static inline void znalazlem(VI& v) { znalazlem(v.data()); } int main() { int d = podajD(); int k = podajK(); int r = podajR(); assert(k >= d * 100); VI base(d, 0); // [0, 0, ..., 0] VI diffed = base; vector<bool> is_max(d); int maxes_collected = 0; while (maxes_collected < d) { E("======================================="); if (*max_element(ALL(diffed)) == r) break; // We cannot increase the max_value - it has reached the maximum, so there is nothing left to do assert(diffed == base); // diffed = base + [1, 1, ..., 1] for (int& x : diffed) ++x; /* Find new maxes: 2 * (# of maxs found) * log d * czyCieplo() + 1 * log d * czyCieplo <<<< because we have to check whether there is any maximum to search for */ // Increase (decrease the diff) current maxes by 1 to exclude them from searching // REP (i, d) // if (is_max[i]) // ++base[i]; // Binary search for (;;) { LOGN(base); int down = 0, up = d; while (down < up) { int mid = (down + up) >> 1; E("(", down, " -> ", mid, " <- ", up, ")"); // Check if there is a new max on the left side diffed = base; czyCieplo(base); // Add 1 to all (except maxes and left side - including x) FOR (i, 0, mid) if (is_max[i]) ++diffed[i]; FOR (i, mid + 1, d - 1) ++diffed[i]; LOGN(diffed); if (czyCieplo(diffed)) // There is no new max in [0, mid] down = mid + 1; else up = mid; } if (up == d) // There is no new max break; E("found: ", up); if (is_max[up]) goto break2; is_max[up] = true; ++maxes_collected; } if (false) { break2: break; } // Update maxes REP (i, d) if (is_max[i]) ++base[i]; diffed = base; LOGN(diffed); /* Equalize current maxes with next maxes */ int down = (*min_element(ALL(diffed)) == 0 ? 0 : -1); // -1 to adjust if too much was added int up = r - *max_element(ALL(diffed)); // 2d lg r * czyCieplo() base = diffed; // Search for lowest x for which czyCieplo(base + [x, x, ...]); czyCieplo(base + [x + 1, x + 1, ...]) == false while (down < up) { int mid = (down + up) >> 1; // E("{", down, " -> ", mid, " <- ", up, "}"); REP (i, d) if (is_max[i]) diffed[i] = base[i] + mid; assert(mid < up); czyCieplo(diffed); REP (i, d) if (is_max[i]) ++diffed[i]; if (czyCieplo(diffed)) down = mid + 1; else up = mid; } // Compute diffed = base + [up, up, ...] REP (i, d) if (is_max[i]) diffed[i] = base[i] + up; base = diffed; LOGN(base); } VI old_base = base; // Run it from other side to cover stupid edge cases when there is 0 in the solution if (find(ALL(base), 0) != base.end() && *max_element(ALL(base)) < r) { // 2d * czyCieplo() VI base1(d, r); diffed.assign(d, r - 1); // Find true 0s REP (i, d) { if (base[i] != 0) continue; // Check if new true 0 ++diffed[i]; czyCieplo(base1); if (!czyCieplo(diffed)) { // true 0 --diffed[i]; base[i] = -1; } } // The values in base are decreased by 1 now for (int& x : base) ++x; } // 2 * czyCieplo() if (czyCieplo(old_base), !czyCieplo(base)) base = move(old_base); // So in summary ((2 + 1)d log d + 2d log r + 2d + 2) times the function czyCieplo() is called LOGN(base); znalazlem(base); 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 | // Krzysztof Małysa #include <bits/stdc++.h> using namespace std; #define FOR(i,a,n) for (int i = (a), i##__ = (n); i <= i##__; ++i) #define REP(i,n) FOR(i,0,n-1) #define FORD(i,a,n) for (int i = (a), i##__ = (n); i >= i##__; --i) #define ALL(x) x.begin(), x.end() #define EB emplace_back #define ST first #define ND second #define OO(A) template<class... T> ostream& operator<<(ostream& os, const A<T...>& x) { return __o(os, ALL(x)); } #define SZ(x) ((int)x.size()) typedef long long LL; typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<PII> VPII; template<class A, class B> ostream& operator<<(ostream&, const pair<A, B>&); template<class I> ostream& __o(ostream&, I, I); template<class T, size_t N> ostream& operator<<(ostream& os, const array<T, N>& x) { return __o(os, ALL(x)); } OO(vector) OO(deque) OO(set) OO(multiset) OO(map) OO(multimap) template<class A, class B> ostream& operator<<(ostream& os, const pair<A, B>& p) { return os << "(" << p.ST << ", " << p.ND << ")"; } template<class I> ostream& __o(ostream& os, I a, I b) { os << "{"; for (; a != b;) os << *a++, cerr << (a == b ? "" : " "); return os << "}"; } template<class I> ostream& __d(ostream& os, I a, I b) { os << "{\n"; for (I c = a; a != b; ++a) os << " " << distance(c, a) << ": " << *a << endl; return os << "}"; } template<class... T> void __e(T&&... a) { int t[] = {(cerr << forward<T>(a), 0)...}; (void)t; cerr << endl; } template<class A, class B> void mini(A& a, B&& b) { if (b < a) a = b; } template<class A, class B> void maxi(A& a, B&& b) { if (b > a) a = b; } int ceil2(int x) { return 1 << (sizeof(x) * 8 - __builtin_clz(x - 1)); } #ifdef DEBUG # define D(...) __VA_ARGS__ #else # define D(...) #endif #define LOG(x) D(cerr << #x ": " << x) #define LOGN(x) D(LOG(x) << endl) #define DUMP(x) D(cerr << #x ": ", __d(cerr, ALL(x)) << endl) #define E(...) D(__e(__VA_ARGS__)) #define endl '\n' constexpr char nl = '\n'; // End of templates #include "cielib.h" static inline bool czyCieplo(VI& v) { return czyCieplo(v.data()); } static inline void znalazlem(VI& v) { znalazlem(v.data()); } int main() { int d = podajD(); int k = podajK(); int r = podajR(); assert(k >= d * 100); VI base(d, 0); // [0, 0, ..., 0] VI diffed = base; vector<bool> is_max(d); int maxes_collected = 0; while (maxes_collected < d) { E("======================================="); if (*max_element(ALL(diffed)) == r) break; // We cannot increase the max_value - it has reached the maximum, so there is nothing left to do assert(diffed == base); // diffed = base + [1, 1, ..., 1] for (int& x : diffed) ++x; /* Find new maxes: 2 * (# of maxs found) * log d * czyCieplo() + 1 * log d * czyCieplo <<<< because we have to check whether there is any maximum to search for */ // Increase (decrease the diff) current maxes by 1 to exclude them from searching // REP (i, d) // if (is_max[i]) // ++base[i]; // Binary search for (;;) { LOGN(base); int down = 0, up = d; while (down < up) { int mid = (down + up) >> 1; E("(", down, " -> ", mid, " <- ", up, ")"); // Check if there is a new max on the left side diffed = base; czyCieplo(base); // Add 1 to all (except maxes and left side - including x) FOR (i, 0, mid) if (is_max[i]) ++diffed[i]; FOR (i, mid + 1, d - 1) ++diffed[i]; LOGN(diffed); if (czyCieplo(diffed)) // There is no new max in [0, mid] down = mid + 1; else up = mid; } if (up == d) // There is no new max break; E("found: ", up); if (is_max[up]) goto break2; is_max[up] = true; ++maxes_collected; } if (false) { break2: break; } // Update maxes REP (i, d) if (is_max[i]) ++base[i]; diffed = base; LOGN(diffed); /* Equalize current maxes with next maxes */ int down = (*min_element(ALL(diffed)) == 0 ? 0 : -1); // -1 to adjust if too much was added int up = r - *max_element(ALL(diffed)); // 2d lg r * czyCieplo() base = diffed; // Search for lowest x for which czyCieplo(base + [x, x, ...]); czyCieplo(base + [x + 1, x + 1, ...]) == false while (down < up) { int mid = (down + up) >> 1; // E("{", down, " -> ", mid, " <- ", up, "}"); REP (i, d) if (is_max[i]) diffed[i] = base[i] + mid; assert(mid < up); czyCieplo(diffed); REP (i, d) if (is_max[i]) ++diffed[i]; if (czyCieplo(diffed)) down = mid + 1; else up = mid; } // Compute diffed = base + [up, up, ...] REP (i, d) if (is_max[i]) diffed[i] = base[i] + up; base = diffed; LOGN(base); } VI old_base = base; // Run it from other side to cover stupid edge cases when there is 0 in the solution if (find(ALL(base), 0) != base.end() && *max_element(ALL(base)) < r) { // 2d * czyCieplo() VI base1(d, r); diffed.assign(d, r - 1); // Find true 0s REP (i, d) { if (base[i] != 0) continue; // Check if new true 0 ++diffed[i]; czyCieplo(base1); if (!czyCieplo(diffed)) { // true 0 --diffed[i]; base[i] = -1; } } // The values in base are decreased by 1 now for (int& x : base) ++x; } // 2 * czyCieplo() if (czyCieplo(old_base), !czyCieplo(base)) base = move(old_base); // So in summary ((2 + 1)d log d + 2d log r + 2d + 2) times the function czyCieplo() is called LOGN(base); znalazlem(base); return 0; } |