Submission #1073313

# Submission time Handle Problem Language Result Execution time Memory
1073313 2024-08-24T12:12:03 Z vjudge1 Tricks of the Trade (CEOI23_trade) C++17
55 / 100
2690 ms 2097152 KB
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;

using ll = long long;
using ld = long double;

#define int ll
#define sz(x) ((int)(x).size())

using pii = pair<int,int>;
using tii = tuple<int,int,int>;

const int nmax = 25e4 + 5;

int K;

template<typename T>
struct Deq {
   vector<T> v;
   void push_front(T x) { v.emplace_back(x); }
   T& operator[](const int& x) { return rbegin(v)[x]; }
   void resize(int k) {
      if(k >= sz(v)) return;
      v.erase(begin(v), begin(v) + sz(v) - k);
      return;
   }
   int size() { return sz(v); }
}; // trebuie facut persistent

struct Tree {
   vector<int> g[nmax];
   void add_edge(int a, int b) {
      g[a].emplace_back(b);
   }
   
   struct mdeq : Deq<ll> {
      ll lazy = 0;
   };
   
   mdeq dp[nmax];
   vector<bool> merge[nmax];
   int calculated[nmax];
   Tree(int n) {
      memset(calculated, 0, sizeof(calculated));
   }
   template<class CB, class CB2> void calcdp(CB&& cost, CB2 atr, int node) {
      if(calculated[node]) return;
      calculated[node] = 1;
      
      
      for(auto x : g[node]) {
         calcdp(cost, atr, x);
         bool swapped = 0;
         
         auto T = dp[x];
         T.resize(K + 1);
         
         if(sz(T) > sz(dp[node]))
            swap(T, dp[node]);
            
         for(int i = 0; i < sz(T); i++)
            dp[node][i] = max(T[i] + T.lazy - dp[node].lazy, dp[node][i]);
      }
      dp[node].push_front(cost(node) - dp[node].lazy);
      for(auto &x : dp[node].v) x += atr(node);
      dp[node].lazy = 0;
      merge[node].assign(sz(dp[node]), 0);
      return;
   }
   template<class CB> void propmerge(CB&& atr, vector<int> order) {
      for(auto node : order) {
         for(auto x : g[node]) {
            auto T = dp[x];
            for(int i = 0; i < sz(T) && i + 1 < sz(dp[node]); i++) {
               if(T[i] + T.lazy + atr(node) == dp[node][i + 1] +  dp[node].lazy) {
                  merge[x][i] = merge[x][i] || merge[node][i + 1];
                  //if(x == 66)
                     //cerr << merge[node][i + 1] << ' ' << node << ' ' << i + 1 << " -> " << x << ' ' << i << '\n';
               }
            }
         }
      }
      return;
   }
};

ll spart[nmax];

signed main() {
   cin.tie(0) -> sync_with_stdio(0);
   int n;
   cin >> n >> K;
   
   for(int i = 1, x; i <= n; i++) {
      cin >> spart[i];
      spart[i] += spart[i - 1];
   }
   
   vector<int> v(n + 1);
   for(int i = 1; i <= n; i++) cin >> v[i];
   //for(int i = 1; i <= n; i++) {
      //if(v[i] >= 10000)cerr << i << ' ';
   //}cerr << '\n';
   Tree toleft(n), toright(n);
   
   vector<int> st;
   
   for(int i = 1; i <= n; i++) {
      while(sz(st) && v[st.back()] <= v[i]) { toright.add_edge(st.back(), i); toleft.add_edge(i, st.back()); st.pop_back(); }
      toleft.add_edge(0, i);
      if(sz(st)) toright.add_edge(st.back(), i), toleft.add_edge(i, st.back());
      st.emplace_back(i);
   }
   st.clear();
   
   for(int i = n; i > 0; i--) {
      while(sz(st) && v[st.back()] <= v[i]) { toleft.add_edge(st.back(), i); toright.add_edge(i, st.back()); st.pop_back(); }
      toright.add_edge(0, i);
      if(sz(st)) toleft.add_edge(st.back(), i), toright.add_edge(i, st.back());
      st.emplace_back(i);
   }
   st.clear();
   
   toleft.calcdp([&](int x) { return x == 0? 0 : spart[x - 1]; }, [&](int x) { return v[x]; }, 0);
   toright.calcdp([&](int x) { return x == 0? 0 : -spart[x]; }, [&](int x) { return v[x]; }, 0);

   ll best_cost = -1e18;

   for(int P = 1; P <= n; P++) {
      auto& X = toleft.dp[P];
      auto& Y = toright.dp[P];
      bool swapped = 0;
      if(sz(X) > sz(Y)) swapped = 1, swap(X, Y);
      
      for(int i = 0; i < min(K, sz(X)); i++) {
         int j = K - i - 1;
         if(j >= sz(Y)) continue;
         
         best_cost = max(best_cost, X[i] + Y[j] + X.lazy + Y.lazy - v[P]);
      } 
      if(swapped) swap(X, Y);
   }
   
   for(int P = 1; P <= n; P++) {
      auto& X = toleft.dp[P];
      auto& Y = toright.dp[P];
      bool swapped = 0;
      if(sz(X) > sz(Y)) swapped = 1, swap(X, Y);
      
      
      for(int i = 0; i < min(K, sz(X)); i++) {
         int j = K - i - 1;
         if(j >= sz(Y)) continue;
         
         if(best_cost == X[i] + Y[j] + X.lazy + Y.lazy - v[P]) {
            if(!swapped)
               toleft.merge[P][i] = 1, toright.merge[P][j] = 1;
            else
               toright.merge[P][i] = 1, toleft.merge[P][j] = 1;
         }  
      }
      if(swapped) swap(X, Y);
   }
   
   vector<int> order(n);
   iota(all(order), 1);
   toright.propmerge([&](int x) { return v[x]; }, order);
   
   reverse(all(order));
   toleft.propmerge([&](int x) { return v[x]; }, order);
   
   cout << best_cost << '\n';
   for(int i = 1; i <= n; i++) {
      bool ok = 0;
      for(auto x : toleft.merge[i]) {
         if(ok) break;
         if(x) { ok = 1; break; }
      }
      for(auto x : toright.merge[i]) {
         if(ok) break;
         if(x) { ok = 1; break; }
      }
      cout << ok;
      //if(!ok)cerr << i << '\n';
   }
   cout << '\n';
}


