QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#68802 | #5246. Nawiasowe podziały [B] | Adam_GS | 0 | 1813ms | 17320kb | C++20 | 4.3kb | 2022-12-21 02:59:56 | 2022-12-21 02:59:57 |
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+1)/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
Time Limit Exceeded
Test #1:
score: 0
Time Limit Exceeded
input:
1 1 (
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: 0
Time Limit Exceeded
input:
4000 1 (((((())())()((())(())(()()())()())(()(())(()))()(()))(()((())()()()()(()())(()()()))()(())()()(()(()()())))(()))(()(((()))()())((())(()()(()()))))((()((()())())((())())((()()))(())(())(()(()()))(((())())())((())()()(()))(()()()(()(())(()))()())((()))((())()(())()())))(((()(()()()()())(())())...
output:
result:
Subtask #4:
score: 0
Wrong Answer
Test #54:
score: 0
Wrong Answer
time: 56ms
memory: 6532kb
input:
4000 2 ((())(()(())))()((())()()()(())()()(())()((()))()()((()()))((())())((())())(()())()(())(()(()))((()())())(()())((()()))(()()())((()))()())(()(()))((()()())(())()((())()))(())((())((())))(((()(())))()(())())(()(())(())())((())(())())((()(()))()()()()(()())())(((()))(())())((()())())((())()((()...
output:
18430
result:
wrong answer 1st lines differ - expected: '18432', found: '18430'
Subtask #5:
score: 0
Time Limit Exceeded
Test #75:
score: 0
Time Limit Exceeded
input:
100000 3 (()((())(()))(())((()())))(((()()))(()))(()()()((())))()()((())(())((())(()(()))())((())))()()((())(()()))(((())))((())()(()())(()())()(()(()()())))(()(()()))()(()((())()()()()())((()))(())(())())(((()))()(()()())()(()()))((()()))(((()()()()))(()))(((())))(((()))(()()())(()(()()))(())(()())...
output:
result:
Subtask #6:
score: 0
Time Limit Exceeded
Test #89:
score: 0
Time Limit Exceeded
input:
100000 6 ((((()()((())))()((()())()())((())()(((()()))())((())((()()()))()()()(())))(((()(())(()()))(())())(())))(((()))((((())))()))(()((())(())(())((()())))(()(()(()))(())(()()()(())())(()(())(()(()))(()())()((()))()()))(()()(())))((()())()((())((())()()(((()))(())))((())())()((()())(()))))()(((()...
output:
result:
Subtask #7:
score: 0
Wrong Answer
Test #103:
score: 0
Wrong Answer
time: 1445ms
memory: 15820kb
input:
80000 81 ((((((()())((()((()))))(()())()(()(((()(()())(()())())(())))()(())(()()))()))(((()(()))((()))(())((((())))(())()((()))())((()(()())))(())(()()))(((()()))(()((())))())()(((()()))(()())()()(((()((()))(())))((())(()((())))())(()())))()()()((()()(()()(())()()((()))(())()))((()(())()()))(()(()()...
output:
87718
result:
wrong answer 1st lines differ - expected: '87799', found: '87718'
Subtask #8:
score: 0
Time Limit Exceeded
Test #129:
score: 0
Time Limit Exceeded
input:
79856 243 (())()(())(())(()()())()(())()(()())()(())()(())()((()))(())(()(())())()(()())()()(())(())((()()))(())(()()())(()())((()())()())(())()()(())()(())((()()))()()(()()())(())()()(()())(())(())((()))(())(())()()()()()(()(()))(()(())()(()))()()()()()()()()((()()))()()()()()(())()(())(()())()(()(...
output:
result:
Subtask #9:
score: 0
Wrong Answer
Test #155:
score: 0
Wrong Answer
time: 1676ms
memory: 17096kb
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: 1813ms
memory: 17320kb
input:
100000 256 ((((())()(())())(((()))()((())()(()(())))))(((()(()))(((()))()()()()))()((((()))(())((()))))((()))(()((()()()()())()())(())(())()()(()()))(((()()))))())((((()))()((())()(()(())((()))))()((()()()()((()))))()(()(()()(()(())(()))))(())(((())))((()()((()))()())()())()((()())))()(((((()()))))(...
output:
107146
result:
wrong answer 1st lines differ - expected: '107402', found: '107146'