Submission #956658

# Submission time Handle Problem Language Result Execution time Memory
956658 2024-04-02T09:47:41 Z huutuan Chorus (JOI23_chorus) C++14
100 / 100
1807 ms 86792 KB
#include<bits/stdc++.h>

using namespace std;

#define int long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define isz(x) ((int)x.size())
#define sumof(x) accumulate(all(x), 0ll)

struct Line{
   mutable int k, m, p, cnt;
   bool operator<(const Line &a){ return k<a.k; }
   bool operator<(int x){ return p<x; }
};

const int inf=1e18;

struct LineContainer: vector<Line>{
   int id;
   int div(int a, int b){
      return a/b-((a^b)<0 && a%b);
   }
   bool isect(iterator x, iterator y){
      if (y==end()) return x->p=inf, 0;
      if (x->k==y->k) x->p=(x->k>=y->k)?inf:-inf;
      else x->p=div(y->m-x->m, x->k-y->k);
      return x->p>=y->p;
   }
   void add(int k, int m, int cnt){
      auto z=insert(end(), {k, m, 0, cnt}), y=z++, x=y;
      while (isect(y, z)) z=erase(z);
      if (x!=begin() && isect(--x, y)) isect(x, erase(y));
      while ((y=x)!=begin() && (--x)->p>=y->p) isect(x, erase(y));
   }
   LineContainer(){
      id=0;
   }
   pair<int, int> query(int x){
      while ((begin()+id)->operator<(x)) ++id;
      return {(begin()+id)->k*x+(begin()+id)->m, (begin()+id)->cnt};
   }
};

const int N=1e6+10;
int n, k, a[N], pf[N];
pair<int, int> f[N];
string s;

pair<int, int> dp(int penalty){
   LineContainer cht;
   for (int i=1; i<=n; ++i) f[i]={inf, 0};
   f[0]={0, 0};
   cht.add(0, 0, 0);
   for (int i=1, j=0; i<=n; ++i){
      while (j<i && a[j+1]<i) ++j;
      auto tmp=cht.query(i);
      f[i]={max(0ll, -tmp.first+j*i-pf[j])+penalty, tmp.second+1};
      cht.add(i, -f[i].first-pf[i], f[i].second);
   }
   return f[n];
}

void solve(){
   cin >> n >> k >> s;
   for (int i=0, j=1; i<n*2; ++i){
      if (s[i]=='A') ++a[j];
      else a[j+1]=a[j], ++j;
   }
   partial_sum(a, a+n+1, pf);
   dp(0);
   int l=0, r=1e12;
   while (l<=r){
      int mid=(l+r)>>1;
      auto tmp=dp(mid);
      if (tmp.second<=k) r=mid-1;
      else l=mid+1;
   }
   auto ans=dp(l);
   cout << ans.first-l*k;
}

