QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#160169 | #5246. Nawiasowe podziały [B] | HMSF | 0 | 219ms | 455520kb | C++14 | 4.6kb | 2023-09-02 19:40:17 | 2023-09-02 19:40:18 |
Judging History
answer
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
//#include<set>
//#include<queue>
//#include<stack>
#define PII pair<int,int>
#define MP make_pair
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
inline LL read()
{
LL s=0; bool w=0; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') w=1; ch=getchar();}
while(ch>='0'&&ch<='9'){s=(s<<3)+(s<<1)+(ch^48); ch=getchar();}
return w ? ~s+1 : s;
}
const int maxn = 400005;
const int inf = 2147483624;
const LL infl = 9223372036854775807;
int n,m;
char str[maxn];
int a[maxn]; //前缀和
struct Node
{
int l,r;
vector<int> son;
vector<int> typ;
}tr[maxn*20];
int tot;
void MakeNode(int l,int r,int fa,int x,int t)
{
tr[x].l = l; tr[x].r = r; tr[fa].son.emplace_back(x); tr[fa].typ.emplace_back(t);
}
void Build(int l,int r,int x)
{
if(l>=r) return;
int minn = inf,lst = l;
for(int i=l; i<=r; i++) minn = min(minn,a[i]);
tr[x].son.emplace_back(0); tr[x].typ.emplace_back(0);
for(int i=l; i<=r; i++)
{
if(a[i] == minn)
{
if(lst <= i-1)
{
MakeNode(lst,i-1,x,++tot,0);
Build(lst,i-1,tot);
}
MakeNode(i,i,x,++tot,1);
lst = i+1;
}
else if(i==r) //以防最后一个不是最小值
{
MakeNode(lst,i,x,++tot,0);
Build(lst,i,tot);
}
}
//printf("%d %d~%d son: ",x,tr[x].l,tr[x].r);
//for(int i=1; i<tr[x].son.size(); i++) { printf("%d[%d] ",tr[x].son[i],tr[x].typ[i]); }
//printf("\n");
return;
}
struct DP1
{
LL val; //子串的数目
int num; //切了多少刀
DP1(){}; DP1(LL val_,int num_) {val = val_; num = num_;}
}f[maxn],g[maxn]; //在每个节点处用于计算当前节点的f
LL h[maxn]; //每个节点不被切时的贡献
LL pre[maxn],sum[maxn];
struct DP2
{
LL x,y;
int num;
DP2(){}; DP2(LL x_,LL y_,LL n_){x=x_; y=y_; num=n_;}
}que[maxn<<2];
int hd,tl;
LL tng; //切线的斜率
double Slope(DP2 x,DP2 y) { return y.x == x.x ? inf : (1.0*(y.y - x.y)/(y.x - x.x)); }
void Push(DP2 d)
{
while(tl>hd && Slope(que[tl],d) < Slope(que[tl-1],que[tl])) {tl--;}
que[++tl] = d; return;
}
void Pop() { while(tl>hd && Slope(que[hd],que[hd+1]) < tng) hd++; }
void DP(int u,LL K) //K是每一个段需要减去的额外价值
{
if(tr[u].l > tr[u].r) {f[u].val = f[u].num = 0; return; } //空区间根本没法下刀
if(tr[u].l == tr[u].r) {f[u].val = -K; f[u].num = 1; return;} //如果只有一个,只能切一刀
int sz = tr[u].son.size()-1;
for(int i=1; i<=sz; i++) { DP(tr[u].son[i],K); }
for(int i=1; i<=sz; i++)
{
sum[i] = sum[i-1] + h[tr[u].son[i]];
pre[i] = pre[i-1] + tr[u].typ[i];
}
//printf("now at node %d\n",u);
//for(int i=1; i<=sz; i++) printf("%lld ",sum[i]);
//printf("\n");
//for(int i=1; i<=sz; i++) printf("%lld ",pre[i]);
//printf("\n");
hd = 1; tl = 0;
Push(DP2(0,0,0));
for(int i=1; i<=sz; i++)
{
tng = pre[i]; Pop();
//printf("tangent = %d\n",tng);
//for(int i=hd; i<=tl; i++) printf("(%d %d %d) ",que[i].x,que[i].y,que[i].num);
//printf("\n");
g[i].val = que[hd].y - que[hd].x * tng
+ f[tr[u].son[i]].val + sum[i-1] + ((pre[i]-1)*pre[i])/2;
g[i].num = que[hd].num + f[tr[u].son[i]].num;
Push(DP2(pre[i-1],g[i].val - sum[i] + ((pre[i-1]+1)*pre[i-1])/2,g[i].num));
//printf("g[%d] = %lld\n",i,g[i].val);
}
LL tmp = 0;
LL minn = infl,res = 0;
for(int i=sz; i>=1; i--) //这里是不是可以改成0,来避免后面的特判
{
g[i].val += tmp + ((pre[sz] - pre[i-1])-1)*(pre[sz] - pre[i-1])/2;
tmp += h[tr[u].son[i]];
if(g[i].val<minn || (g[i].val == minn && g[i].num>g[res].val))
{ minn = g[i].val; res = i; }
}
h[u] = sum[sz] + ((pre[sz]-1)*pre[sz])/2;
f[u].val = g[res].val; f[u].num = g[res].num;
//printf("res = %lld f[u] = (%lld %d)\n",res,f[u].val,f[u].num);
//printf("h[%d] = %lld\n",u,h[u]);
//if(f[u].val > h[u]){f[u].val = h[u]; f[u].num = 0;}
return;
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
n = read(); m = read();
scanf("%s",str+1);
a[1] = 0;
for(int i=1; i<=n; i++) a[i+1] = a[i] + (str[i] == '(' ? 1 : -1);
n++;
tr[1].l = 1; tr[1].r = n;
Build(1,n,++tot);
int l = -n*n,r = 0,mid;
LL minans = infl;
while(l<r)
{
mid = (l+r)>>1;
//printf("K=%d\n",mid);
DP(1,mid);
//printf("f1[].val = %d f[1].num = %d\n",f[1].val,f[1].num);
if(f[1].val > h[1]) {f[1].val = h[1]; f[1].num = 0; }
if(f[1].num+1 >= m) {minans = f[1].val; r = mid;}
else l = mid+1;
}
if(minans == infl) {printf("0\n"); return 0;}
printf("%lld\n",minans + (m-1)*l);
//fclose(stdin);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 1
Accepted
time: 56ms
memory: 446436kb
input:
1 1 (
output:
0
result:
ok single line: '0'
Test #2:
score: 0
Accepted
time: 35ms
memory: 448304kb
input:
1 1 )
output:
0
result:
ok single line: '0'
Test #3:
score: 0
Accepted
time: 56ms
memory: 446264kb
input:
2 1 ()
output:
1
result:
ok single line: '1'
Test #4:
score: 0
Accepted
time: 52ms
memory: 446244kb
input:
2 1 )(
output:
0
result:
ok single line: '0'
Test #5:
score: 0
Accepted
time: 48ms
memory: 448228kb
input:
2 2 ()
output:
0
result:
ok single line: '0'
Test #6:
score: 0
Accepted
time: 52ms
memory: 446244kb
input:
2 2 )(
output:
0
result:
ok single line: '0'
Test #7:
score: 0
Accepted
time: 62ms
memory: 448300kb
input:
10 4 ()))(()(()
output:
0
result:
ok single line: '0'
Test #8:
score: -1
Wrong Answer
time: 48ms
memory: 448280kb
input:
15 4 ())))()(()()(((
output:
0
result:
wrong answer 1st lines differ - expected: '1', found: '0'
Subtask #2:
score: 0
Wrong Answer
Test #16:
score: 0
Wrong Answer
time: 71ms
memory: 446244kb
input:
300 25 ((()()()(()())(((())())()))()((()(())))((()()))((()()))()()(((())())))((((())(()()()())(((()))(()())()(())())()()()()())(()((()()()))))(())()((()))((()))(()(()())))((()(())(((()))))((()()()()()())())(((()))()()()(())())(())()(((()))()((()()())))((())())(((()())())((()))(()()()()(())(()())((()...
output:
87
result:
wrong answer 1st lines differ - expected: '90', found: '87'
Subtask #3:
score: 0
Wrong Answer
Test #33:
score: 1
Accepted
time: 68ms
memory: 446424kb
input:
4000 1 (((((())())()((())(())(()()())()())(()(())(()))()(()))(()((())()()()()(()())(()()()))()(())()()(()(()()())))(()))(()(((()))()())((())(()()(()()))))((()((()())())((())())((()()))(())(())(()(()()))(((())())())((())()()(()))(()()()(()(())(()))()())((()))((())()(())()())))(((()(()()()()())(())())...
output:
5599
result:
ok single line: '5599'
Test #34:
score: 0
Accepted
time: 50ms
memory: 446428kb
input:
4000 3 (((((((())))()()((((())(())()())()())(()(()())())))(()((())()((()()()((())()()))()))(())(()(()))()(()()()()((()(()))())()((()()()))(())(()()((())())))))(()(((((())))())((()()(((()()))())())())(())(()())(((()))())(()((()()()))(((()))())())())((())(())((())(())()(())()(((()))((())))))(((((())))...
output:
4568
result:
ok single line: '4568'
Test #35:
score: 0
Accepted
time: 61ms
memory: 448464kb
input:
4000 8 (((()))((()()(()())(()())((()))(((())))))(((())()(())()()()()((())())())((())()((())()))()(((())())()())(((()()()))(()()()(())(()))))(()()((()))((()())())((())(())()(())))(((((()()()())))(()()(()()())()()))()((())(()(()))((())()())((()()))()())(((())())()((()())(()()(())())()()((())))(())()((...
output:
4780
result:
ok single line: '4780'
Test #36:
score: -1
Wrong Answer
time: 76ms
memory: 448484kb
input:
4000 21 (((((()())(())()))(()(())((((())())))()((()))()(()())()((())()())()()((((()))))))(((((()())()()()))((()))())((()))(()()(()))(()()((())))(())((()()())())(()()(())))(((())(()((())))()()(())(()((()()())(()))(()))))((()()((()()()())()())(()((()()()())())))(()()(((())()())()()((())(()))()(()))()(...
output:
3863
result:
wrong answer 1st lines differ - expected: '3864', found: '3863'
Subtask #4:
score: 0
Wrong Answer
Test #54:
score: 1
Accepted
time: 47ms
memory: 448500kb
input:
4000 2 ((())(()(())))()((())()()()(())()()(())()((()))()()((()()))((())())((())())(()())()(())(()(()))((()())())(()())((()()))(()()())((()))()())(()(()))((()()())(())()((())()))(())((())((())))(((()(())))()(())())(()(())(())())((())(())())((()(()))()()()()(()())())(((()))(())())((()())())((())()((()...
output:
18432
result:
ok single line: '18432'
Test #55:
score: 0
Accepted
time: 56ms
memory: 448420kb
input:
4000 5 (((((((()))(())(()(())()()(())(()))()()(((()))())()(()))(((()(()()))()())()()(()(())()(())))((()()()())(((())))((()())()(()()())()()()())()()(()(()))))())((()()())((((((()))()())(()())())())()((()()()(()()((()))()()())(())()())()()()))(())())()(((((()()(()(())()()))))((()()((())())((()())))((...
output:
4336
result:
ok single line: '4336'
Test #56:
score: 0
Accepted
time: 64ms
memory: 448480kb
input:
4000 13 (((((()()))(()()(()((())(()()()))(()))(((()()(()()))()())((())()())()())((()()(())())((()()()()))(())()(())())()((()(())())()()()()())((()(()()()(()())))(()()))(((())(()()))()(())))))()((())((((()()())(()()))(())((()((())))())()(((())()()(()()())(()))()(((()))(((())()()(())))))(((()())))(()(...
output:
3928
result:
ok single line: '3928'
Test #57:
score: -1
Wrong Answer
time: 68ms
memory: 448416kb
input:
4000 34 (((((()())()(((()())()(()(()))(())(((())))()))(()()(()(()(()))))((((()))))((()))(((()()(())))()()(())((()))))()(()(())()()((())))()(()((()))())()((())((((()(()))(()())()()(()(())(())))(()()()())))(()()))((()))((((()()))(((()))))))((((()))((()()())())()(((((())))()(())))())(((())))()((((())))...
output:
3282
result:
wrong answer 1st lines differ - expected: '3285', found: '3282'
Subtask #5:
score: 0
Time Limit Exceeded
Test #75:
score: 1
Accepted
time: 142ms
memory: 455520kb
input:
100000 3 (()((())(()))(())((()())))(((()()))(()))(()()()((())))()()((())(())((())(()(()))())((())))()()((())(()()))(((())))((())()(()())(()())()(()(()()())))(()(()()))()(()((())()()()()())((()))(())(())())(((()))()(()()())()(()()))((()()))(((()()()()))(()))(((())))(((()))(()()())(()(()()))(())(()())...
output:
3832013
result:
ok single line: '3832013'
Test #76:
score: 0
Accepted
time: 147ms
memory: 451624kb
input:
100000 9 (((((())(((())))(()))(()(()))((()()()))()(())(()))(((()()(()))))(()((()()))()(((()))((())))(())(()))(((()())(())((())))(()))()()(()((()))()(()(()())))((()(()))(()()((()())))((()(())())()((()))(()))()(()))((()((())))())(((()(())))()(((()())()()())(())()()(()(())(()())())))((()))(((()())()()(...
output:
131464
result:
ok single line: '131464'
Test #77:
score: 0
Accepted
time: 146ms
memory: 453464kb
input:
100000 15 ()((()(()()))()(())(()))(((()))()((())(())(())(()())))(()()()())(()((())))()(((()))()(()(()))((()))(()))(()((())()()()(()))(())(())(()())(()()))(()(())(())(()()))(((())(())))(())(()()()()()()(()))((()()())())((()))(((())()()()))((())())()((())(())(())()()(()))((()()(()))()()()())()(()())((...
output:
1352858
result:
ok single line: '1352858'
Test #78:
score: 0
Accepted
time: 172ms
memory: 453428kb
input:
100000 21 ((((())()(()()()((())))((())(())))((()()((((())()))()(()()()())()(()())(()(()))))(((()())(())()()((())))))()((()(()()())()(()()(()()((()))))))()(((((())())())((()())(())()()(())()()()))(()))()(((((()())()))((()()))))(((())(())())(()(())((()))))()((((()()())())))()(((())()()()(()(()()()))))...
output:
120911
result:
ok single line: '120911'
Test #79:
score: 0
Accepted
time: 146ms
memory: 453428kb
input:
100000 27 ((()))(()(()))()(())((())())((())()(())())((())())()()(()()())(())(()(()())()(())(())(()()))((())()(()))(()(())()(()()))()(()(()(())()())()(()))((()())()(()())(((()))))((()))(())()((()()()())()(()()())((())())()()((())())(()()()))(()()(())())(()()(()()))(((()))(())()())(())()(()(()()()))((...
output:
1866778
result:
ok single line: '1866778'
Test #80:
score: -1
Time Limit Exceeded
input:
100000 1 )))))()))(()()()()((())(()()()))())())(()()()((()))()(())(((()))((())())()((((()))()()((()((()(()))())))(()))))()()())(()(())()))(()(()((()(())((((()((()())()))())())(((((((())())))(())))((()(())()(((((()()(()(()))(()))(())))()())()))))((()())))()))(()()))(((())(()(()()()(()))(((((((())()()...
output:
result:
Subtask #6:
score: 0
Wrong Answer
Test #89:
score: 1
Accepted
time: 169ms
memory: 453524kb
input:
100000 6 ((((()()((())))()((()())()())((())()(((()()))())((())((()()()))()()()(())))(((()(())(()()))(())())(())))(((()))((((())))()))(()((())(())(())((()())))(()(()(()))(())(()()()(())())(()(())(()(()))(()())()((()))()()))(()()(())))((()())()((())((())()()(((()))(())))((())())()((()())(()))))()(((()...
output:
126039
result:
ok single line: '126039'
Test #90:
score: 0
Accepted
time: 169ms
memory: 453516kb
input:
100000 12 ((()((())(((()))()())()()((()))())((((()()))()()))))(((())(()))(((((())))((())))(()()((()()()))((())()()((()(())((()))()()))())(()((()())())(()()(()))())()(()(())(())))()((()(()))((()))()((()()((())(())))))())(((()()))(()((()()))()(((()()()())()())))())(()((()())(())()(((()()))(()()()()))(...
output:
130680
result:
ok single line: '130680'
Test #91:
score: 0
Accepted
time: 161ms
memory: 453568kb
input:
100000 18 ((())(()()()()())()()(())((())()()()())()()()()((()()())((()))()()(()())()()()())(()())()(()(())))(()()((()()))(())()()(()())()(()())((()())()()(())()())((())(()))(()()()))((())(()())()(((())))(()(()))(()(()))()(())()(((())())()()())((())))((()())((()())))((()))(()(()()()()((())))()(()))((...
output:
525576
result:
ok single line: '525576'
Test #92:
score: 0
Accepted
time: 162ms
memory: 451720kb
input:
100000 24 ((((((())()((())))()()(())((()())())()()(((()))()()(((())))())(()())(()(()()(())((()())(()())))(()())()(()(())()())(()((()))))()(((()(()()))())())((()))()()(()(()))())((()()()()(()(()))(()))()(()))()(((()))(()()(()(()))())()((()()())()((())(())((())(())()()())()())((())))()((()())((()()()(...
output:
118587
result:
ok single line: '118587'
Test #93:
score: 0
Accepted
time: 158ms
memory: 453460kb
input:
100000 30 ((((())()()(((()()())())))((()))(())(()()(())))((())())((())(())())(((()(()(()))())(()())()()(())))(((()())(()))()((())((())()())((())()()()())()()()(())()()(()()))(((()()))(()()())(()(()()(())()))(())((())(()))(())()(()))))(((())(()()())()()((()))(()(()()()()((())))))((((()()()))(()((()))...
output:
132047
result:
ok single line: '132047'
Test #94:
score: 0
Accepted
time: 219ms
memory: 453788kb
input:
99999 2 ()))())))))()))()))(()()))())))())(()))(((())(()(((()))(((()((((((((((((()))(())()(()((())(()())(())())()())()())()(()(((())())()(()(())(()))))))))))()))()()))())()(()())))((()(()((()))))))(()()())))()((())))))((()(((()()(())())()()))(())())())())))(())))(())()))())())(((((()()((((((()()(())...
output:
97853
result:
ok single line: '97853'
Test #95:
score: -1
Wrong Answer
time: 189ms
memory: 453660kb
input:
100000 5 )()()())(()())()()(())()))(()((())))(((((()())()((())()((()))()))(((()))(((()())((())((()())())()())))((((()))))(()((())(((()))(()((((()(())(((()(((()()))))((()((()(()(()((((()()()()(((()())))(((()(()()))))))()(()))((())()(((()(()())))))(((((()(())())(((()())()(())(()))()()()))()()())(((())...
output:
96850
result:
wrong answer 1st lines differ - expected: '96190', found: '96850'
Subtask #7:
score: 0
Wrong Answer
Test #103:
score: 1
Accepted
time: 129ms
memory: 452708kb
input:
80000 81 ((((((()())((()((()))))(()())()(()(((()(()())(()())())(())))()(())(()()))()))(((()(()))((()))(())((((())))(())()((()))())((()(()())))(())(()()))(((()()))(()((())))())()(((()()))(()())()()(((()((()))(())))((())(()((())))())(()())))()()()((()()(()()(())()()((()))(())()))((()(())()()))(()(()()...
output:
87799
result:
ok single line: '87799'
Test #104:
score: -1
Wrong Answer
time: 131ms
memory: 452848kb
input:
80000 729 (((())(()))(()()())(()(()))(()())(()()()())(((()()())))()(()()()(()())))(((())((()()())))())((()(())()()())((()()()))()(()()()())()()(((())(()))(()())()()()(())))((()))((()())()(()()())(()()()()())(())(((()))(()(()())())())(()()())(()(())()()()()(())())()())(((())()))(((()())(())()(())((()...
output:
76293
result:
wrong answer 1st lines differ - expected: '76309', found: '76293'
Subtask #8:
score: 0
Wrong Answer
Test #129:
score: 1
Accepted
time: 109ms
memory: 452920kb
input:
79856 243 (())()(())(())(()()())()(())()(()())()(())()(())()((()))(())(()(())())()(()())()()(())(())((()()))(())(()()())(()())((()())()())(())()()(())()(())((()()))()()(()()())(())()()(()())(())(())((()))(())(())()()()()()(()(()))(()(())()(()))()()()()()()()()((()()))()()()()()(())()(())(()())()(()(...
output:
794572
result:
ok single line: '794572'
Test #130:
score: -1
Wrong Answer
time: 143ms
memory: 452888kb
input:
79156 2187 ((((((())))((()))()(()(())()()())(()))((()(())()))((((())()))(()(((()()))())(()())(())(())(()()(())(()))(((())))(()()))()(((()()()))(()(()())))(((())(()(()))(())()())()()(())))((())()(())())(((()()()()())(())(())()((((()))(())())))((()(()()))((()()))())(())(()(()(()))()()()())((()()())())...
output:
43604
result:
wrong answer 1st lines differ - expected: '43675', found: '43604'
Subtask #9:
score: 0
Wrong Answer
Test #155:
score: 1
Accepted
time: 154ms
memory: 451988kb
input:
99599 64 ((((((()(((())()))))((((((()))()())())())(()(()(()())))(()(()()(())())((()(()()))((()))()(())()())()(()(())()((()()())()())()(((())()))(()))((())(()(())()))(()))())()(((())(((())()((())))())(()()(()))(()())()((())((()())()(())())(())()(())((())(()()(()))((()))(()()())))()(((()))(())))(())((...
output:
110447
result:
ok single line: '110447'
Test #156:
score: -1
Wrong Answer
time: 161ms
memory: 453632kb
input:
100000 1024 ((((((((()()))(((()()()(((())())))()(((())))((())()(()()())(()()))((()())(())()()))))(((((())())()))))(((((((()))))())(((())()()((())))((()()))())()(()()())((()()(((()))()())(()(()()))()((()(()))(())()(())()()(()())())()((()))(())())()()))())(((()()((()(())()))(()))((()())((()()()))()(()...
output:
74449
result:
wrong answer 1st lines differ - expected: '74496', found: '74449'
Subtask #10:
score: 0
Wrong Answer
Test #181:
score: 1
Accepted
time: 149ms
memory: 453512kb
input:
100000 256 ((((())()(())())(((()))()((())()(()(())))))(((()(()))(((()))()()()()))()((((()))(())((()))))((()))(()((()()()()())()())(())(())()()(()()))(((()()))))())((((()))()((())()(()(())((()))))()((()()()()((()))))()(()(()()(()(())(()))))(())(((())))((()()((()))()())()())()((()())))()(((((()()))))(...
output:
107402
result:
ok single line: '107402'
Test #182:
score: -1
Wrong Answer
time: 171ms
memory: 451640kb
input:
99728 4096 ((((((()((()))))()))((((((()()(()())))(())()(()))()(()()()()()((()()()))()(((()))()(()))((()))(()()())()()))())(((()))(())((()((())()(()()))()))()(((())(())(()))(())((()(()(())()())()))((()())()())(()())(((()()()(())))))())(((())()())((()())())((()((()()())()()))()((()))))(()(((()((()))))...
output:
41856
result:
wrong answer 1st lines differ - expected: '41913', found: '41856'