#define _CRT_NONSTDC_NO_WARNINGS #define _SILENCE_CXX17_ITERATOR_BASE_CLASS_DEPRECATION_WARNING #include #include #include #include #include #ifdef _MSC_VER #include #include #include #include #include #include #include #include /* g++ functions */ int __builtin_clz(unsigned int n) { unsigned long index; _BitScanReverse(&index, n); return 31 - index; } int __builtin_ctz(unsigned int n) { unsigned long index; _BitScanForward(&index, n); return index; } namespace std { inline int __lg(int __n) { return sizeof(int) * 8 - 1 - __builtin_clz(__n); } } /* enable __uint128_t in MSVC */ //#include //using __uint128_t = boost::multiprecision::uint128_t; #else #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #endif /** compro io **/ namespace aux { template struct tp { static void output(std::ostream& os, const T& v) { os << std::get(v) << ", "; tp::output(os, v); } }; template struct tp { static void output(std::ostream& os, const T& v) { os << std::get(v); } }; } template std::ostream& operator<<(std::ostream& os, const std::tuple& t) { os << '['; aux::tp, 0, sizeof...(Ts) - 1>::output(os, t); return os << ']'; } // tuple out template std::basic_ostream& operator<<(std::basic_ostream& os, const Container& x); // container out (fwd decl) template std::ostream& operator<<(std::ostream& os, const std::pair& p) { return os << "[" << p.first << ", " << p.second << "]"; } // pair out template std::istream& operator>>(std::istream& is, std::pair& p) { return is >> p.first >> p.second; } // pair in std::ostream& operator<<(std::ostream& os, const std::vector::reference& v) { os << (v ? '1' : '0'); return os; } // bool (vector) out std::ostream& operator<<(std::ostream& os, const std::vector& v) { bool f = true; os << "["; for (const auto& x : v) { os << (f ? "" : ", ") << x; f = false; } os << "]"; return os; } // vector out template std::basic_ostream& operator<<(std::basic_ostream& os, const Container& x) { bool f = true; os << "["; for (auto& y : x) { os << (f ? "" : ", ") << y; f = false; } return os << "]"; } // container out template())), class = typename std::enable_if::value>::type> std::istream& operator>>(std::istream& is, T& a) { for (auto& x : a) is >> x; return is; } // container in template auto operator<<(std::ostream& out, const T& t) -> decltype(out << t.stringify()) { out << t.stringify(); return out; } // struct (has stringify() func) out /** io setup **/ struct IOSetup { IOSetup(bool f) { if (f) { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); } std::cout << std::fixed << std::setprecision(15); } } iosetup(true); // set false when solving interective problems /** string formatter **/ template std::string format(const std::string& f, Ts... t) { size_t l = std::snprintf(nullptr, 0, f.c_str(), t...); std::vector b(l + 1); std::snprintf(&b[0], l + 1, f.c_str(), t...); return std::string(&b[0], &b[0] + l); } /** dump **/ #define DUMPOUT std::cerr std::ostringstream DUMPBUF; #define dump(...) do{DUMPBUF<<" ";DUMPBUF<<#__VA_ARGS__<<" :[DUMP - "<<__LINE__<<":"<<__FUNCTION__<<"]"< void dump_func(Head&& head, Tail&&... tail) { DUMPBUF << head; if (sizeof...(Tail) == 0) { DUMPBUF << " "; } else { DUMPBUF << ", "; } dump_func(std::move(tail)...); } /** timer **/ class Timer { double t = 0, paused = 0, tmp; public: Timer() { reset(); } static double time() { #ifdef _MSC_VER return __rdtsc() / 2.3e9; #else unsigned long long a, d; __asm__ volatile("rdtsc" : "=a"(a), "=d"(d)); return (d << 32 | a) / 2.3e9; #endif } void reset() { t = time(); } void pause() { tmp = time(); } void restart() { paused += time() - tmp; } double elapsed_ms() const { return (time() - t - paused) * 1000.0; } }; /** rand **/ struct Xorshift { static constexpr uint64_t M = INT_MAX; static constexpr double e = 1.0 / M; uint64_t x = 88172645463325252LL; Xorshift() {} Xorshift(uint64_t seed) { reseed(seed); } inline void reseed(uint64_t seed) { x = 0x498b3bc5 ^ seed; for (int i = 0; i < 20; i++) next(); } inline uint64_t next() { x = x ^ (x << 7); return x = x ^ (x >> 9); } inline int next_int() { return next() & M; } inline int next_int(int mod) { return next() % mod; } inline int next_int(int l, int r) { return l + next_int(r - l + 1); } inline double next_double() { return next_int() * e; } }; /** shuffle **/ template void shuffle_vector(std::vector& v, Xorshift& rnd) { int n = v.size(); for (int i = n - 1; i >= 1; i--) { int r = rnd.next_int(i); std::swap(v[i], v[r]); } } /** split **/ std::vector split(std::string str, const std::string& delim) { for (char& c : str) if (delim.find(c) != std::string::npos) c = ' '; std::istringstream iss(str); std::vector parsed; std::string buf; while (iss >> buf) parsed.push_back(buf); return parsed; } /** misc **/ template inline void Fill(A(&array)[N], const T& val) { std::fill((T*)array, (T*)(array + N), val); } // fill array template auto make_vector(T x, int arg, Args ...args) { if constexpr (sizeof...(args) == 0)return std::vector(arg, x); else return std::vector(arg, make_vector(x, args...)); } template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; } template bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } return false; } /** using **/ using ll = long long; using ld = long double; //using ld = boost::multiprecision::cpp_bin_float_quad; using pii = std::pair; using pll = std::pair; using std::cin, std::cout, std::cerr, std::endl, std::string, std::vector, std::array; constexpr int N = 200000; constexpr int Q = 200000; double ltbl[0x10000]; // hyper params bool optuna_mode = false; int weight_type = -1; int bucket_size = 1718; double start_temp = 156.71493463839815; double end_temp = 0; int range_max = 35; int bucket_size_opt[3] = { 1718, 1299, 1638 }; double start_temp_opt[3] = { 156.71493463839815, 107.94021671154113, 130.36303804130475 }; int range_max_opt[3] = { 35, 30, 32 }; void initialize(int argc, char** argv) { Xorshift rnd; for(int i = 0; i < 1000; i++) rnd.next_int(); for(int i = 0; i < 0x10000; i++) ltbl[i] = log(rnd.next_double()); if (argc > 1 && string(argv[1]) == "optuna") { optuna_mode = true; weight_type = std::stoi(argv[2]); // 1,2,3 bucket_size = std::stoi(argv[3]); start_temp = std::stod(argv[4]); range_max = std::stoi(argv[5]); } } struct Result { int64_t score; int64_t loop; }; struct State { Xorshift rnd; int ST; int L[Q], R[Q]; int P[Q]; int64_t cost; State(std::istream& in) { in >> ST >> ST >> ST >> ST; if (!optuna_mode) { bucket_size = bucket_size_opt[ST - 1]; start_temp = start_temp_opt[ST - 1]; range_max = range_max_opt[ST - 1]; } for (int i = 0; i < N; i++) in >> L[0]; // ignore weights for (int i = 0; i < Q; i++) in >> L[i] >> R[i]; std::iota(P, P + Q, 0); std::sort(P, P + Q, [&](int a, int b) { int ablock = L[a] / bucket_size, bblock = L[b] / bucket_size; if (ablock != bblock) return ablock < bblock; return (ablock & 1) ? R[a] > R[b] : R[a] < R[b]; }); cost = R[P[0]] - L[P[0]] + 1; for (int k = 0; k < Q - 1; k++) cost += calc_adj_cost(k); } Result run(double duration) { Timer timer; int64_t loop = 0; auto get_temp = [](double start_temp, double end_temp, double now_time, double end_time) { return end_temp + (start_temp - end_temp) * (end_time - now_time) / end_time; }; double now_time = timer.elapsed_ms(); double temp = get_temp(start_temp, end_temp, now_time, duration); while(true) { loop++; int l = loop % (Q - 1), r = std::min(Q - 1, l + rnd.next_int(1, range_max)); int diff = calc_rev_cost(l, r); if (-diff > temp * ltbl[loop & 0xFFFF]) { std::reverse(P + l, P + r + 1); cost += diff; } //if (!(loop & 0xFFFFFF)) { // dump(loop, temp, cost); //} if (!(loop & 0xFFFF)) { now_time = timer.elapsed_ms(); if (now_time > duration) break; temp = get_temp(start_temp, end_temp, now_time, duration); } } return { eval(), loop }; } void run(int num_loop) { auto get_temp = [](double start_temp, double end_temp, double now_time, double end_time) { return end_temp + (start_temp - end_temp) * (end_time - now_time) / end_time; }; double temp = get_temp(start_temp, end_temp, 0, num_loop); for (int loop = 0; loop < num_loop; loop++) { int l = loop % (Q - 1), r = std::min(Q - 1, l + rnd.next_int(1, range_max)); int diff = calc_rev_cost(l, r); if (-diff > temp * ltbl[loop & 0xFFFF]) { std::reverse(P + l, P + r + 1); cost += diff; } if (!(loop & 0xFFFF)) temp = get_temp(start_temp, end_temp, loop, num_loop); } } inline int calc_adj_cost(int k) const { return abs(L[P[k]] - L[P[k + 1]]) + abs(R[P[k]] - R[P[k + 1]]); } int calc_rev_cost(int l, int r) { int diff = 0; diff -= l ? calc_adj_cost(l - 1) : (R[P[0]] - L[P[0]] + 1); diff -= (r + 1 < Q) ? calc_adj_cost(r) : 0; std::swap(P[l], P[r]); diff += l ? calc_adj_cost(l - 1) : (R[P[0]] - L[P[0]] + 1); diff += (r + 1 < Q) ? calc_adj_cost(r) : 0; std::swap(P[l], P[r]); return diff; } int64_t eval() const { return std::min((ll)std::round(1e16 / cost), (ll)1e16); } void output(std::ostream& out) const { out << P[0] + 1; for (int i = 1; i < Q; i++) out << ' ' << P[i] + 1; out << '\n'; } }; int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv) { Timer timer; #ifdef HAVE_OPENCV_HIGHGUI cv::utils::logging::setLogLevel(cv::utils::logging::LogLevel::LOG_LEVEL_SILENT); #endif #ifdef _MSC_VER std::ifstream ifs("../tools/input1/0002.txt"); std::istream& in = ifs; std::ofstream ofs("../tools/output1/0002.txt"); std::ostream& out = ofs; #else std::istream& in = cin; std::ostream& out = cout; #endif initialize(argc, argv); if (optuna_mode) { #ifdef _MSC_VER vector paths; for (int s = weight_type; s <= 30; s += 3) { paths.push_back(format("G:\\dev\\heuristic\\yukicoder5010\\tools\\input1\\%04d.txt", s)); } vector scores(paths.size()); int batch_size = 5; for (int begin = 0; begin < paths.size(); begin += batch_size) { int end = std::min((int)paths.size(), begin + batch_size); concurrency::critical_section mtx; concurrency::parallel_for(begin, end, [&](int i) { std::ifstream ifs(paths[i]); State state(ifs); auto [score, loop] = state.run(4900.0); scores[i] = score; { mtx.lock(); cerr << format("case %2d: score=%lld, loop=%lld\n", i, scores[i], loop); mtx.unlock(); } }); } cout << (double)std::accumulate(scores.begin(), scores.end(), 0LL) / scores.size() << '\n'; #endif } else { State state(in); dump(state.cost, state.eval()); state.run(4900 - timer.elapsed_ms()); dump(state.cost, state.eval()); state.output(out); } return 0; }