1604: Operations

时间限制: 2 Sec  内存限制: 128 MB

提交: 313  解决: 97

[提交][状态][讨论版]

题目描述

You can perform the following operations.

  • i = i+1;
  • i = i-1;
  • i = i/2 if i is even

The number i is integer. Find the minimal number of operations to get 0.

输入

Each line contains an N,  1 ≤ N ≤ 2000000。 Process to end of file.

输出

For each case, print one line containing an integer which is the minimal number of operations from N to 0.

样例输入

41559

样例输出

369

提示

来源

2014年湖南科技大学校赛

这题一开始想的是BFS暴力搜索,试了一下2000000,感觉还不错,结果一提交TLE,然后想到打表,结果codeblocks打崩溃了表也没打完,估计打完了也会超过代码长度无法提交,这个时候我意识到了问题的不简单,之后发现这可能是一道贪心题,如果是偶数,为了让他变得更小,一定要除2,如果是奇数,可能要加一或者减一,问题就在这,究竟是加一之后不断初二还是减一之后不断除二,这里打表,判断是加一之后更小还是减一之后更小,之后特判一下1就可以了;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int N = 2000000+5;
int a[N];
void Init(){
    memset(a,0,sizeof(a));
    for(int i=2;i<N;i+=2){
        if(i%2==0){
            int tmp = i;
            while(true){
                tmp/=2;
                if(tmp%2) break;
            }
            a[i] = tmp;
        }
    }
}
 
int main(){
    Init();
    int n;
    while(scanf("%d",&n)==1){
        int cnt = 0;
        while(true){
            if(n==1){cnt++;break;}
            if(n%2==0){n/=2;cnt++;}
            else{
                n = a[n-1]>a[n+1]?n+1:n-1;
                cnt++;
            }
        }
        printf("%d\n",cnt);
    }
    return 0;
}

HNUSTOJ 1604:Operations的更多相关文章

  1. backup, file manipulation operations (such as ALTER DATABASE ADD FILE) and encryption changes on a database must be serialized.

    昨天在检查YourSQLDba备份时,发现有台数据库做备份时出现了下面错误信息,如下所示: <Exec>   <ctx>yMaint.ShrinkLog</ctx> ...

  2. HDU 5938 Four Operations(四则运算)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  3. ios基础篇(二十九)—— 多线程(Thread、Cocoa operations和GCD)

    一.进程与线程 1.进程 进程是指在系统中正在运行的一个应用程序,每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内: 如果我们把CPU比作一个工厂,那么进程就好比工厂的车间,一个工厂有 ...

  4. OpenCascade Modeling Algorithms Boolean Operations

    Modeling Algorithms Boolean Operations of Opencascade eryar@163.com 布尔操作(Boolean Operations)是通过两个形状( ...

  5. A.Kaw矩阵代数初步学习笔记 4. Unary Matrix Operations

    “矩阵代数初步”(Introduction to MATRIX ALGEBRA)课程由Prof. A.K.Kaw(University of South Florida)设计并讲授. PDF格式学习笔 ...

  6. A.Kaw矩阵代数初步学习笔记 3. Binary Matrix Operations

    “矩阵代数初步”(Introduction to MATRIX ALGEBRA)课程由Prof. A.K.Kaw(University of South Florida)设计并讲授. PDF格式学习笔 ...

  7. mouse scrollings and zooming operations in linux & windows are opposite

    mouse scrollings and zooming operations in linux & windows are opposite. windows中, 鼠标滚动的方向是: 查看页 ...

  8. MongoDB—— 写操作 Core MongoDB Operations (CRUD)

    MongoDB使用BSON文件存储在collection中,本文主要介绍MongoDB中的写操作和优化策略. 主要有三种写操作:        Create        Update        ...

  9. MongoDB—— 读操作 Core MongoDB Operations (CRUD)

    本文主要介绍内容:从MongoDB中请求数据的不同的方法 Note:All of the examples in this document use the mongo shell interface ...

随机推荐

  1. linux-ntp-10

    Unix/linux类:ntp.aliyun.com,ntp1-7.aliyun.com windows类: time.pool.aliyun.com s1a.time.edu.cn 北京邮电大学 s ...

  2. 这个 'ip' 竟然把我搞蒙圈了……

    1.查看redis连接配置(某项目) <bean id="redisCacheServer" class="com.spring.cache.RedisCacheS ...

  3. Selenium 多表单(frame/iframe)切换

    frame标签有frameset.frame.iframe三种,frameset跟其他普通标签没有区别,不会影响到正常的定位,而frame与iframe需要切换进去才能定位到其中的元素 比如下面这个网 ...

  4. Codeforces Round #403---C题(DFS,树)

    C. Andryusha and Colored Balloons time limit per test 2 seconds memory limit per test 256 megabytes ...

  5. centos-系统删除多余网卡的方法

    一.删除系统中中多余的ifcfg-eth0.bak Centos系统更改网卡或网卡MAC地址后会出现个eth0.bak配置备份文件解决方法:/etc/sysconfig/networking/devi ...

  6. DVWA--XSS(stored)

    XSS 0X01 1.简介 跨站脚本(cross site script)为了避免与样式css混淆,所以简称为XSS. XSS是一种经常出现在web应用中的计算机安全漏洞,也是web中最主流的攻击方式 ...

  7. vue树形菜单

    vue树形菜单 由于项目原因,没有使用ui框架上的树形菜单,所以自己动手并参考大佬的代码写了一个树形菜单的组件,话不多说,直接上代码.html代码js代码直接调用api 把请求到的数据直接赋值给per ...

  8. Vue学习(一) :入门案例

    1. 开始前的准备 IDE:VSCode(推荐)或者Sublime Text 前导技术:JavaScript中级 2. 官方提供的product例程 product.html页面代码: <div ...

  9. 屏蔽ffmpeg命令的所有提示

    有时候需要隐蔽的执行ffmpeg不希望输出任何日志,提示.这个时候只需要多添加这个参数即可 -loglevel quiet

  10. 修改win10 capslock键成esc键 vim

    桌面编辑一个文件CapsLock2Esc.reg Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentCont ...