Fxx and game
可提交的传送门http://acm.hdu.edu.cn/showproblem.php?pid=5945
分析:这道题目可以采用动态规划来解决
设f[i]表示把i变成1的最小代价。
所以有:f[i] = min{f[(1-t) ~ (i-1)]}+1
特别的,对于i % k == 0 f[i] = min{f[i],f[i/k] + 1}
我们可以先忽略掉特判的一步,这样,f[i]就来自于f[(1-t) ~ (i-1)]之间的最小值了
我们发现这个问题转换成了RMQ问题,可以在logn内解决
可以用单调队列优化dp,这样可以做到复杂度O(n)
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
x=;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=*x+ch-'',ch=getchar(),ch>'!');if(flag) x=-x;
}
inline int cat_max(const int &a,const int &b){return a>b ? a:b;}
inline int cat_min(const int &a,const int &b){return a<b ? a:b;}
const int maxn = ;
const int inf = 0x3f3f3f3f;
int f[maxn],q[maxn],l,r,ans;
inline void work(){
int n,k,t;
read(n);read(k);read(t);
if(t == ){
for(ans=;n>;++ans) n /= k;
printf("%d\n",ans);
return;
}
l = r = ;f[] = ;
q[] = ;
for(int i=;i<=t+ && i <= n;++i) f[i] = ,q[++r] = i;
for(int i=t+;i<=n;++i){
if(i % k == && k != ) f[i] = f[i/k] + ;
else f[i] = inf;
while(l <= r && q[l] < (i - t)) ++l;
f[i] = cat_min(f[i],f[q[l]] + );
while(l <= r && f[q[r]] >= f[i]) --r;
q[++r] = i;
}
printf("%d\n",f[n]);
}
int main(){
int T;read(T);
while(T--) work();
getchar();getchar();
return ;
}
Fxx and game的更多相关文章
- HDU 5944 Fxx and string(暴力/枚举)
传送门 Fxx and string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Othe ...
- HDU 5945 / BestCoder Round #89 1002 Fxx and game 单调队列优化DP
Fxx and game 问题描述 青年理论计算机科学家Fxx给的学生设计了一款数字游戏. 一开始你将会得到一个数\:XX,每次游戏将给定两个参数\:k,tk,t, 任意时刻你可以对你的数执行下面 ...
- BestCoder Round #89 Fxx and string
问题描述 青年理论计算机科学家Fxx得到了一个只包含小写字母的字符串. 字符串的长度为\:nn,下标从1开始,第\:i\:i位的字母为\:s_isi,现在Fxx想知道有多少三元组\:(i,j,k ...
- hdu 5945 Fxx and game
青年理论计算机科学家Fxx给的学生设计了一款数字游戏. 一开始你将会得到一个数X,每次游戏将给定两个参数x,k,t, 任意时刻你可以对你的数执行下面两个步骤之一: .X=X−i(<=i<= ...
- hdu 5944 Fxx and string
\:nn,下标从1开始,第\:i\:i位的字母为\:s_isi,现在Fxx想知道有多少三元组\:(i,j,k)\:(i,j,k)满足下列条件 1.i,j,k\:i,j,k三个数成等比数列 2.s ...
- hdu 5945 Fxx and game(单调队列优化DP)
题目链接:hdu 5945 Fxx and game 题意: 让你从x走到1的位置,问你最小的步数,给你两种走的方式,1.如果k整除x,那么你可以从x走一步到k.2.你可以从x走到j,j+t<= ...
- HDU 5944 Fxx and string (暴力)
题意:给定一个字符串,问有多少个三元组满足 i, j, k组成一个等比数列,并且s[i] = 'y', s[j] = 'r', s[k] = 'x',且j/i ,j/k中至少一个是整数. 析:直接暴力 ...
- 【23.33%】【hdu 5945】Fxx and game
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submission(s ...
- hdu 5945 Fxx and game(dp+单调队列! bc#89)
Young theoretical computer scientist Fxx designed a game for his students. In each game, you will ge ...
- BestCoder Round #89 B题---Fxx and game(单调队列)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5945 问题描述 输入描述 输出描述 输入样例 输出样例 题意:中文题,不再赘述: 思路: B ...
随机推荐
- 使用putty与SSHSecureShellClient登录远程服务器完成与本地Git项目的同步
使用软件远程登录管理服务器 今天给大家介绍两款远程登录管理服务器的软件(Putty和SSHSecureShellClient),这两款也是我在工作中经常的软件. 使用 PuTTY 远程登录管理服务器 ...
- Cannot execute as the database principal because the principal "guest" does not exist, this type of principal cannot be impersonated, or you do not have permission.
今天遇到这样一个问题:一个系统的作业需要给系统Support人员开放SQL Agent下作业的查看.执行权限.数据库版本为SQL Server 2014 SP2,给这个系统Support人员的NT账号 ...
- 弹窗样式 idialog,purebox,artdialog4.1.2,jquery.alert.v1.2
<script src="<?php echo $imgurl;?>/tools/media/js/jquery.iDialog.js"></scri ...
- 实战搭建SVN代码版本服务器
前言:公司要求搭建一台SVN代码版本管理服务器,用于管理所有代码资产: 项目架构图 1.环境安装 [root@host_centos ~]#yum –y install subversion mod_ ...
- 使用Jackson解析Json示例
原文http://blog.csdn.net/gebitan505/article/details/17005735 custom.json: { "country":&q ...
- shell编程之正则表达式
什么是正则表达式?正则表达式是用于描述字符排列和匹配模式的一种语法规则.在很多程序设计语言中都支持利用正则表达式来进行字符串的操作,不同语言中的正则表达式略有不同,但是毕竟都是正则,其本质思想都是一致 ...
- selenium自动化-java-封断言类2
封装断言类 package streamax.com; import java.util.ArrayList; import java.util.List; import org.testng.Ass ...
- wireshark 分析重传包
如下图所示,经过实验,wireshark把第一次重传包分类为out of order 类型,可以通过tcp.analysis.out_of_order过滤,如果第二次重传,分类为fast retran ...
- intellij idea让资源文件自动更新
intellij idea默认文件是自动保存的,但是手头有个项目jsp文件改动后,在tomcat中不能立即响应变化.要jsp文件改动后立刻看到变化,有个配置.在idea tomcat 中server的 ...
- Oracle trunc()函数的用法
Oracle trunc()函数的用法 /**************日期********************/1.select trunc(sysdate) from dual --2013-0 ...