HDU 5945 Fxx and game (DP+单调队列)
题意:给定一个 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+单调队列)的更多相关文章
- hdu 5945 Fxx and game(单调队列优化DP)
题目链接:hdu 5945 Fxx and game 题意: 让你从x走到1的位置,问你最小的步数,给你两种走的方式,1.如果k整除x,那么你可以从x走一步到k.2.你可以从x走到j,j+t<= ...
- [poj3017] Cut the Sequence (DP + 单调队列优化 + 平衡树优化)
DP + 单调队列优化 + 平衡树 好题 Description Given an integer sequence { an } of length N, you are to cut the se ...
- DP+单调队列 codevs 1748 瑰丽华尔兹(还不是很懂具体的代码实现)
codevs 1748 瑰丽华尔兹 2005年NOI全国竞赛 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 题目描述 Descripti ...
- hdu 3706 Second My Problem First 单调队列
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3706 Second My Problem First Time Limit: 12000/4000 M ...
- 习题:烽火传递(DP+单调队列)
烽火传递[题目描述]烽火台又称烽燧,是重要的防御设施,一般建在险要处或交通要道上.一旦有敌情发生,白天燃烧柴草,通过浓烟表达信息:夜晚燃烧干柴,以火光传递军情.在某两座城市之间有n个烽火台,每个烽火台 ...
- (noip模拟二十一)【BZOJ2500】幸福的道路-树形DP+单调队列
Description 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一同晨练来享受在一起的时光. 他们画出了晨练路线的草图,眼尖的小T发现可以用树来描绘这个草图. ...
- 3622 假期(DP+单调队列优化)
3622 假期 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 黄金 Gold 题目描述 Description 经过几个月辛勤的工作,FJ决定让奶牛放假.假期可以在1-N天内任意选择 ...
- 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 ...
- HDU 5945 题解(DP)(单调队列)
题面: Fxx and game Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) T ...
随机推荐
- python学习之-- shutil模块
shutil 模块功能:文件/文件夹的复制,压缩处理模块shutil.copyfileobj(fsrc,fdst[,length]):将文件内容拷贝到另一个文件中,也可以是部分内容举例:文件复制 im ...
- Last Defence - UVA7045
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- freeswitch对媒体的处理的三种方式
一.默认方式:媒体通过freeswitch, RTP被freeswtich转发, freeswitch控制编码的协商并在协商不一致时提供语音编码转换能力, 支持录音,二次拨号等. 二.代理模式: ...
- Spring中使用存储过程
以下内容引用自http://wiki.jikexueyuan.com/project/spring/jdbc-framework-overview/sql-stored-procedure-in-sp ...
- oracle数据库 SQL语句、内置函数大全
1.数值函数 函数 返回值 样例 显示 CEIL(n) 大于或等于数值n的最小整数 SELECT CEIL(10.6) FROM TABLE_NAME; 11 FLOOR(n) 小于等于数 ...
- Java反射常用示例
package xmq.study.reflection; import java.lang.annotation.Annotation; import java.lang.reflect.Const ...
- 如何把你的Windows PC变成瘦客户机
越来越多的用户开始使用vmware view 4.5来做为企业桌面虚拟化的平台,通过view,所有的管理工作都转移到数据中心,但是考虑到成本原因,很多人员还在使用PC机,有没有办法将PC机变成瘦客户机 ...
- swift-for循环遍历,遍历字典,循环生成数组
// Playground - noun: a place where people can play import UIKit //--------------------------------- ...
- JS 省市区三级联动
JS 省市区三级联动: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...
- CSS中:overflow:hidden的作用
功能1.隐藏溢出 在IE6下,当子容器的宽高超出父容器时,父容器就会被撑开来. 要想解决这个问题,在父容器中除定义宽和高的值以外,还必须写overflow:hidden,这样就能把子容器的其它内容隐 ...