QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#137321#2356. Partition of QuerieskiwiHM#WA 2ms9828kbC++141.4kb2023-08-10 10:24:532023-08-10 10:24:53

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-10 10:24:53]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:9828kb
  • [2023-08-10 10:24:53]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e6 + 5;

int n, y, top;
int a[maxn], b[maxn];
long long f[maxn], g[maxn];
long long K[maxn], B[maxn];
char s[maxn];

inline long long eval(int x){
	if(top == 0) return 1e18;
	if(top == 1 || top > 1 && K[1] * x + B[1] <= K[2] * x + B[2]){
		// printf("%lld * %d + %lld\n", K[1], x, B[1]);
		return K[1] * x + B[1];
	}
	int lb = 1, rb = top;
	for(; lb + 1 < rb; ){
		int md = (lb + rb) >> 1;
		if(K[md] * x + B[md] > K[md + 1] * x + B[md + 1])
			lb = md;
		else
			rb = md;
	}
	// printf("%lld * %d + %lld\n", K[lb + 1], x, B[lb + 1]);
	return K[lb + 1] * x + B[lb + 1];
}

inline void insert(long long k, long long b){
	for(; top > 1 && (B[top] - b) * (k - K[top - 1]) < (B[top - 1] - b) * (k - K[top]); --top);
	++top, B[top] = b, K[top] = k;
	return;
}

int main(){
	scanf("%d%d", &n, &y);
	scanf("%s", s + 1);
	for(int i = 1; i <= n; ++i){
		a[i] = a[i - 1] + (s[i] == '?');
		b[i] = b[i - 1] + (s[i] == '+');
		g[i] = g[i - 1] + (s[i] == '?') * b[i];
	}
	
	for(int i = 1; i <= n; ++i){
		f[i] = min(g[i], g[i] + y + eval(a[i]));
		// printf("i = %d g = %lld f = %lld\n", i, g[i], f[i]);
		insert(-1ll * b[i], f[i] - g[i] + 1ll * a[i] * b[i]);
		// printf("K = %lld B = %lld\n", -1ll * b[i], f[i] - g[i] + 1ll * a[i] * b[i]);
	}
	
	printf("%lld\n", f[n]);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 8004kb

input:

6 5
++??+?

output:

6

result:

ok single line: '6'

Test #2:

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

input:

6 8
++??+?

output:

7

result:

ok single line: '7'

Test #3:

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

input:

5 1
+++++

output:

0

result:

ok single line: '0'

Test #4:

score: -100
Wrong Answer
time: 0ms
memory: 9828kb

input:

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

output:

2

result:

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