QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#160181#5246. Nawiasowe podziały [B]HMSF0 195ms454964kbC++144.6kb2023-09-02 19:43:092023-09-02 19:43:10

Judging History

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

  • [2023-09-02 19:43:10]
  • 评测
  • 测评结果:0
  • 用时:195ms
  • 内存:454964kb
  • [2023-09-02 19:43:09]
  • 提交

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;
	}
	DP(1,l); minans = f[1].val;
	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: 0
Wrong Answer
time: 52ms
memory: 448276kb

input:

1 1
(

output:

4

result:

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

Subtask #2:

score: 0
Wrong Answer

Test #16:

score: 0
Wrong Answer
time: 71ms
memory: 446200kb

input:

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

output:

87

result:

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

Subtask #3:

score: 0
Wrong Answer

Test #33:

score: 0
Wrong Answer
time: 48ms
memory: 448500kb

input:

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

output:

16013214

result:

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

Subtask #4:

score: 0
Wrong Answer

Test #54:

score: 1
Accepted
time: 72ms
memory: 448484kb

input:

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

output:

18432

result:

ok single line: '18432'

Test #55:

score: 0
Accepted
time: 59ms
memory: 448348kb

input:

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

output:

4336

result:

ok single line: '4336'

Test #56:

score: 0
Accepted
time: 44ms
memory: 446456kb

input:

4000 13
(((((()()))(()()(()((())(()()()))(()))(((()()(()()))()())((())()())()())((()()(())())((()()()()))(())()(())())()((()(())())()()()()())((()(()()()(()())))(()()))(((())(()()))()(())))))()((())((((()()())(()()))(())((()((())))())()(((())()()(()()())(()))()(((()))(((())()()(())))))(((()())))(()(...

output:

3928

result:

ok single line: '3928'

Test #57:

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

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: 155ms
memory: 453448kb

input:

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

output:

3832013

result:

ok single line: '3832013'

Test #76:

score: 0
Accepted
time: 163ms
memory: 453516kb

input:

100000 9
(((((())(((())))(()))(()(()))((()()()))()(())(()))(((()()(()))))(()((()()))()(((()))((())))(())(()))(((()())(())((())))(()))()()(()((()))()(()(()())))((()(()))(()()((()())))((()(())())()((()))(()))()(()))((()((())))())(((()(())))()(((()())()()())(())()()(()(())(()())())))((()))(((()())()()(...

output:

131464

result:

ok single line: '131464'

Test #77:

score: 0
Accepted
time: 161ms
memory: 453432kb

input:

100000 15
()((()(()()))()(())(()))(((()))()((())(())(())(()())))(()()()())(()((())))()(((()))()(()(()))((()))(()))(()((())()()()(()))(())(())(()())(()()))(()(())(())(()()))(((())(())))(())(()()()()()()(()))((()()())())((()))(((())()()()))((())())()((())(())(())()()(()))((()()(()))()()()())()(()())((...

output:

1352858

result:

ok single line: '1352858'

Test #78:

score: 0
Accepted
time: 156ms
memory: 453544kb

input:

100000 21
((((())()(()()()((())))((())(())))((()()((((())()))()(()()()())()(()())(()(()))))(((()())(())()()((())))))()((()(()()())()(()()(()()((()))))))()(((((())())())((()())(())()()(())()()()))(()))()(((((()())()))((()()))))(((())(())())(()(())((()))))()((((()()())())))()(((())()()()(()(()()()))))...

output:

120911

result:

ok single line: '120911'

Test #79:

score: 0
Accepted
time: 147ms
memory: 451568kb

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: 173ms
memory: 451480kb

input:

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

output:

126039

result:

ok single line: '126039'

Test #90:

score: 0
Accepted
time: 147ms
memory: 451932kb

input:

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

output:

130680

result:

ok single line: '130680'

Test #91:

score: 0
Accepted
time: 145ms
memory: 451424kb

input:

100000 18
((())(()()()()())()()(())((())()()()())()()()()((()()())((()))()()(()())()()()())(()())()(()(())))(()()((()()))(())()()(()())()(()())((()())()()(())()())((())(()))(()()()))((())(()())()(((())))(()(()))(()(()))()(())()(((())())()()())((())))((()())((()())))((()))(()(()()()()((())))()(()))((...

output:

525576

result:

ok single line: '525576'

Test #92:

score: 0
Accepted
time: 154ms
memory: 453444kb

input:

100000 24
((((((())()((())))()()(())((()())())()()(((()))()()(((())))())(()())(()(()()(())((()())(()())))(()())()(()(())()())(()((()))))()(((()(()()))())())((()))()()(()(()))())((()()()()(()(()))(()))()(()))()(((()))(()()(()(()))())()((()()())()((())(())((())(())()()())()())((())))()((()())((()()()(...

output:

118587

result:

ok single line: '118587'

Test #93:

score: 0
Accepted
time: 160ms
memory: 453344kb

input:

100000 30
((((())()()(((()()())())))((()))(())(()()(())))((())())((())(())())(((()(()(()))())(()())()()(())))(((()())(()))()((())((())()())((())()()()())()()()(())()()(()()))(((()()))(()()())(()(()()(())()))(())((())(()))(())()(()))))(((())(()()())()()((()))(()(()()()()((())))))((((()()()))(()((()))...

output:

132047

result:

ok single line: '132047'

Test #94:

score: 0
Accepted
time: 195ms
memory: 453792kb

input:

99999 2
()))())))))()))()))(()()))())))())(()))(((())(()(((()))(((()((((((((((((()))(())()(()((())(()())(())())()())()())()(()(((())())()(()(())(()))))))))))()))()()))())()(()())))((()(()((()))))))(()()())))()((())))))((()(((()()(())())()()))(())())())())))(())))(())()))())())(((((()()((((((()()(())...

output:

97853

result:

ok single line: '97853'

Test #95:

score: -1
Wrong Answer
time: 189ms
memory: 453684kb

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: 141ms
memory: 448884kb

input:

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

output:

87799

result:

ok single line: '87799'

Test #104:

score: -1
Wrong Answer
time: 127ms
memory: 452936kb

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: 124ms
memory: 454964kb

input:

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

output:

794572

result:

ok single line: '794572'

Test #130:

score: -1
Wrong Answer
time: 128ms
memory: 448796kb

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: 161ms
memory: 451964kb

input:

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

output:

110447

result:

ok single line: '110447'

Test #156:

score: -1
Wrong Answer
time: 163ms
memory: 449356kb

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: 147ms
memory: 453476kb

input:

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

output:

107402

result:

ok single line: '107402'

Test #182:

score: -1
Wrong Answer
time: 151ms
memory: 451816kb

input:

99728 4096
((((((()((()))))()))((((((()()(()())))(())()(()))()(()()()()()((()()()))()(((()))()(()))((()))(()()())()()))())(((()))(())((()((())()(()()))()))()(((())(())(()))(())((()(()(())()())()))((()())()())(()())(((()()()(())))))())(((())()())((()())())((()((()()())()()))()((()))))(()(((()((()))))...

output:

41856

result:

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