CF440C

C. One-Based Arithmetic
time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Prof. Vasechkin wants to represent positive integer n as a sum of addends, where each addends is an integer number containing only 1s. For example, he can represent 121 as 121=111+11+–1. Help him to find the least number of digits 1 in such sum.

Input

The first line of the input contains integer n (1 ≤ n < 1015).

Output

Print expected minimal number of digits 1.

Sample test(s)
Input
121
Output
6

http://codeforces.com/contest/440/problem/C

 

用一堆由1组成的数来加减得到某数,输入某数,求最少要用多少个1。

深搜!由于这题的性质,每次搜肯定能得到一个解,然后之后搜的时候用的1数大于这个解,就可以跳出,怒剪了一大波枝。(最优性剪枝)

就是从最高位开始,慢慢把位削成0。因为把第i位消除成0后,可以选择数保证这一位不会再变回1,所以就一位一位消下去就行。

深搜只有两种方向,例如第i位是x,第一种方向就是搞x个11111把它消了,另一种是搞一堆11111把它加上去,然后搞一个多一位的111111把它再消掉,这两种都有可能,而其他的方法都不如这2种。

所以哐哐哐就是搜啦!搜啊!

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<cstdlib>
 5 #include<cmath>
 6 using namespace std;
 7 
 8 typedef long long ll;
 9 
10 int ans,len;
11 ll one[17]={0,1,11,111,1111,11111,111111,1111111,11111111,111111111,
12             1111111111LL,11111111111LL,111111111111LL,1111111111111LL,
13             11111111111111LL,111111111111111LL,1111111111111111LL};
14 void dfs(ll x,int sum)
15 {
16     int a,b;
17     if(sum>=ans) return;
18     if(x==0)
19     {
20         ans=sum;
21         return;
22     }
23     if(x<0) x=-x;
24     ll y=x;
25     int t=0;
26     while(y!=0) t++,y/=10;
27     ll reta=x,retb=one[t+1]-x;
28     ll h=pow(10,t-1);
29     int sa=0,sb=t+1;
30     while(reta>=h) reta-=one[t],sa+=t;
31     while(retb>=h) retb-=one[t],sb+=t;
32     dfs(reta,sum+sa);
33     dfs(retb,sum+sb);
34     return;
35 }
36 
37 int main()
38 {
39     ll x;
40     while(scanf("%I64d",&x)!=EOF)
41     {
42         ans=1e9;
43         dfs(x,0);
44         printf("%d\n",ans);
45     }
46     return 0;
47 }
View Code

 

 

posted @ 2014-06-07 23:23  带鱼Yuiffy  阅读(814)  评论(0)    收藏  举报