UVA.11384 Help is needed for Dexter (思维题)
UVA.11384 Help is needed for Dexter (思维题)
题意分析
同样水题一道,这回思路对了。
给出数字n,面对一个1,2,3,4……n的数字序列,你可以对他们的部分或者全部减去一个相同数字,最后使得这个序列变为全0的序列,求这样操作的次数最小值。
一开始着手想的是两两分组,既然能两两分组,为什么不三三分组呢?既然能三三分组,为什么不能四四分组呢?不难联想到二分法,即折半分组,看如下的例子:
n = 9
1,2,3,4,5,6,7,8,9
折半后
1,2,3,4
5,6,7,8,9
使5到9均减5,便得到
1,2,3,4
0,1,2,3,4
好了就有2个1234的序列了,再次折半,变成:
1,2,1,2
3,4,3,4(那个0已经省略了),然后对下面的序列全部减2,就会得到。
1,2,1,2
1,2,1,2
然后再折半,把所有的2全部减1,即得到:
1,1,1,1,1,1,1,1
最后一步,所有的1都减去1,变成0了。
根据上述的思路,可以看出,每一次折半,都要进行一次减法操作,那么我们不难想到这样一个算法:每次都除2,除2的同时计数器+1,知道算到1为止。
代码总览
#include <iostream>
#include <cstdio>
using namespace std;
int f(int n)
{
return n == 1 ? 1 :f(n/2) +1;
}
int main()
{
int n;
while(scanf("%d",&n)!= EOF){
int ans = f(n);
printf("%d\n",ans);
}
return 0;
}
UVA.11384 Help is needed for Dexter (思维题)的更多相关文章
- UVA 11384 Help is needed for Dexter(问题转化 递归)
Help is needed for Dexter Time Limit: 3 Second Dexter is tired of Dee Dee. So he decided to keep Dee ...
- UVa 11384 - Help is needed for Dexter 分析, 树状数组 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- UVa 11384 Help is needed for Dexter
分析题目以后得出,对于一个连续等差递增的序列,例如1,2,3,4,5,6,每次选择其中后一半,减去能减的最大数,则是最优操作. 上述序列经过一次操作后变为1,2,3,0,1,2,此时可抛弃后一半(已经 ...
- UVa 11384 Help is needed for Dexter 正整数序列
给定一个正整数 n ,你的任务使用最少的操作次数把序列 1, 2, 3, -- , n 中的所有数都变成 0 .每次操作可以从序列中选择一个或者多个数,同时减去一个相同的正整数.比如,1, 2, 3 ...
- UVa 11384 Help is needed for Dexter (递归)
题意:给定一个n表示1到n的序列,让你用最小的步数把这个序列都变为0,每个操作可以从序列中选择一个或多个个,同时减掉一个正整数,求最少的步数. 析:一看这个题,感觉挺高深的,但是静下心来想想,其实挺简 ...
- UVA 11384 Help is needed for Dexter(递归)
题目链接:https://vjudge.net/problem/UVA-11384 这道题要分析得透: 如果我们手模的话,会发现:如果先将大于$\frac{n}{2}$的数都减去$\frac{n}{2 ...
- 【巧妙算法系列】【UVA 11384】 Help is needed for Dexter 正整数序列
Help is needed for Dexter Time Limit: 3 Second Dexter is tired of Dee Dee. So he decided to keep Dee ...
- uva------Help is needed for Dexter(11384)
Problem H Help is needed for Dexter Time Limit: 3 Second Dexter is tired of Dee Dee. So he decided t ...
- UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)
UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...
随机推荐
- 一对多,多的逗号分隔存在新字段中(Group_concat 用法)
sql 语句: SELECT ( SELECT Group_concat(t_work_group_user.user_id) FROM ...
- Putty远程连接Ubuntu14.04
步骤一.在ubuntu系统中安装ssh,可使用如下的命令进行安装: sudo apt-get install openssh-server 步骤二.为了保险起见,安装完成后重启一下ssh服务,命令如下 ...
- 感觉总结了一切python常见知识点,可直接运行版本
#encoding=utf-8#http://python.jobbole.com/85231/#作用域a=1def A(a): a=2 print 'A:',a def B(): print 'B: ...
- JVM之G1收集器
Garbage-First,面向服务端的垃圾收集器. 并行与并发:充分利用多核环境减少停顿时间, 分代收集:不需要配合其它收集器 空间整合:整体上看属于标记整理算法,局部(region之间)数据复制算 ...
- laravel 的DB::raw() 语法使用
z之前在项目中遇到一个问题,复杂的sql查询,用laravel的查询构造器,非常的不方便,各种查询条件拼接一长串拼得脑瓜疼:然后想使用原生的sql语句来查询,然后又使用不了laravel的pagina ...
- leetcode-前K个高频元素
给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [1,2] 示例 2: 输入: nums = [1], ...
- 166. Nth to Last Node in List
Description Find the nth to last element of a singly linked list. The minimum number of nodes in lis ...
- javascript对table的添加,删除行的操作
<body> <form name="myForm"> <table width="100%" id="tab" ...
- 2.安装hdfs yarn
下载hadoop压缩包设置hadoop环境变量设置hdfs环境变量设置yarn环境变量设置mapreduce环境变量修改hadoop配置设置core-site.xml设置hdfs-site.xml设置 ...
- HTML5form表单的相关知识总结
首先在介绍HTML5form表单的新增内容之前,我总结了一下HTML的form表单的内容. <!DOCTYPE html> <html lang="en"> ...