답안 #1073711

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1073711 2024-08-24T18:54:25 Z cadmiumsky Tricks of the Trade (CEOI23_trade) C++17
50 / 100
6679 ms 23440 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;

const ll inf = 1e18;

struct KthHeap {
   multiset<int> outside, inside;
   void repair() {
      while(sz(inside) > K) {
         int x = *inside.begin();
         inside.erase(inside.find(x));
         outside.insert(x);
         sum -= x;
      }
      while(sz(inside) < K && sz(outside)) {
         int x = *outside.rbegin();
         outside.erase(outside.find(x));
         inside.insert(x);
         sum += x;
      }
      while(sz(inside) && sz(outside) && *inside.begin() < *outside.rbegin()) {
         int x = *outside.rbegin(), y = *inside.begin();
         outside.erase(outside.find(x));
         inside.erase(inside.find(y));
         outside.emplace(y);
         inside.emplace(x);
         sum += x - y;
      }
      return;
   }
   void erase(int x) {
      if(outside.find(x) != outside.end())
         outside.erase(outside.find(x));
      else if(inside.find(x) != inside.end())
         inside.erase(inside.find(x)), sum -= x;
      else assert(false);
      repair();
   }
   void insert(int x) {
      outside.emplace(x);
      repair();
   }
   ll query() {
      repair();
      if(sz(inside) < K) return -inf;
      return sum;
   }
   
   private:
      ll sum = 0;
};

int bestcut[nmax];
ll dp[nmax];

ll spart[nmax];
ll v[nmax];

ll S(int l, int r) { return spart[r] - spart[l - 1]; }

namespace Divide {
   KthHeap hint;
   
   void divide(int l, int r) {
      if(l + 1 == r) return;
      int optl = bestcut[l], optr = bestcut[r];
      
      int mid = l + r >> 1;
      if(r <= optl) {
         for(int i = mid; i < r; i++) hint.insert(v[i]);
         ll best = hint.query() - S(mid, optl);
         int atr = optl;
         for(int i = optl + 1; i <= optr; i++) {
            hint.insert(v[i]);
            if(hint.query() - S(mid, i) > best) tie(best, atr) = make_pair(hint.query() - S(mid, i), i);
         }
         bestcut[mid] = atr, dp[mid] = best;
         if(atr - mid + 1 == K) {
            int SA = S(mid, atr), SB = 0;
            for(int i = mid; i <= atr; i++) SB += v[i];
         }
         
         for(int i = optl + 1; i <= optr; i++) hint.erase(v[i]);
         divide(l, mid);
         for(int i = mid; i < r; i++) hint.erase(v[i]);
         for(int i = optl + 1; i <= atr; i++) hint.insert(v[i]);
         divide(mid, r);
         for(int i = optl + 1; i <= atr; i++) hint.erase(v[i]);
      }
      else {
         int border = max(mid - 1, optl);
         for(int i = mid; i <= border; i++) hint.insert(v[i]);
         ll best = hint.query() - S(mid, border), atr = border;
         for(int i = border + 1; i <= optr; i++) {
            hint.insert(v[i]);
            if(hint.query() - S(mid, i) > best) tie(best, atr) = make_pair(hint.query() - S(mid, i), i);
         }
         
         bestcut[mid] = atr, dp[mid] = best;
         
         for(int i = optr; i > border; i--) hint.erase(v[i]);
         divide(l, mid);
         for(int i = mid; i <= border; i++) hint.erase(v[i]);
         for(int i = r; i <= atr; i++) hint.insert(v[i]);
         divide(mid, r);
         for(int i = r; i <= atr; i++) hint.erase(v[i]);
      }
   }
   
}


