QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#22363#2356. Partition of QueriesDaBenZhongXiaSongKuaiDi#WA 3ms3792kbC++201.3kb2022-03-09 16:18:552022-04-30 00:57:35

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-04-30 00:57:35]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:3792kb
  • [2022-03-09 16:18:55]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1000005;
int n;
ll y;
ll a[N];
int tot=0;
ll f[N],ans=100000000000000ll;
int q[N<<1],nowl=1,nowr=0;
struct Number{
	ll a,b;
};
Number Slope(int i,int j){
	ll x1=f[i]+(ll)a[i]*(tot+1)-f[j]-(ll)a[j]*(tot+1);
	ll x2=a[i]-a[j];
	if(x2<0) x1=-x1,x2=-x2;
	return (Number){x1,x2};
}
bool Comp(Number k1,Number k2){
	return (k1.a*k2.b<k1.b*k2.a);
}
inline ll Min(ll A,ll B){return (A>B)?B:A;}
int main(){
	scanf("%d %lld\n",&n,&y);
	//n=1000000,y=1000000;
	int lst=0;
	for(int i=1;i<=n;i++){
		char x;
		scanf("%c",&x);
		//x=((i&1)?'+':'?');
		//cout<<i<<" "<<x<<endl;
		if(x=='?') a[++tot]=(i-lst-1),lst=i;
	}
	for(int i=1;i<=tot;i++) a[i]+=a[i-1];
	for(int i=1;i<=tot;i++) f[0]+=a[i];
	ans=Min(ans,f[0]);//for(int i=1;i<=tot;i++) cout<<a[i]<<" ";cout<<endl;
	q[++nowr]=0;
	for(int i=1;i<=tot;i++){
		while(nowl<nowr && !Comp(Slope(q[nowl],q[nowl+1]),(Number){-i,1})) nowl++;
		int p=q[nowl];
		f[i]=f[p]+y-(ll)(a[i]-a[p])*(tot-i+1);ans=Min(ans,f[i]);
		//cout<<i<<" zhuan: "<<p<<" "<<f[i]<<endl;
		while(nowl<nowr && !Comp(Slope(q[nowr-1],q[nowr]),Slope(q[nowr],i))) nowr--;
		q[++nowr]=i;
	}
	cout<<ans<<endl;
	return 0;
}
/*
12 6
++++?+++?+??
8 1
+??+++?+
8 0
+++?++??
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 3792kb

input:

6 5
++??+?

output:

6

result:

ok single line: '6'

Test #2:

score: 0
Accepted
time: 2ms
memory: 3748kb

input:

6 8
++??+?

output:

7

result:

ok single line: '7'

Test #3:

score: 0
Accepted
time: 2ms
memory: 3716kb

input:

5 1
+++++

output:

0

result:

ok single line: '0'

Test #4:

score: 0
Accepted
time: 2ms
memory: 3680kb

input:

10 0
++?+??++??

output:

0

result:

ok single line: '0'

Test #5:

score: 0
Accepted
time: 3ms
memory: 3724kb

input:

12 100
+?+++??+??++

output:

19

result:

ok single line: '19'

Test #6:

score: 0
Accepted
time: 3ms
memory: 3636kb

input:

1 1
?

output:

0

result:

ok single line: '0'

Test #7:

score: 0
Accepted
time: 2ms
memory: 3732kb

input:

9 7
++++++++?

output:

7

result:

ok single line: '7'

Test #8:

score: 0
Accepted
time: 3ms
memory: 3724kb

input:

9 8
++++++++?

output:

8

result:

ok single line: '8'

Test #9:

score: 0
Accepted
time: 1ms
memory: 3740kb

input:

10 15
++++++++??

output:

15

result:

ok single line: '15'

Test #10:

score: 0
Accepted
time: 3ms
memory: 3732kb

input:

5 3
+?+?+

output:

3

result:

ok single line: '3'

Test #11:

score: -100
Wrong Answer
time: 3ms
memory: 3676kb

input:

10 5
+?+?+??+??

output:

16

result:

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