【巧妙算法系列】【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 Dee busy in a game. The game he planned for her is quite easy to play but not easy to win at least not for Dee Dee. But Dexter does not have time to spend on this silly task, so he wants your help.
There will be a button, when it will be pushed a random number N will be chosen by computer. Then on screen there will be numbers from 1 to N. Dee Dee can choose any number of numbers from the numbers on the screen, and then she will command computer to
subtract a positive number chosen by her (not necessarily on screen) from the selected numbers. Her objective will be to make all the numbers 0.
For example if N = 3, then on screen there will be 3 numbers on screen: 1, 2, 3. Say she now selects 1 and 2. Commands to subtract 1, then the numbers on the screen will be: 0, 1, 3. Then she selects 1 and 3 and commands to subtract 1. Now the numbers are
0, 0, 2. Now she subtracts 2 from 2 and all the numbers become 0.
Dexter is not so dumb to understand that this can be done very easily, so to make a twist he will give a limit L for each N and surely L will be as minimum as possible so that it is still possible to win within L moves. But Dexter does not have time to think
how to determine L for each N, so he asks you to write a code which will take N as input and give L as output.
Input and Output:
Input consists of several lines each with N such that 1 ≤ N ≤ 1,000,000,000. Input will be terminated by end of file. For each N output L in separate lines.
SAMPLE INPUT |
OUTPUT FOR SAMPLE INPUT |
1 2 3 |
1 2 2 |
思路:
很容易发现
切掉一块后 再切掉的两边形成两个子问题
将接下来的两个子问题解决的步数 取决于值较大哪一方
显然 因此我们若切一刀 使两个子问题平均点的话 无疑是最优方案
书上答案:
拿到这道题目之后,最好的方式是自己试一试。经过若干次尝试和总结后,不难发现第一步的最好方式
换句话说,当n=6的时候留下1, 2, 3,而把4,5,6同时减去min{4,5,6}=4得到序列1, 2, 3, 0, 1, 2,它等价于1, 2, 3(想一想,为什么)。换句话说,f(6)=f(3)+1。
一般地,为了平衡,我们保留1~n/2,把剩下的数同时减去n/2+1,得到序列1, 2, …,n/2, 0,1,…,(n-1)/2,它等价于1, 2, …,n/2,因此f(n)=f(n/2)+1。边界是f(1)=1。
代码:
#include<stdio.h>
int getans(int n)
{
if(n==0) return 0;
if(n==1) return 1;
else return getans(n>>1)+1;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
printf("%d\n",getans(n));
}
return 0;
}
【巧妙算法系列】【UVA 11384】 Help is needed for Dexter 正整数序列的更多相关文章
- UVa 11384 Help is needed for Dexter 正整数序列
给定一个正整数 n ,你的任务使用最少的操作次数把序列 1, 2, 3, -- , n 中的所有数都变成 0 .每次操作可以从序列中选择一个或者多个数,同时减去一个相同的正整数.比如,1, 2, 3 ...
- UVA.11384 Help is needed for Dexter (思维题)
UVA.11384 Help is needed for Dexter (思维题) 题意分析 同样水题一道,这回思路对了. 给出数字n,面对一个1,2,3,4--n的数字序列,你可以对他们的部分或者全 ...
- 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到n的序列,让你用最小的步数把这个序列都变为0,每个操作可以从序列中选择一个或多个个,同时减掉一个正整数,求最少的步数. 析:一看这个题,感觉挺高深的,但是静下心来想想,其实挺简 ...
- UVA 11384 Help is needed for Dexter(递归)
题目链接:https://vjudge.net/problem/UVA-11384 这道题要分析得透: 如果我们手模的话,会发现:如果先将大于$\frac{n}{2}$的数都减去$\frac{n}{2 ...
- 【巧妙算法系列】【Uva 11464】 - Even Parity 偶数矩阵
偶数矩阵(Even Parity, UVa 11464) 给你一个n×n的01矩阵(每个元素非0即1),你的任务是把尽量少的0变成1,使得每个元素的上.下.左.右的元素(如果存在的话)之和均为偶数.比 ...
- JAVA算法系列 冒泡排序
java算法系列之排序 手写冒泡 冒泡算是最基础的一个排序算法,简单的可以理解为,每一趟都拿i与i+1进行比较,两个for循环,时间复杂度为 O(n^2),同时本例与选择排序进行了比较,选择排序又叫直 ...
随机推荐
- LDA-线性判别分析(三)
本来是要调研 Latent Dirichlet Allocation 的那个 LDA 的, 没想到查到很多关于 Linear Discriminant Analysis 这个 LDA 的资料.初步看了 ...
- Linux curses库使用
相信您在网路上一定用过如 tin,elm 等工具, 这些软体有项共同的特色, 即他们能利用上下左右等方向键来控制游标的位置. 除此之外, 这些程式的画面也较为美观. 对Programming 有兴趣 ...
- Juqery 中使用 ajax
从 test.js 载入 JSON 数据,附加参数,显示 JSON 数据中一个 name 字段数据. jQuery 代码: $.getJSON("test.js", { name: ...
- 《JavaScript 闯关记》之原型及原型链
原型链是一种机制,指的是 JavaScript 每个对象都有一个内置的 __proto__ 属性指向创建它的构造函数的 prototype(原型)属性.原型链的作用是为了实现对象的继承,要理解原型链, ...
- ajax局部刷新分页
//请求数据加载绑定页面 function DindAjax(pageIndex) {//获取参数 var colors = $("#colorsVal").val(); $.aj ...
- Linux中yum的安装
下载安装yum: wget http://yum.baseurl.org/download/3.2/yum-3.2.28.tar.gz .tar.gz cd touch /etc/yum.conf c ...
- OC——UIlabel text的常规应用
UILabel *downloader = [[UILabel alloc]init]; NSString *downloadCount = [[LibraryArr objectAtIndex:in ...
- centos源码安装git
因为Centos上yum安装的话可能版本比较低,使用中会有一些难以预料的问题出现. 从源代码编译安装方法: #Centos执行: yum install curl-devel expat-devel ...
- linux crontab运行
Linux在相应用户下,用crontab -l 命令可以查看该用户定时执行的任务. 1- $>crontab -l 无内容. 则表示没有指定用户执行对应用户下的crontab文件. 2- $&g ...
- [C++程序设计]对“&”和“*”运算符
对“&”和“*”运算符再做些说明:(1) 如果已执行了“pointer_1=&a;”语句,请问&*pointer_1的含义是什么?“&”和“*”两个运算符的优先级别相同 ...