답안 #1073214

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1073214 2024-08-24T10:32:54 Z vjudge1 Tricks of the Trade (CEOI23_trade) C++17
30 / 100
1716 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];
   int p[nmax];
   void add_edge(int a, int b) {
      g[a].emplace_back(b);
      p[b] = a;
   }
   
   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));
      for(int i = 0; i <= n; i++) merge[i].assign(K + 1, 0);
   }
   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);
         dp[x].resize(K);
         bool swapped = 0;
         
         auto T = dp[x];
         
         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);
      dp[node].lazy += atr(node);
      return;
   }
   void propmerge(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 - dp[node].lazy == dp[node][i + 1])
                  merge[x][i] = merge[x][i] || merge[node][i + 1];
            }
         }
      }
      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];
   Tree toleft(n), toright(n);
   
   vector<int> st;
   
   for(int i = 1; i <= n; i++) {
      while(sz(st) && v[st.back()] <= v[i]) st.pop_back();
      if(sz(st) == 0) toleft.add_edge(0, i);
      else toleft.add_edge(st.back(), i), toright.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]) st.pop_back();
      if(sz(st) == 0) toright.add_edge(0, i);
      else toright.add_edge(st.back(), i), toleft.add_edge(i, st.back());
      st.emplace_back(i);
   }
   st.clear();
   
   toleft.calcdp([&](int x) { return x == 0? 0 : -spart[x]; }, [&](int x) { return v[x]; }, 0);
   toright.calcdp([&](int x) { return x == 0? 0 : spart[x - 1]; }, [&](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];
      if(sz(X) > sz(Y)) 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]);
      } 
   }
   
   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;
         }  
      }
   }
   
   vector<int> order(n);
   iota(all(order), 1);
   toleft.propmerge(order);
   
   reverse(all(order));
   toright.propmerge(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;
   }
   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:93:19: warning: unused variable 'x' [-Wunused-variable]
   93 |    for(int i = 1, x; i <= n; i++) {
      |                   ^
trade.cpp: In instantiation of 'void Tree::calcdp(CB&&, CB2, int) [with CB = main()::<lambda(int)>; CB2 = main()::<lambda(int)>]':
trade.cpp:120:94:   required from here
trade.cpp:58:15: warning: unused variable 'swapped' [-Wunused-variable]
   58 |          bool swapped = 0;
      |               ^~~~~~~
trade.cpp: In instantiation of 'void Tree::calcdp(CB&&, CB2, int) [with CB = main()::<lambda(int)>; CB2 = main()::<lambda(int)>]':
trade.cpp:121:98:   required from here
trade.cpp:58:15: warning: unused variable 'swapped' [-Wunused-variable]
trade.cpp: In instantiation of 'void Tree::calcdp(CB&&, CB2, int) [with CB = main()::<lambda(int)>&; CB2 = main()::<lambda(int)>]':
trade.cpp:56:16:   required from 'void Tree::calcdp(CB&&, CB2, int) [with CB = main()::<lambda(int)>; CB2 = main()::<lambda(int)>]'
trade.cpp:120:94:   required from here
trade.cpp:58:15: warning: unused variable 'swapped' [-Wunused-variable]
trade.cpp: In instantiation of 'void Tree::calcdp(CB&&, CB2, int) [with CB = main()::<lambda(int)>&; CB2 = main()::<lambda(int)>]':
trade.cpp:56:16:   required from 'void Tree::calcdp(CB&&, CB2, int) [with CB = main()::<lambda(int)>; CB2 = main()::<lambda(int)>]'
trade.cpp:121:98:   required from here
trade.cpp:58:15: warning: unused variable 'swapped' [-Wunused-variable]
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 51168 KB Output is correct
2 Correct 32 ms 51112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 51320 KB Output is correct
2 Correct 32 ms 51120 KB Output is correct
3 Correct 35 ms 51088 KB Output is correct
4 Correct 30 ms 51796 KB Output is correct
5 Correct 31 ms 52060 KB Output is correct
6 Correct 29 ms 51288 KB Output is correct
7 Correct 28 ms 51540 KB Output is correct
8 Correct 29 ms 51856 KB Output is correct
9 Partially correct 34 ms 51540 KB Partially correct
10 Correct 30 ms 51548 KB Output is correct
11 Partially correct 29 ms 51468 KB Partially correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 51320 KB Output is correct
2 Correct 32 ms 51120 KB Output is correct
3 Correct 35 ms 51088 KB Output is correct
4 Correct 30 ms 51796 KB Output is correct
5 Correct 31 ms 52060 KB Output is correct
6 Correct 29 ms 51288 KB Output is correct
7 Correct 28 ms 51540 KB Output is correct
8 Correct 29 ms 51856 KB Output is correct
9 Partially correct 34 ms 51540 KB Partially correct
10 Correct 30 ms 51548 KB Output is correct
11 Partially correct 29 ms 51468 KB Partially correct
12 Correct 30 ms 51320 KB Output is correct
13 Correct 29 ms 51320 KB Output is correct
14 Correct 32 ms 51236 KB Output is correct
15 Correct 29 ms 51804 KB Output is correct
16 Correct 47 ms 52020 KB Output is correct
17 Correct 36 ms 51316 KB Output is correct
18 Correct 36 ms 51428 KB Output is correct
19 Correct 40 ms 51780 KB Output is correct
20 Partially correct 33 ms 51532 KB Partially correct
21 Correct 30 ms 51548 KB Output is correct
22 Partially correct 29 ms 51596 KB Partially correct
23 Correct 439 ms 417296 KB Output is correct
24 Correct 34 ms 53664 KB Output is correct
25 Correct 177 ms 187908 KB Output is correct
26 Correct 56 ms 71976 KB Output is correct
27 Correct 320 ms 327824 KB Output is correct
28 Correct 33 ms 53556 KB Output is correct
29 Correct 321 ms 321036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 51288 KB Output is correct
2 Correct 234 ms 148216 KB Output is correct
3 Correct 322 ms 149632 KB Output is correct
4 Correct 248 ms 150640 KB Output is correct
5 Correct 230 ms 150176 KB Output is correct
6 Correct 214 ms 150376 KB Output is correct
7 Correct 262 ms 149572 KB Output is correct
8 Correct 249 ms 149376 KB Output is correct
9 Correct 260 ms 148412 KB Output is correct
10 Correct 218 ms 149156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 51288 KB Output is correct
2 Correct 234 ms 148216 KB Output is correct
3 Correct 322 ms 149632 KB Output is correct
4 Correct 248 ms 150640 KB Output is correct
5 Correct 230 ms 150176 KB Output is correct
6 Correct 214 ms 150376 KB Output is correct
7 Correct 262 ms 149572 KB Output is correct
8 Correct 249 ms 149376 KB Output is correct
9 Correct 260 ms 148412 KB Output is correct
10 Correct 218 ms 149156 KB Output is correct
11 Correct 31 ms 51256 KB Output is correct
12 Correct 229 ms 148216 KB Output is correct
13 Correct 264 ms 149712 KB Output is correct
14 Correct 222 ms 150644 KB Output is correct
15 Correct 225 ms 150016 KB Output is correct
16 Correct 220 ms 150344 KB Output is correct
17 Correct 245 ms 149376 KB Output is correct
18 Correct 270 ms 149540 KB Output is correct
19 Correct 232 ms 148348 KB Output is correct
20 Correct 224 ms 148980 KB Output is correct
21 Correct 33 ms 51280 KB Output is correct
22 Correct 29 ms 51288 KB Output is correct
23 Correct 30 ms 51792 KB Output is correct
24 Correct 35 ms 52048 KB Output is correct
25 Correct 36 ms 51536 KB Output is correct
26 Correct 31 ms 51540 KB Output is correct
27 Correct 34 ms 51792 KB Output is correct
28 Partially correct 31 ms 51548 KB Partially correct
29 Correct 34 ms 51540 KB Output is correct
30 Partially correct 33 ms 51588 KB Partially correct
31 Correct 1716 ms 1699032 KB Output is correct
32 Correct 237 ms 157868 KB Output is correct
33 Correct 1673 ms 1706556 KB Output is correct
34 Correct 1586 ms 1706756 KB Output is correct
35 Partially correct 1582 ms 1706308 KB Partially correct
36 Partially correct 1656 ms 1705592 KB Partially correct
37 Correct 1618 ms 1708060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 51168 KB Output is correct
2 Correct 32 ms 51112 KB Output is correct
3 Correct 35 ms 51320 KB Output is correct
4 Correct 32 ms 51120 KB Output is correct
5 Correct 35 ms 51088 KB Output is correct
6 Correct 30 ms 51796 KB Output is correct
7 Correct 31 ms 52060 KB Output is correct
8 Correct 29 ms 51288 KB Output is correct
9 Correct 28 ms 51540 KB Output is correct
10 Correct 29 ms 51856 KB Output is correct
11 Partially correct 34 ms 51540 KB Partially correct
12 Correct 30 ms 51548 KB Output is correct
13 Partially correct 29 ms 51468 KB Partially correct
14 Correct 30 ms 51320 KB Output is correct
15 Correct 29 ms 51320 KB Output is correct
16 Correct 32 ms 51236 KB Output is correct
17 Correct 29 ms 51804 KB Output is correct
18 Correct 47 ms 52020 KB Output is correct
19 Correct 36 ms 51316 KB Output is correct
20 Correct 36 ms 51428 KB Output is correct
21 Correct 40 ms 51780 KB Output is correct
22 Partially correct 33 ms 51532 KB Partially correct
23 Correct 30 ms 51548 KB Output is correct
24 Partially correct 29 ms 51596 KB Partially correct
25 Correct 439 ms 417296 KB Output is correct
26 Correct 34 ms 53664 KB Output is correct
27 Correct 177 ms 187908 KB Output is correct
28 Correct 56 ms 71976 KB Output is correct
29 Correct 320 ms 327824 KB Output is correct
30 Correct 33 ms 53556 KB Output is correct
31 Correct 321 ms 321036 KB Output is correct
32 Correct 32 ms 51288 KB Output is correct
33 Correct 234 ms 148216 KB Output is correct
34 Correct 322 ms 149632 KB Output is correct
35 Correct 248 ms 150640 KB Output is correct
36 Correct 230 ms 150176 KB Output is correct
37 Correct 214 ms 150376 KB Output is correct
38 Correct 262 ms 149572 KB Output is correct
39 Correct 249 ms 149376 KB Output is correct
40 Correct 260 ms 148412 KB Output is correct
41 Correct 218 ms 149156 KB Output is correct
42 Correct 31 ms 51256 KB Output is correct
43 Correct 229 ms 148216 KB Output is correct
44 Correct 264 ms 149712 KB Output is correct
45 Correct 222 ms 150644 KB Output is correct
46 Correct 225 ms 150016 KB Output is correct
47 Correct 220 ms 150344 KB Output is correct
48 Correct 245 ms 149376 KB Output is correct
49 Correct 270 ms 149540 KB Output is correct
50 Correct 232 ms 148348 KB Output is correct
51 Correct 224 ms 148980 KB Output is correct
52 Correct 33 ms 51280 KB Output is correct
53 Correct 29 ms 51288 KB Output is correct
54 Correct 30 ms 51792 KB Output is correct
55 Correct 35 ms 52048 KB Output is correct
56 Correct 36 ms 51536 KB Output is correct
57 Correct 31 ms 51540 KB Output is correct
58 Correct 34 ms 51792 KB Output is correct
59 Partially correct 31 ms 51548 KB Partially correct
60 Correct 34 ms 51540 KB Output is correct
61 Partially correct 33 ms 51588 KB Partially correct
62 Correct 1716 ms 1699032 KB Output is correct
63 Correct 237 ms 157868 KB Output is correct
64 Correct 1673 ms 1706556 KB Output is correct
65 Correct 1586 ms 1706756 KB Output is correct
66 Partially correct 1582 ms 1706308 KB Partially correct
67 Partially correct 1656 ms 1705592 KB Partially correct
68 Correct 1618 ms 1708060 KB Output is correct
69 Correct 33 ms 51288 KB Output is correct
70 Correct 220 ms 148440 KB Output is correct
71 Correct 241 ms 149628 KB Output is correct
72 Correct 219 ms 150644 KB Output is correct
73 Correct 236 ms 150020 KB Output is correct
74 Correct 222 ms 150396 KB Output is correct
75 Correct 262 ms 149492 KB Output is correct
76 Correct 265 ms 149504 KB Output is correct
77 Correct 229 ms 148436 KB Output is correct
78 Correct 230 ms 148984 KB Output is correct
79 Correct 29 ms 51288 KB Output is correct
80 Correct 32 ms 51212 KB Output is correct
81 Correct 42 ms 51792 KB Output is correct
82 Correct 34 ms 52060 KB Output is correct
83 Correct 35 ms 51292 KB Output is correct
84 Correct 34 ms 51536 KB Output is correct
85 Correct 30 ms 51804 KB Output is correct
86 Partially correct 29 ms 51732 KB Partially correct
87 Correct 30 ms 51548 KB Output is correct
88 Partially correct 30 ms 51540 KB Partially correct
89 Correct 1503 ms 1699176 KB Output is correct
90 Correct 219 ms 157940 KB Output is correct
91 Correct 1492 ms 1706560 KB Output is correct
92 Correct 1555 ms 1706788 KB Output is correct
93 Partially correct 1533 ms 1706364 KB Partially correct
94 Partially correct 1596 ms 1705596 KB Partially correct
95 Correct 1609 ms 1708020 KB Output is correct
96 Correct 358 ms 417448 KB Output is correct
97 Correct 37 ms 53836 KB Output is correct
98 Correct 157 ms 188100 KB Output is correct
99 Correct 51 ms 71792 KB Output is correct
100 Correct 303 ms 327724 KB Output is correct
101 Correct 45 ms 53584 KB Output is correct
102 Correct 295 ms 320852 KB Output is correct
103 Runtime error 816 ms 2097152 KB Execution killed with signal 9
104 Halted 0 ms 0 KB -