Guest User

Untitled

a guest
Nov 29th, 2025
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.40 KB | None | 0 0
  1. #pragma GCC optimize("Ofast,unroll-loops")
  2.  
  3. #include "bits/stdc++.h"
  4.  
  5. //#define NDEBUG
  6. #define F first
  7. #define S second
  8. #define vec vector
  9. #define pb push_back
  10. #define pll pair<ll, ll>
  11. #define all(m) m.begin(), m.end()
  12. #define rall(m) m.rbegin(), m.rend()
  13. #define uid uniform_int_distribution
  14. #define timeStamp() std::chrono::steady_clock::now()
  15. #define unify(m) sort(all(m)), m.erase(unique(all(m)), m.end());
  16. #define duration_micro(a) chrono::duration_cast<chrono::microseconds>(a).count()
  17. #define duration_milli(a) chrono::duration_cast<chrono::milliseconds>(a).count()
  18. #define fast cin.tie(0), cout.tie(0), cin.sync_with_stdio(0), cout.sync_with_stdio(0);
  19. using namespace std;
  20. using str = string;
  21. using ll = long long;
  22. using ld = long double;
  23. mt19937 rnd(timeStamp().time_since_epoch().count());
  24. mt19937_64 rndll(timeStamp().time_since_epoch().count());
  25. template<typename T, typename U> __attribute__((always_inline)) inline bool chmin(T& a, const U& b) {return (T)b < a ? a = b, 1 : 0;}
  26. template<typename T, typename U> bool chmax(T& a, const U& b) {return (T)b > a ? a = b, 1 : 0;}
  27. struct custom_hash {static uint64_t xs(uint64_t x) {x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31);} template<typename T> size_t operator()(T x) const {static const uint64_t C = timeStamp().time_since_epoch().count(); return xs(hash<T> {}(x) + C);}};
  28. template<typename K> using uset = unordered_set<K, custom_hash>;
  29. template<typename K, typename V> using umap = unordered_map<K, V, custom_hash>;
  30. template<typename T1, typename T2> ostream& operator<<(ostream& out, const pair<T1, T2>& x) {return out << x.F << ' ' << x.S;}
  31. template<typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& x) {return in >> x.F >> x.S;}
  32. template<typename T, size_t N> istream& operator>>(istream& in, array<T, N>& a) {for (auto &x : a) in >> x; return in;}
  33. template<typename T, size_t N> ostream& operator<<(ostream& out, const array<T, N>& a) {for (size_t i = 0; i < a.size(); ++i) {out << a[i]; if (i + 1 < a.size()) out << ' ';} return out;}
  34. template<typename T> istream& operator>>(istream& in, vector<T>& a) {for (auto& x : a) in >> x; return in;}
  35. template<typename T> ostream& operator<<(ostream& out, const vector<T>& a) {for (size_t i = 0; i < a.size(); ++i) {out << a[i]; if (i + 1 < a.size()) out << ' ';} return out;}
  36.  
  37. mutex mtx;
  38. void common() {
  39.  
  40. }
  41. const ll inf = 1ll << 48;
  42.  
  43. class TestCase {
  44.     int num;
  45.     double weight = 1;
  46.     stringstream out;
  47.  
  48. public:
  49.     TestCase(int num): num(num) {}
  50.     double get_weight() const {return weight;}
  51.     int get_number() const {return num;}
  52.     string get_output() const {return out.str();}
  53.  
  54.     ll a; vec<ll> mm, n;
  55.     void read_input() {
  56.         cin >> a; mm.resize(a), n.resize(a); cin >> mm >> n;
  57.     }
  58.  
  59.     void solve() {
  60.         uint64_t m[6000]; for (int q = 0; q < a; ++q) m[q] = mm[q];
  61.         vec dp(2, vec(a + 1, vec<uint64_t>()));
  62.         for (int q = 0; q < 2; ++q) {
  63.             for (int e = 0; e <= a; ++e) {
  64.                 dp[q][e].resize(e + 1, inf);
  65.             }
  66.         }
  67.         if (n[0] == 0) dp[0][0][0] = 0;
  68.         dp[0][1][1] = m[0];
  69.         for (int q = 0; q + 1 < a; ++q) {
  70.             const int qq = q & 1;
  71.             auto& dpc = dp[qq];
  72.             auto& dpn = dp[qq ^ 1];
  73.             for (int e = 0; e <= q + 1; ++e) {
  74.                 fill(dpn[e].begin(), dpn[e].begin() + min(e + 1, q + 2), inf);
  75.             }
  76.             for (int e = n[q]; e <= q + 1; ++e) {
  77.                 for (int w = 0; w < e; ++w) {
  78.                     chmin(dpn[e][0], dpc[e][w]);
  79.                     chmin(dpn[e][w + 1], dpc[e][w] + m[q + 1]);
  80.                 }
  81.                 for (int w = e; w <= e; ++w) {
  82.                     chmin(dpn[e][0], dpc[e][w]);
  83.                     chmin(dpn[e + 1][w + 1], dpc[e][w] + m[q + 1]);
  84.                 }
  85.             }
  86.         }
  87.         ll best = inf;
  88.         for (int e = n[a - 1]; e <= a; ++e) {
  89.             for (int w = 0; w <= e; ++w) {
  90.                 chmin(best, dp[(a - 1) & 1][e][w]);
  91.             }
  92.         }
  93.         out << best;
  94.     }
  95. };
  96.  
  97. int main() {
  98.     cin.tie(0), cout.tie(0), cin.sync_with_stdio(0), cout.sync_with_stdio(0);
  99.     common();
  100.     int T; cin >> T;
  101.     vector<TestCase*> tests(T);
  102.     double W = 0;
  103.     for (int i = 0; i < T; ++i) {
  104.         tests[i] = new TestCase(i);
  105.         tests[i]->read_input();
  106.         W += tests[i]->get_weight();
  107.     }
  108.     int THR = 3;
  109.     vector<thread> threads(THR);
  110.     cerr << "Threads = " << threads.size() << endl;
  111.     for (auto& thr : threads) {
  112.         static auto st = std::chrono::steady_clock::now();
  113.         static double w = 0;
  114.         static atomic<int> tc = 0;
  115.         thr = thread([&]() {
  116.             while (tc < T) {
  117.                 int cur = tc++;
  118.                 tests[cur]->solve();
  119.                 mtx.lock();
  120.                 double pw = w;
  121.                 if (w += tests[cur]->get_weight() / W; int(w * 100 + 1e-9) > int(pw * 100 + 1e-9)) {
  122.                     cerr << "Done " << w * 100 << "% in " << chrono::duration_cast<chrono::milliseconds>(std::chrono::steady_clock::now() - st).count() / 1e3 << "s" << endl;
  123.                 }
  124.                 mtx.unlock();
  125.             }
  126.         });
  127.     }
  128.     for (auto& thr : threads) thr.join();
  129.     for (int i = 0; i < T; ++i) {
  130.         cout << "Case #" << i + 1 << ": " << tests[i]->get_output() << '\n';
  131.     }
  132. }
  133.  
Advertisement
Add Comment
Please, Sign In to add comment