/**
      Töte es durch genaue Untersuchung\Töte es kann es nur noch schlimmer machen\Es lässt es irgendwie atmen
--
*/ 

Compilation message

trade.cpp: In function 'int main()':
trade.cpp:95:19: warning: unused variable 'x' [-Wunused-variable]
   95 |    for(int i = 1, x; i <= n; i++) {
      |                   ^
trade.cpp: In instantiation of 'void Tree::calcdp(CB&&, CB2, ll) [with CB = main()::<lambda(ll)>; CB2 = main()::<lambda(ll)>; ll = long long int]':
trade.cpp:125:97:   required from here
trade.cpp:54:15: warning: unused variable 'swapped' [-Wunused-variable]
   54 |          bool swapped = 0;
      |               ^~~~~~~
trade.cpp: In instantiation of 'void Tree::calcdp(CB&&, CB2, ll) [with CB = main()::<lambda(ll)>; CB2 = main()::<lambda(ll)>; ll = long long int]':
trade.cpp:126:95:   required from here
trade.cpp:54:15: warning: unused variable 'swapped' [-Wunused-variable]
trade.cpp: In instantiation of 'void Tree::calcdp(CB&&, CB2, ll) [with CB = main()::<lambda(ll)>&; CB2 = main()::<lambda(ll)>; ll = long long int]':
trade.cpp:53:16:   required from 'void Tree::calcdp(CB&&, CB2, ll) [with CB = main()::<lambda(ll)>; CB2 = main()::<lambda(ll)>; ll = long long int]'
trade.cpp:125:97:   required from here
trade.cpp:54:15: warning: unused variable 'swapped' [-Wunused-variable]
trade.cpp: In instantiation of 'void Tree::calcdp(CB&&, CB2, ll) [with CB = main()::<lambda(ll)>&; CB2 = main()::<lambda(ll)>; ll = long long int]':
trade.cpp:53:16:   required from 'void Tree::calcdp(CB&&, CB2, ll) [with CB = main()::<lambda(ll)>; CB2 = main()::<lambda(ll)>; ll = long long int]'
trade.cpp:126:95:   required from here
trade.cpp:54:15: warning: unused variable 'swapped' [-Wunused-variable]
# Verdict Execution time Memory Grader output
1 Correct 32 ms 51284 KB Output is correct
2 Correct 36 ms 51284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 51288 KB Output is correct
2 Correct 33 ms 51216 KB Output is correct
3 Correct 34 ms 51116 KB Output is correct
4 Correct 34 ms 51572 KB Output is correct
5 Correct 35 ms 52052 KB Output is correct
6 Correct 34 ms 51284 KB Output is correct
7 Correct 38 ms 51484 KB Output is correct
8 Correct 35 ms 51536 KB Output is correct
9 Correct 35 ms 51540 KB Output is correct
10 Correct 36 ms 51288 KB Output is correct
11 Correct 35 ms 51292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 51288 KB Output is correct
2 Correct 33 ms 51216 KB Output is correct
3 Correct 34 ms 51116 KB Output is correct
4 Correct 34 ms 51572 KB Output is correct
5 Correct 35 ms 52052 KB Output is correct
6 Correct 34 ms 51284 KB Output is correct
7 Correct 38 ms 51484 KB Output is correct
8 Correct 35 ms 51536 KB Output is correct
9 Correct 35 ms 51540 KB Output is correct
10 Correct 36 ms 51288 KB Output is correct
11 Correct 35 ms 51292 KB Output is correct
12 Correct 40 ms 51280 KB Output is correct
13 Correct 33 ms 51292 KB Output is correct
14 Correct 33 ms 51292 KB Output is correct
15 Correct 37 ms 51544 KB Output is correct
16 Correct 35 ms 52052 KB Output is correct
17 Correct 35 ms 51296 KB Output is correct
18 Correct 36 ms 51536 KB Output is correct
19 Correct 40 ms 51552 KB Output is correct
20 Correct 32 ms 51392 KB Output is correct
21 Correct 35 ms 51308 KB Output is correct
22 Correct 35 ms 51288 KB Output is correct
23 Correct 839 ms 467312 KB Output is correct
24 Correct 40 ms 53072 KB Output is correct
25 Correct 260 ms 147672 KB Output is correct
26 Correct 66 ms 62288 KB Output is correct
27 Correct 628 ms 277840 KB Output is correct
28 Correct 44 ms 53100 KB Output is correct
29 Correct 608 ms 308540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 51292 KB Output is correct
2 Correct 263 ms 124960 KB Output is correct
3 Correct 424 ms 131872 KB Output is correct
4 Correct 279 ms 129888 KB Output is correct
5 Correct 335 ms 129040 KB Output is correct
6 Correct 319 ms 129448 KB Output is correct
7 Correct 356 ms 131100 KB Output is correct
8 Correct 349 ms 131608 KB Output is correct
9 Correct 365 ms 129980 KB Output is correct
10 Correct 290 ms 127236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 51292 KB Output is correct
2 Correct 263 ms 124960 KB Output is correct
3 Correct 424 ms 131872 KB Output is correct
4 Correct 279 ms 129888 KB Output is correct
5 Correct 335 ms 129040 KB Output is correct
6 Correct 319 ms 129448 KB Output is correct
7 Correct 356 ms 131100 KB Output is correct
8 Correct 349 ms 131608 KB Output is correct
9 Correct 365 ms 129980 KB Output is correct
10 Correct 290 ms 127236 KB Output is correct
11 Correct 34 ms 51344 KB Output is correct
12 Correct 263 ms 124960 KB Output is correct
13 Correct 377 ms 131864 KB Output is correct
14 Correct 292 ms 129800 KB Output is correct
15 Correct 307 ms 129104 KB Output is correct
16 Correct 308 ms 129448 KB Output is correct
17 Correct 353 ms 131212 KB Output is correct
18 Correct 376 ms 131708 KB Output is correct
19 Correct 351 ms 129948 KB Output is correct
20 Correct 293 ms 127260 KB Output is correct
21 Correct 33 ms 51288 KB Output is correct
22 Correct 41 ms 51352 KB Output is correct
23 Correct 33 ms 51616 KB Output is correct
24 Correct 35 ms 52048 KB Output is correct
25 Correct 34 ms 51292 KB Output is correct
26 Correct 31 ms 51548 KB Output is correct
27 Correct 30 ms 51536 KB Output is correct
28 Correct 33 ms 51548 KB Output is correct
29 Correct 45 ms 51304 KB Output is correct
30 Correct 34 ms 51288 KB Output is correct
31 Correct 2452 ms 909168 KB Output is correct
32 Correct 319 ms 129180 KB Output is correct
33 Correct 2561 ms 915948 KB Output is correct
34 Correct 2505 ms 915784 KB Output is correct
35 Correct 2457 ms 915772 KB Output is correct
36 Correct 2501 ms 915240 KB Output is correct
37 Correct 2475 ms 916264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 51284 KB Output is correct
2 Correct 36 ms 51284 KB Output is correct
3 Correct 34 ms 51288 KB Output is correct
4 Correct 33 ms 51216 KB Output is correct
5 Correct 34 ms 51116 KB Output is correct
6 Correct 34 ms 51572 KB Output is correct
7 Correct 35 ms 52052 KB Output is correct
8 Correct 34 ms 51284 KB Output is correct
9 Correct 38 ms 51484 KB Output is correct
10 Correct 35 ms 51536 KB Output is correct
11 Correct 35 ms 51540 KB Output is correct
12 Correct 36 ms 51288 KB Output is correct
13 Correct 35 ms 51292 KB Output is correct
14 Correct 40 ms 51280 KB Output is correct
15 Correct 33 ms 51292 KB Output is correct
16 Correct 33 ms 51292 KB Output is correct
17 Correct 37 ms 51544 KB Output is correct
18 Correct 35 ms 52052 KB Output is correct
19 Correct 35 ms 51296 KB Output is correct
20 Correct 36 ms 51536 KB Output is correct
21 Correct 40 ms 51552 KB Output is correct
22 Correct 32 ms 51392 KB Output is correct
23 Correct 35 ms 51308 KB Output is correct
24 Correct 35 ms 51288 KB Output is correct
25 Correct 839 ms 467312 KB Output is correct
26 Correct 40 ms 53072 KB Output is correct
27 Correct 260 ms 147672 KB Output is correct
28 Correct 66 ms 62288 KB Output is correct
29 Correct 628 ms 277840 KB Output is correct
30 Correct 44 ms 53100 KB Output is correct
31 Correct 608 ms 308540 KB Output is correct
32 Correct 34 ms 51292 KB Output is correct
33 Correct 263 ms 124960 KB Output is correct
34 Correct 424 ms 131872 KB Output is correct
35 Correct 279 ms 129888 KB Output is correct
36 Correct 335 ms 129040 KB Output is correct
37 Correct 319 ms 129448 KB Output is correct
38 Correct 356 ms 131100 KB Output is correct
39 Correct 349 ms 131608 KB Output is correct
40 Correct 365 ms 129980 KB Output is correct
41 Correct 290 ms 127236 KB Output is correct
42 Correct 34 ms 51344 KB Output is correct
43 Correct 263 ms 124960 KB Output is correct
44 Correct 377 ms 131864 KB Output is correct
45 Correct 292 ms 129800 KB Output is correct
46 Correct 307 ms 129104 KB Output is correct
47 Correct 308 ms 129448 KB Output is correct
48 Correct 353 ms 131212 KB Output is correct
49 Correct 376 ms 131708 KB Output is correct
50 Correct 351 ms 129948 KB Output is correct
51 Correct 293 ms 127260 KB Output is correct
52 Correct 33 ms 51288 KB Output is correct
53 Correct 41 ms 51352 KB Output is correct
54 Correct 33 ms 51616 KB Output is correct
55 Correct 35 ms 52048 KB Output is correct
56 Correct 34 ms 51292 KB Output is correct
57 Correct 31 ms 51548 KB Output is correct
58 Correct 30 ms 51536 KB Output is correct
59 Correct 33 ms 51548 KB Output is correct
60 Correct 45 ms 51304 KB Output is correct
61 Correct 34 ms 51288 KB Output is correct
62 Correct 2452 ms 909168 KB Output is correct
63 Correct 319 ms 129180 KB Output is correct
64 Correct 2561 ms 915948 KB Output is correct
65 Correct 2505 ms 915784 KB Output is correct
66 Correct 2457 ms 915772 KB Output is correct
67 Correct 2501 ms 915240 KB Output is correct
68 Correct 2475 ms 916264 KB Output is correct
69 Correct 34 ms 51280 KB Output is correct
70 Correct 306 ms 124952 KB Output is correct
71 Correct 355 ms 131848 KB Output is correct
72 Correct 292 ms 129904 KB Output is correct
73 Correct 304 ms 129036 KB Output is correct
74 Correct 315 ms 129496 KB Output is correct
75 Correct 347 ms 131212 KB Output is correct
76 Correct 357 ms 131608 KB Output is correct
77 Correct 348 ms 130072 KB Output is correct
78 Correct 281 ms 127472 KB Output is correct
79 Correct 33 ms 51288 KB Output is correct
80 Correct 32 ms 51284 KB Output is correct
81 Correct 37 ms 51536 KB Output is correct
82 Correct 34 ms 52052 KB Output is correct
83 Correct 33 ms 51284 KB Output is correct
84 Correct 35 ms 51536 KB Output is correct
85 Correct 34 ms 51548 KB Output is correct
86 Correct 35 ms 51532 KB Output is correct
87 Correct 35 ms 51396 KB Output is correct
88 Correct 32 ms 51280 KB Output is correct
89 Correct 2690 ms 909144 KB Output is correct
90 Correct 311 ms 129036 KB Output is correct
91 Correct 2594 ms 916196 KB Output is correct
92 Correct 2539 ms 915748 KB Output is correct
93 Correct 2420 ms 916104 KB Output is correct
94 Correct 2470 ms 915224 KB Output is correct
95 Correct 2387 ms 916252 KB Output is correct
96 Correct 805 ms 467292 KB Output is correct
97 Correct 37 ms 53076 KB Output is correct
98 Correct 251 ms 147796 KB Output is correct
99 Correct 62 ms 62508 KB Output is correct
100 Correct 568 ms 277844 KB Output is correct
101 Correct 40 ms 53080 KB Output is correct
102 Correct 628 ms 308432 KB Output is correct
103 Runtime error 2480 ms 2097152 KB Execution killed with signal 9
104 Halted 0 ms 0 KB -