ABC340C 题解
题意
删除一个整数
思路
若设
显然可以用递归解决。
但是直接暴力递归时间会炸。
所以考虑使用记忆化搜索,用map记录下每个数所需的代价。
时间复杂度应为
代码
#include<iostream>
#include<map>
#define int long long
using namespace std;
int read()
{
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){ if(ch=='-') w=-1; if(ch==EOF) return 0; ch=getchar();}
while(ch>='0'&&ch<='9'){ s=s*10+ch-'0'; ch=getchar();}
return s*w;
}
int n,ans=0;
map <int,int> ma;
int erase(int x)
{
if(x<2)
return 0;
if(!ma.count(x))
{
if(x%2==0)
ma[x]=erase(x/2)*2+x;
else
ma[x]=erase(x/2)+erase(x/2+1)+x;//简单分类讨论+记忆化
}
return ma[x];
}
signed main()
{
n=read();
cout<<erase(n);
return 0;
}