QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#68800 | #5246. Nawiasowe podziały [B] | Adam_GS | 0 | 1745ms | 17428kb | C++20 | 4.4kb | 2022-12-21 02:57:07 | 2022-12-21 02:57:10 |
Judging History
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=ko=sr;
else if(p>k) po=sr+EPS; else ko=sr-EPS;
}
cout << abs(round(dp[n]-(ld)k*po)) << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Time Limit Exceeded
Test #1:
score: 1
Accepted
time: 0ms
memory: 5964kb
input:
1 1 (
output:
0
result:
ok single line: '0'
Test #2:
score: 0
Accepted
time: 2ms
memory: 5876kb
input:
1 1 )
output:
0
result:
ok single line: '0'
Test #3:
score: 0
Accepted
time: 0ms
memory: 5928kb
input:
2 1 ()
output:
1
result:
ok single line: '1'
Test #4:
score: 0
Accepted
time: 3ms
memory: 6000kb
input:
2 1 )(
output:
0
result:
ok single line: '0'
Test #5:
score: 0
Accepted
time: 3ms
memory: 6012kb
input:
2 2 ()
output:
0
result:
ok single line: '0'
Test #6:
score: 0
Accepted
time: 1ms
memory: 6120kb
input:
2 2 )(
output:
0
result:
ok single line: '0'
Test #7:
score: -1
Time Limit Exceeded
input:
10 4 ()))(()(()
output:
result:
Subtask #2:
score: 0
Time Limit Exceeded
Test #16:
score: 0
Time Limit Exceeded
input:
300 25 ((()()()(()())(((())())()))()((()(())))((()()))((()()))()()(((())())))((((())(()()()())(((()))(()())()(())())()()()()())(()((()()()))))(())()((()))((()))(()(()())))((()(())(((()))))((()()()()()())())(((()))()()()(())())(())()(((()))()((()()())))((())())(((()())())((()))(()()()()(())(()())((()...
output:
result:
Subtask #3:
score: 0
Time Limit Exceeded
Test #33:
score: 1
Accepted
time: 5ms
memory: 6364kb
input:
4000 1 (((((())())()((())(())(()()())()())(()(())(()))()(()))(()((())()()()()(()())(()()()))()(())()()(()(()()())))(()))(()(((()))()())((())(()()(()()))))((()((()())())((())())((()()))(())(())(()(()()))(((())())())((())()()(()))(()()()(()(())(()))()())((()))((())()(())()())))(((()(()()()()())(())())...
output:
5599
result:
ok single line: '5599'
Test #34:
score: 0
Accepted
time: 45ms
memory: 6480kb
input:
4000 3 (((((((())))()()((((())(())()())()())(()(()())())))(()((())()((()()()((())()()))()))(())(()(()))()(()()()()((()(()))())()((()()()))(())(()()((())())))))(()(((((())))())((()()(((()()))())())())(())(()())(((()))())(()((()()()))(((()))())())())((())(())((())(())()(())()(((()))((())))))(((((())))...
output:
4568
result:
ok single line: '4568'
Test #35:
score: 0
Accepted
time: 52ms
memory: 6488kb
input:
4000 8 (((()))((()()(()())(()())((()))(((())))))(((())()(())()()()()((())())())((())()((())()))()(((())())()())(((()()()))(()()()(())(()))))(()()((()))((()())())((())(())()(())))(((((()()()())))(()()(()()())()()))()((())(()(()))((())()())((()()))()())(((())())()((()())(()()(())())()()((())))(())()((...
output:
4780
result:
ok single line: '4780'
Test #36:
score: 0
Accepted
time: 45ms
memory: 6356kb
input:
4000 21 (((((()())(())()))(()(())((((())())))()((()))()(()())()((())()())()()((((()))))))(((((()())()()()))((()))())((()))(()()(()))(()()((())))(())((()()())())(()()(())))(((())(()((())))()()(())(()((()()())(()))(()))))((()()((()()()())()())(()((()()()())())))(()()(((())()())()()((())(()))()(()))()(...
output:
3864
result:
ok single line: '3864'
Test #37:
score: -1
Time Limit Exceeded
input:
4000 55 ((((()))((((()())))())(())((()()())((()))(()()(()((())()((())))()())(()))((()())(()())()()))))((((())))(()()(((())())())()((()))(()(()(()))(())())())((((()())(((()))())())((())())(()((()(())()(()())))()(()()(())))))(((((())()))((()())(())()()(())()()(()())())(()()(())((()()()))))))((()(())((...
output:
result:
Subtask #4:
score: 0
Time Limit Exceeded
Test #54:
score: 1
Accepted
time: 33ms
memory: 6360kb
input:
4000 2 ((())(()(())))()((())()()()(())()()(())()((()))()()((()()))((())())((())())(()())()(())(()(()))((()())())(()())((()()))(()()())((()))()())(()(()))((()()())(())()((())()))(())((())((())))(((()(())))()(())())(()(())(())())((())(())())((()(()))()()()()(()())())(((()))(())())((()())())((())()((()...
output:
18432
result:
ok single line: '18432'
Test #55:
score: 0
Accepted
time: 45ms
memory: 6552kb
input:
4000 5 (((((((()))(())(()(())()()(())(()))()()(((()))())()(()))(((()(()()))()())()()(()(())()(())))((()()()())(((())))((()())()(()()())()()()())()()(()(()))))())((()()())((((((()))()())(()())())())()((()()()(()()((()))()()())(())()())()()()))(())())()(((((()()(()(())()()))))((()()((())())((()())))((...
output:
4336
result:
ok single line: '4336'
Test #56:
score: -1
Time Limit Exceeded
input:
4000 13 (((((()()))(()()(()((())(()()()))(()))(((()()(()()))()())((())()())()())((()()(())())((()()()()))(())()(())())()((()(())())()()()()())((()(()()()(()())))(()()))(((())(()()))()(())))))()((())((((()()())(()()))(())((()((())))())()(((())()()(()()())(()))()(((()))(((())()()(())))))(((()())))(()(...
output:
result:
Subtask #5:
score: 0
Wrong Answer
Test #75:
score: 0
Wrong Answer
time: 745ms
memory: 17164kb
input:
100000 3 (()((())(()))(())((()())))(((()()))(()))(()()()((())))()()((())(())((())(()(()))())((())))()()((())(()()))(((())))((())()(()())(()())()(()(()()())))(()(()()))()(()((())()()()()())((()))(())(())())(((()))()(()()())()(()()))((()()))(((()()()()))(()))(((())))(((()))(()()())(()(()()))(())(()())...
output:
3.83201e+06
result:
wrong answer 1st lines differ - expected: '3832013', found: '3.83201e+06'
Subtask #6:
score: 0
Time Limit Exceeded
Test #89:
score: 1
Accepted
time: 1403ms
memory: 17208kb
input:
100000 6 ((((()()((())))()((()())()())((())()(((()()))())((())((()()()))()()()(())))(((()(())(()()))(())())(())))(((()))((((())))()))(()((())(())(())((()())))(()(()(()))(())(()()()(())())(()(())(()(()))(()())()((()))()()))(()()(())))((()())()((())((())()()(((()))(())))((())())()((()())(()))))()(((()...
output:
126039
result:
ok single line: '126039'
Test #90:
score: 0
Accepted
time: 1479ms
memory: 17316kb
input:
100000 12 ((()((())(((()))()())()()((()))())((((()()))()()))))(((())(()))(((((())))((())))(()()((()()()))((())()()((()(())((()))()()))())(()((()())())(()()(()))())()(()(())(())))()((()(()))((()))()((()()((())(())))))())(((()()))(()((()()))()(((()()()())()())))())(()((()())(())()(((()()))(()()()()))(...
output:
130680
result:
ok single line: '130680'
Test #91:
score: 0
Accepted
time: 1342ms
memory: 17104kb
input:
100000 18 ((())(()()()()())()()(())((())()()()())()()()()((()()())((()))()()(()())()()()())(()())()(()(())))(()()((()()))(())()()(()())()(()())((()())()()(())()())((())(()))(()()()))((())(()())()(((())))(()(()))(()(()))()(())()(((())())()()())((())))((()())((()())))((()))(()(()()()()((())))()(()))((...
output:
525576
result:
ok single line: '525576'
Test #92:
score: 0
Accepted
time: 1687ms
memory: 17240kb
input:
100000 24 ((((((())()((())))()()(())((()())())()()(((()))()()(((())))())(()())(()(()()(())((()())(()())))(()())()(()(())()())(()((()))))()(((()(()()))())())((()))()()(()(()))())((()()()()(()(()))(()))()(()))()(((()))(()()(()(()))())()((()()())()((())(())((())(())()()())()())((())))()((()())((()()()(...
output:
118587
result:
ok single line: '118587'
Test #93:
score: 0
Accepted
time: 1745ms
memory: 17232kb
input:
100000 30 ((((())()()(((()()())())))((()))(())(()()(())))((())())((())(())())(((()(()(()))())(()())()()(())))(((()())(()))()((())((())()())((())()()()())()()()(())()()(()()))(((()()))(()()())(()(()()(())()))(())((())(()))(())()(()))))(((())(()()())()()((()))(()(()()()()((())))))((((()()()))(()((()))...
output:
132047
result:
ok single line: '132047'
Test #94:
score: 0
Accepted
time: 1344ms
memory: 17388kb
input:
99999 2 ()))())))))()))()))(()()))())))())(()))(((())(()(((()))(((()((((((((((((()))(())()(()((())(()())(())())()())()())()(()(((())())()(()(())(()))))))))))()))()()))())()(()())))((()(()((()))))))(()()())))()((())))))((()(((()()(())())()()))(())())())())))(())))(())()))())())(((((()()((((((()()(())...
output:
97853
result:
ok single line: '97853'
Test #95:
score: 0
Accepted
time: 1400ms
memory: 17428kb
input:
100000 5 )()()())(()())()()(())()))(()((())))(((((()())()((())()((()))()))(((()))(((()())((())((()())())()())))((((()))))(()((())(((()))(()((((()(())(((()(((()()))))((()((()(()(()((((()()()()(((()())))(((()(()()))))))()(()))((())()(((()(()())))))(((((()(())())(((()())()(())(()))()()()))()()())(((())...
output:
96190
result:
ok single line: '96190'
Test #96:
score: 0
Accepted
time: 1663ms
memory: 17228kb
input:
100000 15 )())((()))()((()()((()())()(((((())())()()((((()()(()())()(()((())))))(()()(()(()((())))))())(()())()))))())())()())()()(((((()()()()))))(((((()())())(()((()())()))(()()((()()((()((()))(((())()))(())((())(())))))())()((()()))(((())((()((()())(())))()()(())))(())(()()())))()())()((()(()(()(...
output:
93249
result:
ok single line: '93249'
Test #97:
score: -1
Time Limit Exceeded
input:
100000 29 )()()))))()(()()()))(()())()(((())()))()())))())(((())()())))))))))(((())())((()(())()))(()))())())((((((((()(()((((())()())(()())))())((()())(()))(()())()())(((()()())()(()))((((((()(())((())))((()))(((())(())(()))(())()(())((()())(((()(()((()(())((()()(()()(((())()))()(())))((()()))(()))...
output:
result:
Subtask #7:
score: 0
Time Limit Exceeded
Test #103:
score: 1
Accepted
time: 1282ms
memory: 15732kb
input:
80000 81 ((((((()())((()((()))))(()())()(()(((()(()())(()())())(())))()(())(()()))()))(((()(()))((()))(())((((())))(())()((()))())((()(()())))(())(()()))(((()()))(()((())))())()(((()()))(()())()()(((()((()))(())))((())(()((())))())(()())))()()()((()()(()()(())()()((()))(())()))((()(())()()))(()(()()...
output:
87799
result:
ok single line: '87799'
Test #104:
score: -1
Time Limit Exceeded
input:
80000 729 (((())(()))(()()())(()(()))(()())(()()()())(((()()())))()(()()()(()())))(((())((()()())))())((()(())()()())((()()()))()(()()()())()()(((())(()))(()())()()()(())))((()))((()())()(()()())(()()()()())(())(((()))(()(()())())())(()()())(()(())()()()()(())())()())(((())()))(((()())(())()(())((()...
output:
result:
Subtask #8:
score: 0
Time Limit Exceeded
Test #129:
score: 1
Accepted
time: 1601ms
memory: 16296kb
input:
79856 243 (())()(())(())(()()())()(())()(()())()(())()(())()((()))(())(()(())())()(()())()()(())(())((()()))(())(()()())(()())((()())()())(())()()(())()(())((()()))()()(()()())(())()()(()())(())(())((()))(())(())()()()()()(()(()))(()(())()(()))()()()()()()()()((()()))()()()()()(())()(())(()())()(()(...
output:
794572
result:
ok single line: '794572'
Test #130:
score: -1
Time Limit Exceeded
input:
79156 2187 ((((((())))((()))()(()(())()()())(()))((()(())()))((((())()))(()(((()()))())(()())(())(())(()()(())(()))(((())))(()()))()(((()()()))(()(()())))(((())(()(()))(())()())()()(())))((())()(())())(((()()()()())(())(())()((((()))(())())))((()(()()))((()()))())(())(()(()(()))()()()())((()()())())...
output:
result:
Subtask #9:
score: 0
Time Limit Exceeded
Test #155:
score: 1
Accepted
time: 1651ms
memory: 17136kb
input:
99599 64 ((((((()(((())()))))((((((()))()())())())(()(()(()())))(()(()()(())())((()(()()))((()))()(())()())()(()(())()((()()())()())()(((())()))(()))((())(()(())()))(()))())()(((())(((())()((())))())(()()(()))(()())()((())((()())()(())())(())()(())((())(()()(()))((()))(()()())))()(((()))(())))(())((...
output:
110447
result:
ok single line: '110447'
Test #156:
score: -1
Time Limit Exceeded
input:
100000 1024 ((((((((()()))(((()()()(((())())))()(((())))((())()(()()())(()()))((()())(())()()))))(((((())())()))))(((((((()))))())(((())()()((())))((()()))())()(()()())((()()(((()))()())(()(()()))()((()(()))(())()(())()()(()())())()((()))(())())()()))())(((()()((()(())()))(()))((()())((()()()))()(()...
output:
result:
Subtask #10:
score: 0
Time Limit Exceeded
Test #181:
score: 0
Time Limit Exceeded
input:
100000 256 ((((())()(())())(((()))()((())()(()(())))))(((()(()))(((()))()()()()))()((((()))(())((()))))((()))(()((()()()()())()())(())(())()()(()()))(((()()))))())((((()))()((())()(()(())((()))))()((()()()()((()))))()(()(()()(()(())(()))))(())(((())))((()()((()))()())()())()((()())))()(((((()()))))(...