• hdu


    Fxx and game

    Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
    Total Submission(s): 347    Accepted Submission(s): 78


    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=Xi(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(1T20) indicating the number of test cases.

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

    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
     

    dp方程还是比较好推的,关键使用单调队列进行优化。

    #include <map>
    #include <set>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <iostream>
    #include <stack>
    #include <cmath>
    #include <string>
    #include <vector>
    #include <cstdlib>
    //#include <bits/stdc++.h>
    //#define LOACL
    #define space " "
    using namespace std;
    typedef long long LL;
    //typedef __int64 Int;
    typedef pair<int, int> paii;
    const int INF = 0x3f3f3f3f;
    const double ESP = 1e-5;
    const double PI = acos(-1.0);
    const LL MOD = 1e9 + 7;
    const int MAXN = 1e6 + 10;
    int dp[MAXN], que[MAXN], loc[MAXN];
    int X, k, t;
    int main() {
        int T;
        scanf("%d", &T);
        while (T--) {
            scanf("%d%d%d", &X, &k, &t);
            dp[1] = 0; dp[0] = INF;
            int tail = 1, head = 1;
            que[head] = 0; loc[head] = 1;
            for (int i = 2; i <= X; i++) {
                dp[i] = INF;
                if (i%k == 0) dp[i] = dp[i/k] + 1;
                //找出队顶
                while (head <= tail && i - t > loc[head]) head++;
                dp[i] = min(dp[i], que[head] + 1);
                //新元素加入到队列
                while (head <= tail && dp[i] < que[tail]) tail--;
                que[++tail] = dp[i]; loc[tail] = i;
            }
            printf("%d
    ", dp[X]);
        }
        return 0;
    }



  • 相关阅读:
    eclipse对项目Working Sets整理分类
    word中visio只显示边框,不显示内容解决
    使用WebStorm运行vue项目
    如何提高你的学习速度-超链接式学习法
    SQL中的join连接查询
    TCP的三次握手
    Tomcat 实现热部署
    Linux下软件设成系统服务运行
    Redis服务器搭建
    nginx.conf完整配置实例
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770765.html
Copyright © 2020-2023  润新知