题意:给定一个 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. loadrunner 多用户并发操作解读

    假设存在: 数据:A.B.C 虚拟用户:Vuser1.Vuser2.Vuser3 脚本中参数出现三次,脚本迭代三次 怎样取下一行数据? Sequential:顺序,所有虚拟用户按照顺序读取数据表 Ra ...

  2. how-do-i-access-windows-event-viewer-log-data-from-java

    https://stackoverflow.com/questions/310355/how-do-i-access-windows-event-viewer-log-data-from-java

  3. C. Wilbur and Points---cf596C

    http://codeforces.com/problemset/problem/596/C 题目大意:  给你n个数对  确保如果(x,y)存在这个集合  那么  0<=x'<=x &a ...

  4. P1359 租用游艇 洛谷

    https://www.luogu.org/problem/show?pid=1359 题目描述 长江游艇俱乐部在长江上设置了n 个游艇出租站1,2,…,n.游客可在这些游艇出租站租用游艇,并在下游的 ...

  5. [Bzoj4817] [Sdoi2017]树点涂色 (LCT神题)

    4817: [Sdoi2017]树点涂色 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 629  Solved: 371[Submit][Status ...

  6. 44444444444444444444444444444444dddddddddd66666666666666666666666666

    dddddddddddddddddddddddddddddddddddddddddddddddddddd

  7. Win7 丢失MSVCR110.DLL的解决办法

    1 从下面的网站下载dll文件 http://www.ddooo.com/softdown/27034.htm   2 把该文件放到C:\Windows\SysWOW64目录下(64位系统)或者C:\ ...

  8. NODE安装N管理出错

    Windows不支持,反正我在Linux上用过,从此脱离Win坑!

  9. NYOJ 158 省赛来了

    省赛来了 时间限制:1000 ms  |  内存限制:65535 KB 难度: 描写叙述 一年一度的河南省程序设计大赛又要来了. 竞赛是要组队的,组队形式:三人为一队,设队长一名.队员两名. 如今问题 ...

  10. POJ训练计划2299_Ultra-QuickSort(线段树/单点更新)

    解题报告 题意: 求逆序数. 思路: 线段树离散化处理. #include <algorithm> #include <iostream> #include <cstri ...