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种。

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

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<cmath>
using namespace std; typedef long long ll; int ans,len;
ll one[]={,,,,,,,,,,
1111111111LL,11111111111LL,111111111111LL,1111111111111LL,
11111111111111LL,111111111111111LL,1111111111111111LL};
void dfs(ll x,int sum)
{
int a,b;
if(sum>=ans) return;
if(x==)
{
ans=sum;
return;
}
if(x<) x=-x;
ll y=x;
int t=;
while(y!=) t++,y/=;
ll reta=x,retb=one[t+]-x;
ll h=pow(,t-);
int sa=,sb=t+;
while(reta>=h) reta-=one[t],sa+=t;
while(retb>=h) retb-=one[t],sb+=t;
dfs(reta,sum+sa);
dfs(retb,sum+sb);
return;
} int main()
{
ll x;
while(scanf("%I64d",&x)!=EOF)
{
ans=1e9;
dfs(x,);
printf("%d\n",ans);
}
return ;
}

CF440C的更多相关文章

随机推荐

  1. [CareerCup] 3.7 Adopt Animal 领养动物

    3.7 An animal shelter holds only dogs and cats, and operates on a strictly "first in, first out ...

  2. HBase入库调优

    本文章只针对“微型集群处理大数据”的场景. 场景描述: 硬件:5个节点,每个节点可用硬盘1块(700G.500G等).8核cpu,实验室环境(有时候还要跑其他程序跟你抢占资源),16G内存. 软件:h ...

  3. java中的自增问题

    运行下面这段代码,其结果是什么呢? package com.test; public class Inc { public static void main(String[] args) { Inc ...

  4. ModernUI教程:使用预定义的页面布局

    Modern UI for WPF自带了一组页面布局.Modern UI page是继承自control控件的,Page通过是通过ModernWindow.MenuLinkGroups属性来引用显示在 ...

  5. android之读取SD卡状态

    package xidian.dy.com.chujia; import android.os.Build; import android.os.Environment; import android ...

  6. jQuery基础之(五)jQuery自定义添加"$"与解决"$"的冲突

    1.自定义添加$ 从上面四篇文章我们看到jQuery的强大,但无论如何,jQuery都不可能满足所有用户的需求,而且有一些需求十分小众,也不适合放到整个jQuery框架中,正是因为这一点,jQuery ...

  7. Timer中schedule()的用法

    schedule的意思(时间表.进度表) timer.schedule(new TimerTask(){ void run()},0, 60*60*1000);timer.schedule(new M ...

  8. BACKBONE源代码解析

    //2014.11// Backbone.js 1.0.0 // (c) 2010-2013 Jeremy Ashkenas, DocumentCloud Inc. // Backbone may b ...

  9. hdu4081 次小生成树

    题意:有n个点,n-1条边.现在徐福可以让一条边无消耗建立,即魔法边.B表示除魔法边之外的的其他边的消耗值和,A表示这条魔法边相连的2个集合中都选一点,这两点的最大值,现在要求A/B最大. 方法:因为 ...

  10. Java基础-基本数据类型转换案例

    java基本数据类型八中 byte = Byte short = Short char = Character int = Integer long = Long float = Float doub ...