QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#68803#5246. Nawiasowe podziały [B]Adam_GS0 1966ms17352kbC++204.3kb2022-12-21 03:03:112022-12-21 03:03:13

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-12-21 03:03:13]
  • 评测
  • 测评结果:0
  • 用时:1966ms
  • 内存:17352kb
  • [2022-12-21 03:03:11]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long long ld;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const ld EPS=1e-12;
const ll INF=1e18+7;
const int LIM=1e5+7;
vector<pair<ll,ll>>V[LIM];
vector<pair<ll,pair<ll,ll>>>P;
ld dp[LIM];
ll li[LIM], gdzie[LIM], opt[LIM], ile[LIM], nxt[LIM], potem[LIM], n, k;
vector<pair<ll,ll>>kraw;
string s;
pair<ll,ll>F[LIM];
ll tr[4*LIM], lazy[4*LIM], N=1;
void spl(int v, ll x) {
  tr[2*v]+=lazy[v]*x;
  tr[2*v+1]+=lazy[v]*x;
  lazy[2*v]+=lazy[v];
  lazy[2*v+1]+=lazy[v];
  lazy[v]=0;
}
void upd(int v, ll l, ll r, int a, int b, ll x) {
  if(r<a || l>b) return;
  if(a<=l && r<=b) {
    tr[v]+=(r-l+1)*x;
    lazy[v]+=x;
    return;
  }
  if(lazy[v]) spl(v, (r-l+1)/2);
  int mid=(l+r)/2;
  upd(2*v, l, mid, a, b, x);
  upd(2*v+1, mid+1, r, a, b, x);
  tr[v]=tr[2*v]+tr[2*v+1];
}
ll cnt(int v, ll l, ll r, int a, int b) {
  if(r<a || l>b) return 0;
  if(a<=l && r<=b) return tr[v];
  if(lazy[v]) spl(v, (r-l+1)/2);
  int mid=(l+r)/2;
  ll x=cnt(2*v, l, mid, a, b)+cnt(2*v+1, mid+1, r, a, b);
  tr[v]=tr[2*v]+tr[2*v+1];
  return x;
}
pair<ll,ll>fnd(ll x) {
  if(F[x].st==x) return F[x];
  pair<ll,ll>p=fnd(F[x].st);
  F[x]={p.st, p.nd+F[x].nd};
  return F[x];
}
void zwieksz(int x) {
  if(s[x]=='(') {
    if(!P.size() || P.back().nd.st==0) P.pb({x, {0, P.size()}});
    else P.pb({x, {0, P.back().nd.nd}});
    for(auto i : kraw) {
      F[i.st]={x, i.nd};
    }
    kraw.clear();
    return;
  }
  ll lacznie=0, p=(ll)P.size()-1;
  while(p>=0 && P[p].nd.st==1) --p;
  for(int i=P.size()-1; i>p; --i) {
    lacznie+=cnt(1, 0, N-1, i, i);
    kraw.pb({P[i].st, lacznie});
    potem[P[i].st]=lacznie;
    upd(1, 0, N-1, i, i, -cnt(1, 0, N-1, i, i));
    P.pop_back();
  }
  if(p>=0) {
    P[p].nd.st=1;
    upd(1, 0, N-1, p, p, lacznie);
    upd(1, 0, N-1, P[p].nd.nd, p, 1);
  }
}
ld policz_sufix(int x) {
  ll a=nxt[x];
  ll p=fnd(a).nd; a=fnd(a).st;
  if(P.size()) {
    int po=0, ko=P.size()-1;
    while(po<ko) {
      int sr=(po+ko)/2;
      if(P[sr].st<a) po=sr+1; else ko=sr;
    }
    if(P[po].st==a) p+=cnt(1, 0, N-1, po, N-1);
    else p+=potem[a];
  } else p+=potem[a];
  return (ld)p+dp[x];
}
int f(ld c) {
  kraw.clear(); P.clear();
  N=1;
  while(N<n) N*=2;
  rep(i, 2*N) tr[i]=lazy[i]=0;
  rep(i, n+1) {
    V[i].clear();
    potem[i]=li[i]=gdzie[i]=ile[i]=0;
    dp[i]=0;
    opt[i]=0;
    F[i]={i, 0};
  }
  int akt=0;
  V[akt].pb({0, 0}); ++ile[akt];
  rep(i, n) {
    zwieksz(i);
    if(s[i]=='(') {
      ++akt;
      V[akt].clear(); gdzie[akt]=0; li[akt]=0; ile[akt]=0;
      gdzie[akt]=gdzie[akt-1];
      if(policz_sufix(gdzie[akt])>=policz_sufix(V[akt-1][li[akt-1]].st)) gdzie[akt]=V[akt-1][li[akt-1]].st;
      V[akt].pb({i+1, ile[akt]++});
      dp[i+1]=policz_sufix(gdzie[akt])+c;
      opt[i+1]=opt[gdzie[akt]]+1;
      continue;
    }
    pair<ld,int>mi={INF, INF};
    for(auto j : V[akt]) mi=min(mi, {policz_sufix(j.st), j.st});
    if(akt) {
      --akt;
      while(li[akt]+1<V[akt].size() && policz_sufix(V[akt][li[akt]+1].st)<=policz_sufix(V[akt][li[akt]].st)) ++li[akt];
      while(li[akt]<V[akt].size() && policz_sufix(V[akt].back().st)>mi.st) V[akt].pop_back();
      while(li[akt]+1<V[akt].size() &&
    (policz_sufix(V[akt].back().st)-policz_sufix(V[akt][V[akt].size()-2].st))*(ile[akt]-V[akt].back().nd)>
     (mi.st-policz_sufix(V[akt].back().st))*(V[akt].back().nd-V[akt][V[akt].size()-2].nd)) V[akt].pop_back();
      V[akt].pb({mi.nd, ile[akt]++});
    } else {
      for(auto j : V[akt]) if(policz_sufix(j.st)<=policz_sufix(gdzie[akt])) gdzie[akt]=j.st;
      V[akt].clear();
      li[akt]=ile[akt]=0;
      V[akt].pb({0, 0});
      ++ile[akt];
    }
  }
  return opt[n];
}
int main() {
  ios_base::sync_with_stdio(0); cin.tie(0);
  cin >> n >> k >> s; ++n; s+="(";
  nxt[n-1]=n-1;
  for(int i=n-2; i>=0; --i) if(s[i]=='(') nxt[i]=i; else nxt[i]=nxt[i+1];
  /*while(N<n) N*=2;
  rep(i, n+1) F[i]={i, 0};
  rep(i, n) {
    zwieksz(i);
    rep(j, i+1) cout << policz_sufix(j) << " ";
    cout << '\n';
  }
  return 0;*/
  ld po=0, ko=10000000000;
  while(po<ko) {
    ld sr=(po+ko)/2;
    int p=f(sr);
   if(p>k) po=sr+1; else ko=sr;
  }
  cout << abs(round(dp[n]-(ld)k*po)) << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 5960kb

input:

1 1
(

output:

1

result:

wrong answer 1st lines differ - expected: '0', found: '1'

Subtask #2:

score: 0
Wrong Answer

Test #16:

score: 0
Wrong Answer
time: 2ms
memory: 6060kb

input:

300 25
((()()()(()())(((())())()))()((()(())))((()()))((()()))()()(((())())))((((())(()()()())(((()))(()())()(())())()()()()())(()((()()()))))(())()((()))((()))(()(()())))((()(())(((()))))((()()()()()())())(((()))()()()(())())(())()(((()))()((()()())))((())())(((()())())((()))(()()()()(())(()())((()...

output:

62

result:

wrong answer 1st lines differ - expected: '90', found: '62'

Subtask #3:

score: 0
Wrong Answer

Test #33:

score: 1
Accepted
time: 55ms
memory: 6440kb

input:

4000 1
(((((())())()((())(())(()()())()())(()(())(()))()(()))(()((())()()()()(()())(()()()))()(())()()(()(()()())))(()))(()(((()))()())((())(()()(()()))))((()((()())())((())())((()()))(())(())(()(()()))(((())())())((())()()(()))(()()()(()(())(()))()())((()))((())()(())()())))(((()(()()()()())(())())...

output:

5599

result:

ok single line: '5599'

Test #34:

score: -1
Wrong Answer
time: 50ms
memory: 6376kb

input:

4000 3
(((((((())))()()((((())(())()())()())(()(()())())))(()((())()((()()()((())()()))()))(())(()(()))()(()()()()((()(()))())()((()()()))(())(()()((())())))))(()(((((())))())((()()(((()()))())())())(())(()())(((()))())(()((()()()))(((()))())())())((())(())((())(())()(())()(((()))((())))))(((((())))...

output:

4565

result:

wrong answer 1st lines differ - expected: '4568', found: '4565'

Subtask #4:

score: 0
Wrong Answer

Test #54:

score: 1
Accepted
time: 63ms
memory: 6412kb

input:

4000 2
((())(()(())))()((())()()()(())()()(())()((()))()()((()()))((())())((())())(()())()(())(()(()))((()())())(()())((()()))(()()())((()))()())(()(()))((()()())(())()((())()))(())((())((())))(((()(())))()(())())(()(())(())())((())(())())((()(()))()()()()(()())())(((()))(())())((()())())((())()((()...

output:

18432

result:

ok single line: '18432'

Test #55:

score: -1
Wrong Answer
time: 51ms
memory: 6352kb

input:

4000 5
(((((((()))(())(()(())()()(())(()))()()(((()))())()(()))(((()(()()))()())()()(()(())()(())))((()()()())(((())))((()())()(()()())()()()())()()(()(()))))())((()()())((((((()))()())(()())())())()((()()()(()()((()))()()())(())()())()()()))(())())()(((((()()(()(())()()))))((()()((())())((()())))((...

output:

4331

result:

wrong answer 1st lines differ - expected: '4336', found: '4331'

Subtask #5:

score: 0
Wrong Answer

Test #75:

score: 0
Wrong Answer
time: 1966ms
memory: 17172kb

input:

100000 3
(()((())(()))(())((()())))(((()()))(()))(()()()((())))()()((())(())((())(()(()))())((())))()()((())(()()))(((())))((())()(()())(()())()(()(()()())))(()(()()))()(()((())()()()()())((()))(())(())())(((()))()(()()())()(()()))((()()))(((()()()()))(()))(((())))(((()))(()()())(()(()()))(())(()())...

output:

3.83201e+06

result:

wrong answer 1st lines differ - expected: '3832013', found: '3.83201e+06'

Subtask #6:

score: 0
Wrong Answer

Test #89:

score: 1
Accepted
time: 1739ms
memory: 17096kb

input:

100000 6
((((()()((())))()((()())()())((())()(((()()))())((())((()()()))()()()(())))(((()(())(()()))(())())(())))(((()))((((())))()))(()((())(())(())((()())))(()(()(()))(())(()()()(())())(()(())(()(()))(()())()((()))()()))(()()(())))((()())()((())((())()()(((()))(())))((())())()((()())(()))))()(((()...

output:

126039

result:

ok single line: '126039'

Test #90:

score: -1
Wrong Answer
time: 1790ms
memory: 17248kb

input:

100000 12
((()((())(((()))()())()()((()))())((((()()))()()))))(((())(()))(((((())))((())))(()()((()()()))((())()()((()(())((()))()()))())(()((()())())(()()(()))())()(()(())(())))()((()(()))((()))()((()()((())(())))))())(((()()))(()((()()))()(((()()()())()())))())(()((()())(())()(((()()))(()()()()))(...

output:

130668

result:

wrong answer 1st lines differ - expected: '130680', found: '130668'

Subtask #7:

score: 0
Wrong Answer

Test #103:

score: 1
Accepted
time: 1363ms
memory: 16068kb

input:

80000 81
((((((()())((()((()))))(()())()(()(((()(()())(()())())(())))()(())(()()))()))(((()(()))((()))(())((((())))(())()((()))())((()(()())))(())(()()))(((()()))(()((())))())()(((()()))(()())()()(((()((()))(())))((())(()((())))())(()())))()()()((()()(()()(())()()((()))(())()))((()(())()()))(()(()()...

output:

87799

result:

ok single line: '87799'

Test #104:

score: -1
Wrong Answer
time: 1582ms
memory: 16004kb

input:

80000 729
(((())(()))(()()())(()(()))(()())(()()()())(((()()())))()(()()()(()())))(((())((()()())))())((()(())()()())((()()()))()(()()()())()()(((())(()))(()())()()()(())))((()))((()())()(()()())(()()()()())(())(((()))(()(()())())())(()()())(()(())()()()()(())())()())(((())()))(((()())(())()(())((()...

output:

75580

result:

wrong answer 1st lines differ - expected: '76309', found: '75580'

Subtask #8:

score: 0
Wrong Answer

Test #129:

score: 1
Accepted
time: 1899ms
memory: 16348kb

input:

79856 243
(())()(())(())(()()())()(())()(()())()(())()(())()((()))(())(()(())())()(()())()()(())(())((()()))(())(()()())(()())((()())()())(())()()(())()(())((()()))()()(()()())(())()()(()())(())(())((()))(())(())()()()()()(()(()))(()(())()(()))()()()()()()()()((()()))()()()()()(())()(())(()())()(()(...

output:

794572

result:

ok single line: '794572'

Test #130:

score: 0
Accepted
time: 1412ms
memory: 15816kb

input:

79156 2187
((((((())))((()))()(()(())()()())(()))((()(())()))((((())()))(()(((()()))())(()())(())(())(()()(())(()))(((())))(()()))()(((()()()))(()(()())))(((())(()(()))(())()())()()(())))((())()(())())(((()()()()())(())(())()((((()))(())())))((()(()()))((()()))())(())(()(()(()))()()()())((()()())())...

output:

43675

result:

ok single line: '43675'

Test #131:

score: 0
Accepted
time: 1407ms
memory: 15856kb

input:

80000 2
((())((()))()(()()(((()))))(()(((()())(()))(())(()()))()(())(()())()((()))((()()()())()(()())()())(()((()()))(()(()))(()))(()()(())()()()))(((()(())())((()))())((()()()(()))((()())(())())(()())))(())(()()((()(()()))()(())()())()((()))((())()))((()))(()()(())(())())()((()(()()()()())((()()))(...

output:

120005

result:

ok single line: '120005'

Test #132:

score: 0
Accepted
time: 1474ms
memory: 15880kb

input:

80000 34
((((()))((()))((()(()())(())))()())((()())()((()()))()()()((())())(()(()))(()()()()())(())(()(()()()(()())())()())))(((()(()()))(()()(()))()((()))((())()(())()(())()()()((()())())())(()((()())()()())()(())()()(()()))()(()(()(()))(()))())((())()(()(()())()())(()(()())()()))(()(()))((((())))(...

output:

112242

result:

ok single line: '112242'

Test #133:

score: -1
Wrong Answer
time: 1459ms
memory: 15872kb

input:

80000 610
((()())((()(())(()))()((())))((((())())()())((())))()(()()(((())))(()()(()()))(()(())((()))(())()()(()))()(((()))(()))(()())()((()((()))(()()))()))()((()(()))(()())(((()())()())((()()))(()())()(()()))()(()(()()()()())()()(())))()((()(()())()))()((((())()()())())()(()()((()()())()())))(((((...

output:

71511

result:

wrong answer 1st lines differ - expected: '72136', found: '71511'

Subtask #9:

score: 0
Wrong Answer

Test #155:

score: 0
Wrong Answer
time: 1754ms
memory: 17348kb

input:

99599 64
((((((()(((())()))))((((((()))()())())())(()(()(()())))(()(()()(())())((()(()()))((()))()(())()())()(()(())()((()()())()())()(((())()))(()))((())(()(())()))(()))())()(((())(((())()((())))())(()()(()))(()())()((())((()())()(())())(())()(())((())(()()(()))((()))(()()())))()(((()))(())))(())((...

output:

110383

result:

wrong answer 1st lines differ - expected: '110447', found: '110383'

Subtask #10:

score: 0
Wrong Answer

Test #181:

score: 0
Wrong Answer
time: 1785ms
memory: 17352kb

input:

100000 256
((((())()(())())(((()))()((())()(()(())))))(((()(()))(((()))()()()()))()((((()))(())((()))))((()))(()((()()()()())()())(())(())()()(()()))(((()()))))())((((()))()((())()(()(())((()))))()((()()()()((()))))()(()(()()(()(())(()))))(())(((())))((()()((()))()())()())()((()())))()(((((()()))))(...

output:

107146

result:

wrong answer 1st lines differ - expected: '107402', found: '107146'