题意:给定一个 x, k, t,你有两种操作,一种是 x - i (0 <= i <= t),另一种是 x / k  (x % k == 0)。问你把x变成1需要的最少操作。

析:这肯定是DP,也想到可能是单调队列,但是不会啊。。。。就是胡搞了一发,虽然AC了,但是效率极低,比用单调队列少10倍。

dp[i] 表示把 i 变成 1,要用的最少步骤,然后每次取最优。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e6 + 5;
const LL mod = 10000000000007;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const int hr[]= {-2, -2, -1, -1, 1, 1, 2, 2};
const int hc[]= {-1, 1, -2, 2, -2, 2, -1, 1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int dp[maxn];
int q[maxn]; int solve(int k, int x){
int cnt = 0;
while(x){
x /= k;
++cnt;
}
return cnt-1;
} int main(){
int T; cin >> T;
while(T--){
int x, k, t;
scanf("%d %d %d", &x, &k, &t);
if(k == 1){ printf("%d\n", (x-1)%t == 0 ? (x-1)/t : (x-1)/t+1); continue; }
else if(!t){ printf("%d\n", solve(k, x)); continue; }
dp[1] = 0; q[1] = 1;
int l = 1, r = 1;
for(int i = 2; i <= x; ++i){
while(q[l] < i-t) ++l;
dp[i] = dp[q[l]] + 1;
if(i % k == 0) dp[i] = Min(dp[i], dp[i/k]+1);
while(l <= r && dp[q[r]] >= dp[i]) --r;
q[++r] = i;
}
printf("%d\n", dp[x]);
}
return 0;
}

HDU 5945 Fxx and game (DP+单调队列)的更多相关文章

  1. hdu 5945 Fxx and game(单调队列优化DP)

    题目链接:hdu 5945 Fxx and game 题意: 让你从x走到1的位置,问你最小的步数,给你两种走的方式,1.如果k整除x,那么你可以从x走一步到k.2.你可以从x走到j,j+t<= ...

  2. [poj3017] Cut the Sequence (DP + 单调队列优化 + 平衡树优化)

    DP + 单调队列优化 + 平衡树 好题 Description Given an integer sequence { an } of length N, you are to cut the se ...

  3. DP+单调队列 codevs 1748 瑰丽华尔兹(还不是很懂具体的代码实现)

    codevs 1748 瑰丽华尔兹 2005年NOI全国竞赛  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解       题目描述 Descripti ...

  4. hdu 3706 Second My Problem First 单调队列

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3706 Second My Problem First Time Limit: 12000/4000 M ...

  5. 习题:烽火传递(DP+单调队列)

    烽火传递[题目描述]烽火台又称烽燧,是重要的防御设施,一般建在险要处或交通要道上.一旦有敌情发生,白天燃烧柴草,通过浓烟表达信息:夜晚燃烧干柴,以火光传递军情.在某两座城市之间有n个烽火台,每个烽火台 ...

  6. (noip模拟二十一)【BZOJ2500】幸福的道路-树形DP+单调队列

    Description 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一同晨练来享受在一起的时光. 他们画出了晨练路线的草图,眼尖的小T发现可以用树来描绘这个草图. ...

  7. 3622 假期(DP+单调队列优化)

    3622 假期 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 黄金 Gold 题目描述 Description 经过几个月辛勤的工作,FJ决定让奶牛放假.假期可以在1-N天内任意选择 ...

  8. 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 ...

  9. HDU 5945 题解(DP)(单调队列)

    题面: Fxx and game Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) T ...

随机推荐

  1. 库操作&表操作

    系统数据库 ps:系统数据库: mysql 授权库,主要存储系统用户的 权限信息 test MySQL数据库系统自动创建的 测试数据库 ination_schema 虚拟库,不占用磁盘空间,存储的是数 ...

  2. codevs——2822 爱在心中

    2822 爱在心中  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description “每个人都拥有一个梦,即使彼此不相 ...

  3. 利用Cufon技术渲染文字的简单示例

    Cufon是一种能够根据指定的字体渲染文字的技术.今天试用了下,主要有几个步骤: 1.下载Cufon.js(http://cufon.shoqolate.com/generate/) 2.获取需要渲染 ...

  4. 通过XMLHttpRequest,ActiveXObject实现ajax请求

    今天学习了原生的ajax请求.我将涉及到的ajax请求方法封装成了一个对象: var xhr={     getXHR:function(){         var XHR = null;      ...

  5. nginx.org与nginx.com有什么区别(Nginx与Nginx Plus)

    nginx.org是开源社区,管理着Nginx开源版. nginx.xom是商业社区,管理着Nginx Plus商业版. 商业版和开源版是有很大区别的,服务还加更强大的功能,当然依托开源社区加载第三方 ...

  6. Spring MVC中 log4j日志文件配置相对路径

    log4j和web.xml配置webAppRootKey 的问题 1 在web.xml配置 <context-param>  <param-name>webAppRootKey ...

  7. Jinja2如何默认将None 值显示为空字符串?

    在Jinja模板中 {% if User %} {{ User.name }} {% endif %} 可以简化为下面的写法,同时,保证返回值为空,而不是显示为“None” {{ User.name ...

  8. Google C++ style guide——C++类

    1.构造函数的职责 构造函数中仅仅进行那些没有实际意义的初始化.由于成员变量的"有意义"的值大多不在构造函数中确定. 能够的话,使用Init()方法集中初始化为有意义的数据. 长处 ...

  9. Linux命令chattr和lsattr

    先看字面解释: chattr:chattr - change file attributes on a Linux file system lsattr - list file attributes ...

  10. 安装软件:/lib/ld-linux.so.2: bad ELF interpreter解决

    http://linux.chinaitlab.com/set/928509.html 我们在CentOS系统中安装软件:/lib/ld-linux.so.2: bad ELF interpreter ...