#include<iostream>
#include<algorithm>
#include<cstring>
#include<set>
using namespace std;
const int N=2e5+10;
int c[20];
typedef long long LL;
int i=0;
int l[10]={9,8,7,6,5,4,3,2,1,0};
int r[10]={1,2,3,4,5,6,7,8,9,0};
LL n;
int a,b;
LL maxv=0;
void dfs(int u,LL m){
if(u>i){
maxv=max(maxv,m);
return ;
}
if(c[u]==9) dfs(u+1,m*10+9);
if(b>=r[c[u]]||a>=l[c[u]]){
if(b>=r[c[u]]){
b-=r[c[u]];
dfs(u+1,m*10+9);
b+=r[c[u]];
}
if(a>=l[c[u]]){
a-=l[c[u]];
dfs(u+1,m*10+9);
a+=l[c[u]];
}
}
else{
int p=a;
a=0;
dfs(u+1,m*10+p+c[u]);
a=p;
}
}
int main(){
cin>>n>>a>>b;
LL t=n;
while(n){
c[++i]=n%10;
n/=10;
}
for(int j=1,k=i;j<=k;j++,k--){
swap(c[j],c[k]);
}
dfs(1,0);
cout<<max(maxv,t)<<endl;
return 0;
}
【DFS】最大数——蓝桥杯第十三届
最新推荐文章于 2024-07-06 19:19:19 发布