QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#129426#6300. Best Carry Player 2OFforest_1273WA 1ms3768kbC++141.6kb2023-07-22 19:25:192023-07-22 19:25:21

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-22 19:25:21]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3768kb
  • [2023-07-22 19:25:19]
  • 提交

answer

#include<bits/stdc++.h>
#define LL long long
using namespace std;
int read(){int s=0,f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9')s=(s<<1)+(s<<3)+(c^48),c=getchar();return s*f;}
const int N=20;
int T,k,n,m,zero,val[N],res[N];
char x[N];
LL f[N][N][2]/*f[i][j][0/1]:当前考虑到第i位 进位了j次 当前位有没有进位(会给下一位加1)情况下最小的y*/,base[N];
int main(){
	T=read(),base[0]=1;
	for(int i=1;i<N;++i)base[i]=base[i-1]*10;
	while(T--){
		scanf("%s",x+1),k=read(),n=m=strlen(x+1),zero=0;
		while(m>=1&&x[m]=='0')--m,++zero;/*末尾0不影响进位*/
		for(int i=1;i<=m;++i)val[i]=x[m-i+1]-'0';/*反过来*/
		memset(f,0x3f,sizeof(f));
		f[0][0][0]=0;
		for(int i=1;i<N-1/*可能进k位后会很大 18位*/;++i){
			for(int j=0;j<=k;++j){
				f[i][j][0]=min(f[i][j][0],f[i-1][j][0]);
				if(val[i]!=9)f[i][j][0]=min(f[i][j][0],f[i-1][j][1]);/*如果val[i]=9的话就会连续进位两次 不满足*/
				if(val[i]/*0肯定进不了位*/&&j)f[i][j][1]=min(f[i][j][1],f[i-1][j-1][0]+1ll*(10-val[i])*base[i-1]);
				if(j)f[i][j][1]=min(f[i][j][1],f[i-1][j-1][1/*已经给val[i]加了1*/]+1ll*(9-val[i]/*可以少加1*/)*base[i-1]);
			}
		}
		LL ans=min(f[N-2][k][0],f[N-2][k][1]);
		if(!ans){/*特判*/
			vector<int> res;
			for(int i=n;i>=1;--i){
				if(i==1||x[i]!='9'){res.push_back(1);break;}
				res.push_back(0);
			}
			reverse(res.begin(),res.end());/*翻转*/
			for(auto it:res)printf("%d",it);
			printf("\n");continue;
		}
		printf("%lld",ans);
		while(zero){printf("0");--zero;}
		printf("\n");
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3704kb

input:

4
12345678 0
12345678 5
12345678 18
990099 5

output:

1
54322
999999999987654322
9910

result:

ok 4 lines

Test #2:

score: -100
Wrong Answer
time: 1ms
memory: 3768kb

input:

21
999990000099999 0
999990000099999 1
999990000099999 2
999990000099999 3
999990000099999 4
999990000099999 5
999990000099999 6
999990000099999 7
999990000099999 8
999990000099999 9
999990000099999 10
999990000099999 11
999990000099999 12
999990000099999 13
999990000099999 14
999990000099999 15
999...

output:

100000
10000
1000
100
10
1
900001
9900001
99900001
999900001
10000000001
9999910000
9999901000
9999900100
9999900010
9999900001
9000009999900001
99000009999900001
999000009999900001
99900000999990000900000000000000000
100000000000000000

result:

wrong answer 20th lines differ - expected: '99999999999999999900000000000000000', found: '99900000999990000900000000000000000'