QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#22341#2356. Partition of QueriesDaBenZhongXiaSongKuaiDi#WA 4ms7884kbC++201.1kb2022-03-09 15:37:522022-04-30 00:55:11

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:55:11]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:7884kb
  • [2022-03-09 15:37:52]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1000005;
int n;
ll y;
int a[N],tot=0;
ll f[N],ans=100000000000000ll;
int q[N],nowl=1,nowr=0;
struct Number{
	ll a,b;
};
Number Slope(int i,int j){
	ll x1=f[i]+(ll)a[i]*tot-f[j]-(ll)a[j]*tot;
	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);
}
int main(){
	scanf("%d %lld\n",&n,&y);
	int lst=0;
	for(int i=1;i<=n;i++){
		char x;
		scanf("%c",&x);
		//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],i),Slope(q[nowr-1],q[nowr]))) nowr--;
		q[++nowr]=i;
	}
	cout<<ans<<endl;
	return 0;
}
/*
12 6
++++?+++?+??
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

6 5
++??+?

output:

6

result:

ok single line: '6'

Test #2:

score: 0
Accepted
time: 4ms
memory: 7680kb

input:

6 8
++??+?

output:

7

result:

ok single line: '7'

Test #3:

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

input:

5 1
+++++

output:

0

result:

ok single line: '0'

Test #4:

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

input:

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

output:

0

result:

ok single line: '0'

Test #5:

score: 0
Accepted
time: 0ms
memory: 7680kb

input:

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

output:

19

result:

ok single line: '19'

Test #6:

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

input:

1 1
?

output:

0

result:

ok single line: '0'

Test #7:

score: 0
Accepted
time: 0ms
memory: 7808kb

input:

9 7
++++++++?

output:

7

result:

ok single line: '7'

Test #8:

score: 0
Accepted
time: 4ms
memory: 7812kb

input:

9 8
++++++++?

output:

8

result:

ok single line: '8'

Test #9:

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

input:

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

output:

15

result:

ok single line: '15'

Test #10:

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

input:

5 3
+?+?+

output:

3

result:

ok single line: '3'

Test #11:

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

input:

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

output:

10

result:

ok single line: '10'

Test #12:

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

input:

10 7
+?+?+??+??

output:

12

result:

ok single line: '12'

Test #13:

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

input:

15 4
+?+?+??+??+??+?

output:

15

result:

wrong answer 1st lines differ - expected: '14', found: '15'