【巧妙算法系列】【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),同时本例与选择排序进行了比较,选择排序又叫直 ...
随机推荐
- JS 点击复制Copy插件--Zero Clipboard
写博客就是一周工作中遇到哪些问题,一个优点就是能够进行一个总结,另外一个优点就是下次遇到相同的问题即使那你记不住,也能够翻看你的博客攻克了.相同也能够帮到别人遇到与你一样问题的人.或者别人有比你更好的 ...
- Dalvik虚拟机的启动过程分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8885792 在Android系统中,应用程序进 ...
- EffectiveC#3--选择is或者as操作符而不是做强制类型转换
1.用as运算符进行类型转换.因为比起盲目的强制转换它更安全,而且在运行时效率更高. 安全体现在:as操作符就算是转化一个null的引用时,也会安全的返回一个null而不会像强制转换抛出异常. 2.a ...
- C++、GDAL创建shapefile,并向矢量文件中添加网格
//总体来说这个过程就是构建数据源->构建层->构建要素->构建形状->关闭数据源. //要包含的GDAL头文件 #include <gdal_priv.h> #i ...
- 解决mysql中文存储问题
在mysql安装目录下先找到my.ini,给[mysql]和[mysqld]下的default-character-set赋值为utf8 即改为:default-character-set=utf8 ...
- php常用的header头
<?php /** * php常用的header头设置... */ header('HTTP/1.1 200 OK'); // ok 正常访问 header('HTTP/1.1 404 Not ...
- 利用raspberry pi搭建typecho笔记(二) sqlite和typecho部署
sqlite概述 typecho可以支持MYSQL和Sqlite两种数据库,因为Sqlite更为轻量,并且不需要额外的进程,而是直接对数据库文件进行读取,所以配置相对于MySQL也更为简单,仅需指定数 ...
- 百度定位SDK:弥补Android基站WIFI定位缺失
http://tech.qq.com/a/20120524/000347.htm 如今,基于位置信息的移动应用越来越多,从餐饮.购物等本地生活服务,到定向广告的匹配.移动社交网络的构建,LBS类应用的 ...
- Keil C51 中指针的使用
指针是C语言中比较难的一个内容,Keil C51在指针方面有和标准C不一样的地方,今天看了一些资料学习了一下Keil C51 中指针的使用. keil51的指针,包含两种指针:普通指针,兼容标准C:内 ...
- QTabWidget and QTabBar.的文字的颜色设置,三种方法
see the code after subclassingTabWidget::TabWidget(QWidget *parent): QTabWidget(parent),mousePressFl ...