Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)

Total Submission(s): 360 Accepted Submission(s): 84

Problem Description

Young theoretical computer scientist Fxx designed a game for his students.

In each game, you will get three integers X,k,t.In each step, you can only do one of the following moves:

1.X=X−i(0<=i<=t).

2.if k|X,X=X/k.

Now Fxx wants you to tell him the minimum steps to make X become 1.

Input

In the first line, there is an integer T(1≤T≤20) indicating the number of test cases.

As for the following T lines, each line contains three integers X,k,t(0≤t≤106,1≤X,k≤106)

For each text case,we assure that it’s possible to make X become 1。

Output

For each test case, output the answer.

Sample Input

2

9 2 1

11 3 3

Sample Output

4

3

Source

BestCoder Round #89

【题解】



f[i] = min(f[i],f[i/k]+1);

f[i] = min(f[i],f[i-t..i-1]+1);

第二行那个转移可用一个单调队列优化;

因为f[i+1]>=f[i];

dl的最左边维护的是i-t..i-1这个区间内f值最小的点的下标;然后往右f值依次递增;

如果新加入的点i,f[i]小于这个dl最右边x对应值f[x],则把x挤掉(那些被挤掉的元素是肯定没有用了的,如果到了某个时刻f[i]被挤掉了,因为i>x,则x肯定也要被挤掉),重复上述步骤;

就能维护一个单调队列;

有点厉害.

#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#define lson L,m,rt<<1
#define rson m+1,R,rt<<1|1
#define LL long long using namespace std; const int MAXN = 2e6; const int dx[5] = {0,1,-1,0,0};
const int dy[5] = {0,0,0,-1,1};
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f; int x,k,t,l,r;
int f[MAXN],dl[MAXN]; void input_LL(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} void input_int(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
int T;
input_int(T);
while (T--)
{
memset(f,INF,sizeof(f));
input_int(x);input_int(k);input_int(t);
l = 1;r = 1;dl[r] = 1;
f[1] = 0;
for (int i = 2;i <= x;i++)
{
while (l<=r && dl[l]<i-t)l++;
if ((i%k)==0)
f[i] = min(f[i],f[i/k]+1);
if (l <=r)
f[i] = min(f[i],f[dl[l]]+1);
while (l<=r && f[i]<f[dl[r]]) r--;
dl[++r] = i;
}
printf("%d\n",f[x]);
}
return 0;
}

【23.33%】【hdu 5945】Fxx and game的更多相关文章

  1. 【23.33%】【codeforces 557B】Pasha and Tea

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  2. 【23.33%】【codeforces 664C】International Olympiad

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  3. 【改革春风吹满地 HDU - 2036 】【计算几何-----利用叉积计算多边形的面积】

    利用叉积计算多边形的面积 我们都知道计算三角形的面积时可以用两个邻边对应向量积(叉积)的绝对值的一半表示,那么同样,对于多边形,我们可以以多边形上的一个点为源点,作过该点并且过多边形其他点中的某一个的 ...

  4. 【23. 合并K个排序链表】【困难】【优先队列/堆排序】

    合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. 示例: 输入: [ 1->4->5, 1->3->4, 2->6] 输出: 1->1-> ...

  5. 【HDU 2255】奔小康赚大钱 (最佳二分匹配KM算法)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  6. 七夕节 (HDU - 1215) 【简单数论】【找因数】

    七夕节 (HDU - 1215) [简单数论][找因数] 标签: 入门讲座题解 数论 题目描述 七夕节那天,月老来到数字王国,他在城门上贴了一张告示,并且和数字王国的人们说:"你们想知道你们 ...

  7. 【二分】【最长上升子序列】HDU 5489 Removed Interval (2015 ACM/ICPC Asia Regional Hefei Online)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5489 题目大意: 一个N(N<=100000)个数的序列,要从中去掉相邻的L个数(去掉整个区间 ...

  8. 【贪心】【模拟】HDU 5491 The Next (2015 ACM/ICPC Asia Regional Hefei Online)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5491 题目大意: 一个数D(0<=D<231),求比D大的第一个满足:二进制下1个个数在 ...

  9. 【动态规划】【二分】【最长上升子序列】HDU 5773 The All-purpose Zero

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5773 题目大意: T组数据,n个数(n<=100000),求最长上升子序列长度(0可以替代任何 ...

随机推荐

  1. 洛谷 P1143 进制转换

    P1143 进制转换 题目描述 请你编一程序实现两种不同进制之间的数据转换. 输入输出格式 输入格式: 输入数据共有三行,第一行是一个正整数,表示需要转换的数的进制n(2≤n≤16),第二行是一个n进 ...

  2. Kaggle实战分类问题2

    Kaggle实战之二分类问题 0. 前言 1. MNIST 数据集 2. 二分类器 3. 效果评测 4. 多分类器与误差分析 5. Kaggle 实战 0. 前言 “尽管新技术新算法层出不穷,但是掌握 ...

  3. Loadrunner--集合点

    集合点的意思是等到特定的用户数后再一起执行某个操作,比如一起保存,一起提交(我们通常意义上的并发数并不是指一起提交或者一起保存),一般情况下使用不到集合点,不过,订票系统或者促销类需要用到,比如说某个 ...

  4. 老李的菜园 mysql 自定义函数

    新建: Create function function_name(参数列表)returns返回值类型 函数体 函数名,应该合法的标识符,并且不应该与已有的关键字冲突. 一个函数应该属于某个数据库,可 ...

  5. WCF REST 基础教程

    概述 Representational State Transfer(REST)是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格. 因此REST是设计风格而不是标准,R ...

  6. stm32优先级

  7. UI组件之TextView及其子类(三)ToggleButton和Switch

    ToggleButton.Switch.CheckBox和RadioButton都是继承自android.widget.CompoundButton,意思是可选择的,因此它们的使用方法都非常类似. C ...

  8. x265探索与研究(四):怎样编码视频?

    怎样编码视频?           本文介绍x265中的两种编码视频方法,一是採用命令行的方法.二是GUI的方式.   1.命令行的方法   (1).第一种方式生成*.265格式的视频文件       ...

  9. ios开发ios9新特性关键字学习:泛型,逆变,协变,__kindof

    一:如何去学习?都去学习什么? 1:学习优秀项目的设计思想,多问几个为什么,为什么要这么设计,这么设计的好处是什么,还能不能在优化 ,如何应用到自己的项目中 2:学习优秀项目的代码风格,代码的封装设计 ...

  10. html5的float属性超详解(display,position, float)(文本流)

    html5的float属性超详解(display,position, float)(文本流) 一.总结 1.文本流: 2.float和绝对定位都不占文本流的位置 3.普通流是默认定位方式,就是依次按照 ...