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. 使用 CSS 显示 XML

    通过使用 CSS,可为 XML 文档添加显示信息. 使用 CSS 显示您的 XML? 使用 CSS 来格式化 XML 文档是有可能的. 下面的例子就是关于如何使用 CSS 样式表来格式化 XML 文档 ...

  2. linux下vsftpd的安装及配置使用详细步骤(推荐)

    vsftpd 是“very secure FTP daemon”的缩写,安全性是它的一个最大的特点. vsftpd 是一个 UNIX 类操作系统上运行的服务器的名字,它可以运行在诸如 Linux.BS ...

  3. csc.exeCPU100%

    可以通过nuget把Microsoft.CodeDom.Providers.DotNetCompilerPlatform和Microsoft.Net.Compilers这两个包卸载

  4. Chrome 66 新增异步剪贴板 API

    在过去的几年里我们只能使用 document.execCommand 来操作剪贴板.不过,这种操作剪贴板的操作是同步的,并且只能读取和写入 DOM. 现在 Chrome 66 已经支持了新的 Asyn ...

  5. mysql 数据增删改的总结

    一.在MySQL管理软件中,可以通过SQL语句中的DML语言来实现数据的操作,包括 1.使用INSERT实现数据的插入2.UPDATE实现数据的更新3.使用DELETE实现数据的删除4.使用SELEC ...

  6. MySQL的内连接,左连接,右连接,全连接

    内连接(INNER JOIN)(典型的连接运算,使用像   =   或   <>   之类的比较运算符).包括相等连接和自然连接. 内连接使用比较运算符根据每个表共有的列的值匹配两个表中的 ...

  7. jQuery-validate插件初级篇

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  8. SpringBoot 优雅的整合 Shiro

    Apache Shiro是一个功能强大且易于使用的Java安全框架,可执行身份验证,授权,加密和会话管理.借助Shiro易于理解的API,您可以快速轻松地保护任何应用程序 - 从最小的移动应用程序到最 ...

  9. @RequestMapping 和@ResponseBody 和 @RequestBody和@PathVariable 注解 注解用法

    接下来讲解一下 @RequestMapping  和@ResponseBody 和 @RequestBody和@PathVariable 注解 注解用法 @RequestMapping 为url映射路 ...

  10. ssm的自动类型转换器

    1.jsp页面将String 转换成employee类型 <form action="testConversionServiceConverer" method=" ...