int32_t main(){
   ios_base::sync_with_stdio(false);
   cin.tie(nullptr);
   int ntests=1;
   // cin >> ntests;
   for (int i=1; i<=ntests; ++i) solve();
   return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2396 KB Output is correct
25 Correct 1 ms 2396 KB Output is correct
26 Correct 1 ms 2396 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 2648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2396 KB Output is correct
25 Correct 1 ms 2396 KB Output is correct
26 Correct 1 ms 2396 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 2648 KB Output is correct
29 Correct 7 ms 2908 KB Output is correct
30 Correct 8 ms 2868 KB Output is correct
31 Correct 8 ms 2872 KB Output is correct
32 Correct 6 ms 2908 KB Output is correct
33 Correct 7 ms 2908 KB Output is correct
34 Correct 7 ms 3068 KB Output is correct
35 Correct 7 ms 3068 KB Output is correct
36 Correct 8 ms 2768 KB Output is correct
37 Correct 8 ms 2876 KB Output is correct
38 Correct 7 ms 3068 KB Output is correct
39 Correct 8 ms 2908 KB Output is correct
40 Correct 7 ms 2652 KB Output is correct
41 Correct 7 ms 2652 KB Output is correct
42 Correct 7 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2396 KB Output is correct
25 Correct 1 ms 2396 KB Output is correct
26 Correct 1 ms 2396 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 2648 KB Output is correct
29 Correct 7 ms 2908 KB Output is correct
30 Correct 8 ms 2868 KB Output is correct
31 Correct 8 ms 2872 KB Output is correct
32 Correct 6 ms 2908 KB Output is correct
33 Correct 7 ms 2908 KB Output is correct
34 Correct 7 ms 3068 KB Output is correct
35 Correct 7 ms 3068 KB Output is correct
36 Correct 8 ms 2768 KB Output is correct
37 Correct 8 ms 2876 KB Output is correct
38 Correct 7 ms 3068 KB Output is correct
39 Correct 8 ms 2908 KB Output is correct
40 Correct 7 ms 2652 KB Output is correct
41 Correct 7 ms 2652 KB Output is correct
42 Correct 7 ms 2652 KB Output is correct
43 Correct 99 ms 7300 KB Output is correct
44 Correct 194 ms 9436 KB Output is correct
45 Correct 177 ms 9352 KB Output is correct
46 Correct 116 ms 11096 KB Output is correct
47 Correct 116 ms 10328 KB Output is correct
48 Correct 155 ms 13608 KB Output is correct
49 Correct 144 ms 13204 KB Output is correct
50 Correct 168 ms 10020 KB Output is correct
51 Correct 176 ms 9232 KB Output is correct
52 Correct 156 ms 13560 KB Output is correct
53 Correct 150 ms 13188 KB Output is correct
54 Correct 179 ms 9412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2396 KB Output is correct
25 Correct 1 ms 2396 KB Output is correct
26 Correct 1 ms 2396 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 2648 KB Output is correct
29 Correct 7 ms 2908 KB Output is correct
30 Correct 8 ms 2868 KB Output is correct
31 Correct 8 ms 2872 KB Output is correct
32 Correct 6 ms 2908 KB Output is correct
33 Correct 7 ms 2908 KB Output is correct
34 Correct 7 ms 3068 KB Output is correct
35 Correct 7 ms 3068 KB Output is correct
36 Correct 8 ms 2768 KB Output is correct
37 Correct 8 ms 2876 KB Output is correct
38 Correct 7 ms 3068 KB Output is correct
39 Correct 8 ms 2908 KB Output is correct
40 Correct 7 ms 2652 KB Output is correct
41 Correct 7 ms 2652 KB Output is correct
42 Correct 7 ms 2652 KB Output is correct
43 Correct 99 ms 7300 KB Output is correct
44 Correct 194 ms 9436 KB Output is correct
45 Correct 177 ms 9352 KB Output is correct
46 Correct 116 ms 11096 KB Output is correct
47 Correct 116 ms 10328 KB Output is correct
48 Correct 155 ms 13608 KB Output is correct
49 Correct 144 ms 13204 KB Output is correct
50 Correct 168 ms 10020 KB Output is correct
51 Correct 176 ms 9232 KB Output is correct
52 Correct 156 ms 13560 KB Output is correct
53 Correct 150 ms 13188 KB Output is correct
54 Correct 179 ms 9412 KB Output is correct
55 Correct 1100 ms 55592 KB Output is correct
56 Correct 1675 ms 86792 KB Output is correct
57 Correct 1807 ms 67756 KB Output is correct
58 Correct 1368 ms 69852 KB Output is correct
59 Correct 1534 ms 85784 KB Output is correct
60 Correct 1479 ms 86576 KB Output is correct
61 Correct 1452 ms 86044 KB Output is correct
62 Correct 1790 ms 65700 KB Output is correct
63 Correct 1796 ms 66096 KB Output is correct
64 Correct 1495 ms 86644 KB Output is correct
65 Correct 1499 ms 86656 KB Output is correct
66 Correct 1771 ms 70712 KB Output is correct
67 Correct 1478 ms 85084 KB Output is correct