传送门:点击打开链接
题意:只能使用由1组成的数字,通过加法和减法得到n。问这些数字的1个个数之和最少是多少。
思路:非常经典的一道暴搜题,每一层有2个分支,层数递减,很容易证明复杂度最坏大约是O(2^16)
#include<map>
#include<set>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<algorithm>
#include<functional>
#define fuck(x) cout<<"["<<x<<"]"
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w+",stdout)
using namespace std;
typedef long long LL;
typedef pair<LL, int>PII;
const int MX = 1e6 + 5;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
LL one[100];
int DFS(LL n, int d) {
int ret = n / one[d] * d;
n %= one[d];
if(!n) return ret;
return ret + min(DFS(one[d] - n, d - 1) + d, DFS(n, d - 1));
}
int main() {
for(int i = 1; i <= 16; i++) {
one[i] = one[i - 1] * 10 + 1;
}
LL n; scanf("%I64d", &n);
printf("%d\n", DFS(n, 16));
return 0;
}