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-history-ps1-1

    1.串行端口终端(/dev/ttySn) 串行端口终端(Serial Port Terminal)是使用计算机串行端口连接的终端设备.计算机把每个串行端口都看作是一个字符设备.有段时间这些串行端口设备 ...

  2. 在线前端 JS 或 HTML 或 CSS 编写 Demo 处 JSbin 与 jsFiddle 比较

    JSBin 该编辑器的特点是编写可直接编写 HTML.CSS.JavaScript 并且可以在 output 中实时观察编写效果:可设置自动运行 JavasScript 代码,其中最大的好处是有一个 ...

  3. windows与ubuntu双系统的安装

    将ubuntu镜像烧录至U盘,从U盘启动电脑 选择自定义安装,不要选择它本身的双系统选项. 我的方案分区: 1. 挂载点/:主分区:安装系统和软件:大小为30G:分区格式为ext4:2. 挂载点/ho ...

  4. Windows Server 部署WEB API时内部错误

    Windows Server 部署WEB API时,发生HTTP 错误 500.21 - Internal Server Error,如图所示: 错误原因:IIS注册Framework4.0 解决方法 ...

  5. [CSP-S模拟测试]:数列(数学)

    题目传送门(内部题95) 输入格式 第一行三个整数$n,a,b$,第二行$n$个整数$x_1\sim x_n$表示数列. 输出格式 一行一个整数表示答案.无解输出$-1$. 样例 样例输入:2 2 3 ...

  6. [LeetCode]-DataBase-Combine Two Tables

    Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId ...

  7. centos7安装nvidia驱动

    1. disable UEFI security boot! 2.添加 ELRepo 源: Import the public key: rpm --import https://www.elrepo ...

  8. nodejs 中 接受前端的数据请求的处理

    前台 ---->  后台 后台要接受 前台的数据,只能通过 http 但是 前台接受 后台的数据有  from   ajax    jsonp nodejs 给我们提供了模块 url 模块,可以 ...

  9. centos7 升级php版本

    centos7 默认PHP5.4,版本太低,很多要求至少PHP5.5 1.查看已经安装的PHP组件 yum list installed| grep php php.x86_64 -.el7 @bas ...

  10. mysq远程连接报错,host..

    在本机登入mysql后,更改"mysql"数据库里的"user"表里的"host"项,从"localhost"改为'%' ...