signed main() {
   cin.tie(0) -> sync_with_stdio(0);
   int n;
   cin >> n >> K;
   for(int i = 1; i <= n; i++) {
      cin >> spart[i];
      spart[i] += spart[i - 1];
   }
   for(int i = 1; i <= n; i++)
      cin >> v[i];
      
   bestcut[0] = 0;
   bestcut[n - K + 2] = n;
   Divide::divide(0, n - K + 2);
   ll mx = -inf;
   
   vector<int> possible(n + 1);
   
   
   for(int i = 1; i <= n - K + 1; i++) 
      mx = max(mx, dp[i]);
   cout << mx << '\n';
   
   auto cmp = [&](int a, int b) { return (v[a] < v[b] || (v[a] == v[b] && a < b)); };
   set<int, decltype(cmp)> inside(cmp), outside(cmp);
   
   int criteriaIL = n + 1, criteriaIR = -1, criteriaV = inf;
   
   for(int i = 1; i <= n; i++) {
      for(int j = bestcut[i - 1] + 1; j <= bestcut[i]; j++) {
         inside.insert(j);
         while(sz(inside) > K && [&]() {
           auto it = inside.begin(), it2 = next(it);
            return v[*it] < v[*it2];
         }()) {
            auto t = *inside.begin();
            if(t <= criteriaIR && v[t] >= criteriaV) possible[t] = 1;
            inside.erase(inside.begin());
            outside.insert(t);
         }
      }
      if(dp[i] == mx) {
         criteriaIR = bestcut[i];
         criteriaV = v[*inside.begin()];
         //cerr << criteriaIR << ' ' << criteriaV << '\n';
      }
      if(inside.count(i)) {
         if(criteriaIR >= i && v[i] >= criteriaV) possible[i] = 1;
         inside.erase(i); 
      }
      else if(outside.count(i)) outside.erase(i);
      while(sz(inside) < K && sz(outside)) {
         int a = *outside.rbegin();
         outside.erase(a);
         inside.insert(a);
      }
   }
   
   while(!inside.empty()) {
      int t = *inside.begin();
      //cerr << t << ' ' << (t <= criteriaIR && v[t] >= criteriaV) << '\n';
      if(t <= criteriaIR && v[t] >= criteriaV) possible[t] = 1;
      inside.erase(inside.begin());
   }
   
   for(int i = 1; i <= n; i++) cout << possible[i];
   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 'void Divide::divide(ll, ll)':
trade.cpp:82:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   82 |       int mid = l + r >> 1;
      |                 ~~^~~
trade.cpp:93:17: warning: unused variable 'SA' [-Wunused-variable]
   93 |             int SA = S(mid, atr), SB = 0;
      |                 ^~
trade.cpp: In function 'int main()':
trade.cpp:153:8: warning: unused variable 'criteriaIL' [-Wunused-variable]
  153 |    int criteriaIL = n + 1, criteriaIR = -1, criteriaV = inf;
      |        ^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Partially correct 1 ms 6492 KB Partially correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Partially correct 1 ms 6492 KB Partially correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Partially correct 1 ms 6492 KB Partially correct
7 Partially correct 2 ms 6492 KB Partially correct
8 Partially correct 1 ms 6492 KB Partially correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Partially correct 1 ms 6492 KB Partially correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Partially correct 1 ms 6492 KB Partially correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Partially correct 1 ms 6492 KB Partially correct
7 Partially correct 2 ms 6492 KB Partially correct
8 Partially correct 1 ms 6492 KB Partially correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Partially correct 1 ms 6492 KB Partially correct
12 Correct 1 ms 6492 KB Output is correct
13 Partially correct 1 ms 6608 KB Partially correct
14 Correct 1 ms 6600 KB Output is correct
15 Correct 2 ms 6488 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Partially correct 1 ms 6492 KB Partially correct
18 Partially correct 1 ms 6492 KB Partially correct
19 Partially correct 1 ms 6492 KB Partially correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Partially correct 1 ms 6492 KB Partially correct
23 Correct 7 ms 7020 KB Output is correct
24 Partially correct 22 ms 6748 KB Partially correct
25 Partially correct 47 ms 7004 KB Partially correct
26 Partially correct 38 ms 6744 KB Partially correct
27 Partially correct 24 ms 6884 KB Partially correct
28 Correct 8 ms 6748 KB Output is correct
29 Correct 51 ms 7000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Partially correct 1 ms 6492 KB Partially correct
2 Correct 341 ms 14996 KB Output is correct
3 Correct 523 ms 15956 KB Output is correct
4 Correct 1668 ms 18908 KB Output is correct
5 Partially correct 1621 ms 15952 KB Partially correct
6 Partially correct 1045 ms 15724 KB Partially correct
7 Correct 3915 ms 23268 KB Output is correct
8 Correct 591 ms 15992 KB Output is correct
9 Correct 554 ms 15396 KB Output is correct
10 Partially correct 501 ms 15524 KB Partially correct
# 결과 실행 시간 메모리 Grader output
1 Partially correct 1 ms 6492 KB Partially correct
2 Correct 341 ms 14996 KB Output is correct
3 Correct 523 ms 15956 KB Output is correct
4 Correct 1668 ms 18908 KB Output is correct
5 Partially correct 1621 ms 15952 KB Partially correct
6 Partially correct 1045 ms 15724 KB Partially correct
7 Correct 3915 ms 23268 KB Output is correct
8 Correct 591 ms 15992 KB Output is correct
9 Correct 554 ms 15396 KB Output is correct
10 Partially correct 501 ms 15524 KB Partially correct
11 Partially correct 1 ms 6492 KB Partially correct
12 Correct 350 ms 14948 KB Output is correct
13 Correct 561 ms 16040 KB Output is correct
14 Correct 1708 ms 18912 KB Output is correct
15 Partially correct 1659 ms 16108 KB Partially correct
16 Partially correct 1018 ms 15712 KB Partially correct
17 Correct 4344 ms 23440 KB Output is correct
18 Correct 637 ms 16124 KB Output is correct
19 Correct 521 ms 15188 KB Output is correct
20 Partially correct 492 ms 15440 KB Partially correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Partially correct 1 ms 6492 KB Partially correct
26 Partially correct 1 ms 6492 KB Partially correct
27 Partially correct 1 ms 6492 KB Partially correct
28 Correct 1 ms 6744 KB Output is correct
29 Correct 1 ms 6492 KB Output is correct
30 Partially correct 1 ms 6492 KB Partially correct
31 Correct 1378 ms 15956 KB Output is correct
32 Partially correct 1538 ms 15952 KB Partially correct
33 Partially correct 2881 ms 18656 KB Partially correct
34 Partially correct 2419 ms 17748 KB Partially correct
35 Partially correct 1909 ms 15440 KB Partially correct
36 Partially correct 5285 ms 23116 KB Partially correct
37 Partially correct 1010 ms 14936 KB Partially correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Partially correct 1 ms 6492 KB Partially correct
3 Correct 1 ms 6488 KB Output is correct
4 Partially correct 1 ms 6492 KB Partially correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Partially correct 1 ms 6492 KB Partially correct
9 Partially correct 2 ms 6492 KB Partially correct
10 Partially correct 1 ms 6492 KB Partially correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Partially correct 1 ms 6492 KB Partially correct
14 Correct 1 ms 6492 KB Output is correct
15 Partially correct 1 ms 6608 KB Partially correct
16 Correct 1 ms 6600 KB Output is correct
17 Correct 2 ms 6488 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Partially correct 1 ms 6492 KB Partially correct
20 Partially correct 1 ms 6492 KB Partially correct
21 Partially correct 1 ms 6492 KB Partially correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Partially correct 1 ms 6492 KB Partially correct
25 Correct 7 ms 7020 KB Output is correct
26 Partially correct 22 ms 6748 KB Partially correct
27 Partially correct 47 ms 7004 KB Partially correct
28 Partially correct 38 ms 6744 KB Partially correct
29 Partially correct 24 ms 6884 KB Partially correct
30 Correct 8 ms 6748 KB Output is correct
31 Correct 51 ms 7000 KB Output is correct
32 Partially correct 1 ms 6492 KB Partially correct
33 Correct 341 ms 14996 KB Output is correct
34 Correct 523 ms 15956 KB Output is correct
35 Correct 1668 ms 18908 KB Output is correct
36 Partially correct 1621 ms 15952 KB Partially correct
37 Partially correct 1045 ms 15724 KB Partially correct
38 Correct 3915 ms 23268 KB Output is correct
39 Correct 591 ms 15992 KB Output is correct
40 Correct 554 ms 15396 KB Output is correct
41 Partially correct 501 ms 15524 KB Partially correct
42 Partially correct 1 ms 6492 KB Partially correct
43 Correct 350 ms 14948 KB Output is correct
44 Correct 561 ms 16040 KB Output is correct
45 Correct 1708 ms 18912 KB Output is correct
46 Partially correct 1659 ms 16108 KB Partially correct
47 Partially correct 1018 ms 15712 KB Partially correct
48 Correct 4344 ms 23440 KB Output is correct
49 Correct 637 ms 16124 KB Output is correct
50 Correct 521 ms 15188 KB Output is correct
51 Partially correct 492 ms 15440 KB Partially correct
52 Correct 1 ms 6492 KB Output is correct
53 Correct 1 ms 6492 KB Output is correct
54 Correct 1 ms 6492 KB Output is correct
55 Correct 1 ms 6492 KB Output is correct
56 Partially correct 1 ms 6492 KB Partially correct
57 Partially correct 1 ms 6492 KB Partially correct
58 Partially correct 1 ms 6492 KB Partially correct
59 Correct 1 ms 6744 KB Output is correct
60 Correct 1 ms 6492 KB Output is correct
61 Partially correct 1 ms 6492 KB Partially correct
62 Correct 1378 ms 15956 KB Output is correct
63 Partially correct 1538 ms 15952 KB Partially correct
64 Partially correct 2881 ms 18656 KB Partially correct
65 Partially correct 2419 ms 17748 KB Partially correct
66 Partially correct 1909 ms 15440 KB Partially correct
67 Partially correct 5285 ms 23116 KB Partially correct
68 Partially correct 1010 ms 14936 KB Partially correct
69 Partially correct 1 ms 6492 KB Partially correct
70 Correct 369 ms 14940 KB Output is correct
71 Correct 561 ms 15976 KB Output is correct
72 Correct 1727 ms 18932 KB Output is correct
73 Partially correct 1652 ms 16104 KB Partially correct
74 Partially correct 1069 ms 15720 KB Partially correct
75 Correct 4149 ms 23380 KB Output is correct
76 Correct 623 ms 15956 KB Output is correct
77 Correct 575 ms 15260 KB Output is correct
78 Partially correct 497 ms 15640 KB Partially correct
79 Correct 1 ms 6492 KB Output is correct
80 Correct 1 ms 6608 KB Output is correct
81 Correct 2 ms 6492 KB Output is correct
82 Correct 1 ms 6488 KB Output is correct
83 Partially correct 1 ms 6492 KB Partially correct
84 Partially correct 1 ms 6492 KB Partially correct
85 Partially correct 1 ms 6492 KB Partially correct
86 Correct 2 ms 6488 KB Output is correct
87 Correct 1 ms 6608 KB Output is correct
88 Partially correct 1 ms 6492 KB Partially correct
89 Correct 1401 ms 16108 KB Output is correct
90 Partially correct 1579 ms 15980 KB Partially correct
91 Partially correct 2874 ms 18840 KB Partially correct
92 Partially correct 2403 ms 17876 KB Partially correct
93 Partially correct 1879 ms 15556 KB Partially correct
94 Partially correct 5227 ms 23108 KB Partially correct
95 Partially correct 1035 ms 14932 KB Partially correct
96 Correct 7 ms 7004 KB Output is correct
97 Partially correct 26 ms 6748 KB Partially correct
98 Partially correct 44 ms 6744 KB Partially correct
99 Partially correct 38 ms 6744 KB Partially correct
100 Partially correct 25 ms 6748 KB Partially correct
101 Correct 8 ms 6748 KB Output is correct
102 Correct 49 ms 6984 KB Output is correct
103 Correct 5749 ms 18692 KB Output is correct
104 Partially correct 2339 ms 17492 KB Partially correct
105 Partially correct 2640 ms 15680 KB Partially correct
106 Partially correct 4189 ms 17836 KB Partially correct
107 Partially correct 5071 ms 22096 KB Partially correct
108 Partially correct 6679 ms 19436 KB Partially correct
109 Partially correct 2501 ms 18768 KB Partially correct
110 Correct 4883 ms 22352 KB Output is correct
111 Correct 1613 ms 18260 KB Output is correct
112 Partially correct 2302 ms 19028 KB Partially correct