【巧妙算法系列】【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),同时本例与选择排序进行了比较,选择排序又叫直 ...
随机推荐
- 本地化下按首字母分组排序的神器——UILocalizedIndexedCollation
最近在整一个很简单的通讯录相关的项目,通讯录当然就少不了按首字母或者汉字拼音首字母分组排序索引.因为按照我一贯的的做法,都是想要做成更通用的.支持本地化的,所以这就纠结了,世界各地的语言啊我去,我顶多 ...
- html5 video播放不全屏
<video controls="controls" webkit-playsinline src="${page.videoUrl }" type=&q ...
- SQLLoader1(简单测试,以控制文件方式导入数据)
1.创建表:SQL> conn scott/tiger@orcl;已连接. SQL> CREATE TABLE EMP2 AS SELECT * FROM EMP WHERE 1=2; 表 ...
- avalon2学习心得(1)
github上,avalon2的项目描述是这样的:“avalon2是一款基于虚拟DOM与属性劫持的 迷你. 易用. 高性能 的 前端MVVM框架, 适用于各种场景, 兼容各种古老刁钻浏览器, 吸收最新 ...
- iOS-OC-基础-NSNumber常用方法
/*===========================NSNumber数值对象=========================*/ // 将基本数据类型保存为NSNumber 对象类型 NSNu ...
- 1.iOS第一个简单APP
大纲: iOS系统发展 UI和OC 简单的APP程序 程序的生命周期 1.iOS的系统发展 从1983年OC程序开始发展到2015年,30多年的时间,但这依然不是一个十分完善的语言,可以说现在都没 ...
- php中mail()改用msmtp发送邮件
php中的mail()函数默认调用的是sendmail,这次我将其换成了轻量级的msmtp.在次过程中涉及到:修改配置文件php.ini,创建配置文件msmtprc或(.msmtprc)并修改其权限. ...
- No.1_1 java语言基础_学习笔记
import java.util.Scanner; public class HelloWorld { static final double PI=3.14; static int x=125; / ...
- solr源码导入eclipse
转载自:http://blog.csdn.net/vltic/article/details/19917377 (1)相应的开发环境准备 (1)jdk1.6+的安装和环境变量配置(命 ...
- JQuery阻止表单提交的方法总结 - 使用onsubmit()验证表单并阻止非法提交
方法1:<form onsubmit="javascript:confirm()"> 方法内返回false阻止表单提交 示例:代码检测textarea内填写的长度,未填 ...