dfs你怕是要爆炸

考虑dp;

很容易想到 dp[ i ] 表示到 i 时的最少转移步数;

那么: dp[ i ]= min( dp[ i ],dp[ i-j ]+1 );

其中 i-t<=j<=i;

当 i%k==0时 ,dp[ i ]=min( dp[ i ],dp[ i/k ]+1 );

很明显这种要T到飞起;

我们要优化dp;

1e6的数据考虑O(n)级别的;

队列优化:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 2000005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-4
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ int dp[maxn];
int q[maxn]; int main() {
// ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int T; cin >> T;
while (T--) {
ms(dp);
int n, k, t; cin >> n >> k >> t;
int l = 1, r = 1;
dp[1] = 0;
q[r++] = 1;
for (int i = 2; i <= n; i++) {
dp[i] = inf;
while (l < r&&q[l] < i - t)l++;
if (l < r)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-1]] >= dp[i])r--;
q[r++] = i;
}
cout << dp[n] << endl;
}
return 0;
}

Fxx and game hdu 5945 单调队列dp的更多相关文章

  1. HDU 3507 单调队列 斜率优化

    斜率优化的模板题 给出n个数以及M,你可以将这些数划分成几个区间,每个区间的值是里面数的和的平方+M,问所有区间值总和最小是多少. 如果不考虑平方,那么我们显然可以使用队列维护单调性,优化DP的线性方 ...

  2. POJ 3017 单调队列dp

    Cut the Sequence Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8764   Accepted: 2576 ...

  3. [TyvjP1313] [NOIP2010初赛]烽火传递(单调队列 + DP)

    传送门 就是个单调队列+DP嘛. ——代码 #include <cstdio> ; , t = , ans = ~( << ); int q[MAXN], a[MAXN], f ...

  4. zstu 4237 马里奥的求救——(单调队列DP)

    题目链接:http://oj.acm.zstu.edu.cn/JudgeOnline/problem.php?id=4237 这题可以转化为每次可以走g~d+x步,求最大分数,且最大分数的步数最少. ...

  5. hdu 3530 单调队列最值

    /** HDU 3530 单调队列的应用 题意: 给定一段序列,求出最长的一段子序列使得该子序列中最大最小只差x满足m<=x<=k. 解题思路: 建立两个单调队列分别递增和递减维护(头尾删 ...

  6. 1304F2 - Animal Observation (hard version) 线段树or单调队列 +DP

    1304F2 - Animal Observation (hard version) 线段树or单调队列 +DP 题意 用摄像机观察动物,有两个摄像机,一个可以放在奇数天,一个可以放在偶数天.摄像机在 ...

  7. HDU 5945 维护一个单调队列 dp

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

  8. hdu 3401 单调队列优化DP

    Trade Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status ...

  9. hdu 3401 单调队列优化+dp

    http://acm.hdu.edu.cn/showproblem.php?pid=3401 Trade Time Limit: 2000/1000 MS (Java/Others)    Memor ...

随机推荐

  1. CIA泄露资料分析(黑客工具&技术)—Windows篇

    背景 近期,维基解密曝光了一系列据称来自美国中央情报局(CIA)网络攻击活动的秘密文件,代号为“Vault 7”,被泄露文件的第一部分名为“Year Zero”,共有8761个文件,包含7818个网页 ...

  2. 部署和调优 1.3 pureftp部署和优化-1

    FTP 是 File Transfe Protocol(文件传输协议)的英文简称,而中文简称为 “文传协议” 用于 Internet 上的控制件的双向传输. 可以访问    www.pureftpd. ...

  3. C#高级参数out的使用

    C#中有三个高级参数,分别是out,ref,params.本文章中先来介绍out参数的使用. out,用于在方法中返回多余值.(可以理解为让一个方法返回不同的类型值) 我们通过例子来理解例子的功能:用 ...

  4. eclipse安卓模拟器Failed to install on device 'emulator-5554': timeout处理方案

    我们在用模拟器调试的时候,经常会出现Failed to install on device 'emulator-5554': timeout这个错误.其实就是有些虚拟器在部署的时候时间过于长.系统就认 ...

  5. 【坑】idea+tomcat

    idea用tomcat外置容器部署debug要清掉webapp里面的同名项目,否则就呵呵呵呵了

  6. MySQL建立一个连接工具类

    public class DBUtil { public static Connection getConn() { Connection conn = null; try { Class.forNa ...

  7. 基于ActiveMQ的Topic的数据同步——初步实现

    一.背景介绍 公司自成立以来,一直以做项目为主,算是经累经验吧,自去年以来,我们部门准备将以前的项目做成产品,大概细分了几个小的产品,部们下面又分了几个团队,分别负责产品的研发,而我们属于平台团队,负 ...

  8. windows python-mysql 安装

    https://www.cnblogs.com/gbx-bo/p/5993190.html

  9. css代码结构

    整个文档结构如下: 一般性样式 主体样式 reset样式 链接 标题 其他元素 辅助样式 表单 通知和错误 一致的条目 页面结构 标题.页脚和导航 布局 其他页面结构元素 页面组件 各个页面 覆盖

  10. ASCII / Unicode / UTF-8 / GBK

    1 ASCII ASCII(American Standard Code for Information Interchange,美国标准信息交换代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现 ...