QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#555904#5981. Costly Binary Searchyz_ly27 ✓11581ms86796kbC++141.9kb2024-09-10 12:01:252024-09-10 12:01:26

Judging History

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

  • [2024-09-10 12:01:26]
  • 评测
  • 测评结果:27
  • 用时:11581ms
  • 内存:86796kb
  • [2024-09-10 12:01:25]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
inline int read(){
	int f=1,x=0;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-')
			f=-f;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=x*10+ch-'0';
		ch=getchar();
	}
	return f*x;
}
inline void work(int k){
	if(k<0){
		putchar('-');
		k=-k;
	}
	if(k>9)
		work(k/10);
	putchar(k%10+'0');
}
/*
首先有暴力dp
定义dp[l][r]表示在[l,r]的最坏情况最小代价
dp[l][r]=min(c[k]+max(dp[l][k-1],dp[k+1][r]))
因为c[i]<=9,所以dp中的值最多就是clogn的,并且观察式子对于同一个l,当l<=r<=r'的时候dp[l][r]<=dp[l][r'],因为不同区间是取max
对于这种值域很小并且dp值有单调性就可以通过维护每个值域的端点值来维护状态
定义L[c][i]表示右端点为i是,最小的l使得dp[l][i]<=c,R[c][i]同理,维护最大的r
每次转移枚举端点,x=L[c-a[k]][k-1],y=R[c-a[k]][k+1]
L[c][y]=min(x),R[c][x]=max(y)
因为dp值的单调性,再做一个前/后缀,min/max就行了,dp[l][r]<=dp[l][r']->L[v][r]<=L[v][r'],L同理
滚动数组
*/
int t,val[1000005],L[10][1000005],R[10][1000005];
char s[1000005];
int solve(){
	int n=strlen(s+1);
	for(int i=1;i<=n;i++){
		val[i]=s[i]-'0';
	}
	for(int v=0;;v++){
		for(int i=0;i<=n+1;i++){
			L[v%10][i]=i+1;
			R[v%10][i]=i-1;
		}
		for(int i=1;i<=n;i++){
			if(val[i]<=v){
				int l=L[(v-val[i])%10][i-1],r=R[(v-val[i])%10][i+1];
				L[v%10][r]=min(L[v%10][r],l);
				R[v%10][l]=max(R[v%10][l],r);
			}
		}
		for(int i=2;i<=n;i++){
			R[v%10][i]=max(R[v%10][i],R[v%10][i-1]);/*dp[l][r]<=dp[l'][r],l'<=l<=r*/
		}
		for(int i=n-1;i;i--){
			L[v%10][i]=min(L[v%10][i],L[v%10][i+1]);/*dp[l][r]<=dp[l][r'],l<=r<=r'*/
		}
		if(R[v%10][1]>=n)
			return v;
	}
}
int main(){
	t=read();
	for(int i=1;i<=t;i++){
		scanf("%s",s+1);
		printf("Case #%d: %d\n",i,solve());
	}
	return 0;
}

详细

Subtask #1:

score: 8
Accepted

Test #1:

score: 8
Accepted
time: 79ms
memory: 47012kb

input:

50
8
5128831164925953487661279378526736416489768154389124821528467927357884749642747626797857463132866343496157991453139582295398452863172464853698678624523849653538713928372648295734165346972222781179687541337591976277864785653638476127839556323849395641196246971351933655287441377531627938395427487...

output:

Case #1: 8
Case #2: 37
Case #3: 34
Case #4: 37
Case #5: 34
Case #6: 114
Case #7: 126
Case #8: 24
Case #9: 37
Case #10: 103
Case #11: 36
Case #12: 64
Case #13: 37
Case #14: 117
Case #15: 37
Case #16: 35
Case #17: 14
Case #18: 34
Case #19: 36
Case #20: 37
Case #21: 38
Case #22: 39
Case #23: 14
Case #2...

result:

ok 50 lines

Subtask #2:

score: 19
Accepted

Test #2:

score: 19
Accepted
time: 11581ms
memory: 86796kb

input:

50
647322722753778843259213887674615134214258235986992692879314555957455541351526284343217116351733247781713552149464672262787737941588358671583528664757823365936975517145283412965139791726299864122725212222496898855627124979178341548651669956711341742838725446489235961853391195148929571712449139335...

output:

Case #1: 42
Case #2: 43
Case #3: 120
Case #4: 42
Case #5: 43
Case #6: 43
Case #7: 31
Case #8: 43
Case #9: 171
Case #10: 42
Case #11: 39
Case #12: 42
Case #13: 42
Case #14: 44
Case #15: 39
Case #16: 20
Case #17: 180
Case #18: 30
Case #19: 45
Case #20: 43
Case #21: 44
Case #22: 31
Case #23: 83
Case #2...

result:

ok 50 lines

Extra Test:

score: 0
Extra Test Passed