题解:CF2121E Sponsor of Your Problems
题意
对于正整数
做法
从高位往低位考虑,假设当前在第
-
这一位上
l 和r 仍然相等。此时x 在这一位上只有一种选择,答案加二。 -
这一位上
l 和r 不相等,且差值至少为2 。此时x 可以在这一位上选择一个在中间的数,更低位可以随便选,所以不会再有答案了,结束。 -
这一位上
l 和r 不相等,且差值为1 。x 可以选择取两个值中的一个。如果选择较小者,则在l 出现非9 的数字之前,都会有贡献。同理,选择较大者会在r 出现非0 的数字之前有贡献。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
void solve(){
string l,r;
cin>>l>>r;
int res=0;
for(int i=0;i<l.length();i++){
if(l[i]==r[i])res+=2;
else if(l[i]+1==r[i]){
int tmp=0x3f3f3f3f;
int pos=i+1;
while(pos<l.length()&&l[pos]=='9')pos++;
tmp=min(tmp,pos-i);
pos=i+1;
while(pos<r.length()&&r[pos]=='0')pos++;
tmp=min(tmp,pos-i);
res+=tmp;
break;
}
else{
break;
}
}
cout<<res<<"\n";
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tt;
cin>>tt;
while(tt--)solve();
return 0;
}