• hdu 5945 Fxx and game 单调队列优化dp


    Fxx and game

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



    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
     
    Source
    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<stack>
    #include<cstring>
    #include<vector>
    #include<list>
    #include<set>
    #include<map>
    using namespace std;
    #define ll long long
    #define pi (4*atan(1.0))
    #define eps 1e-14
    #define bug(x)  cout<<"bug"<<x<<endl;
    const int N=1e6+10,M=1e6+10,inf=2e9;
    const ll INF=1e18+10,mod=2147493647;
    int dp[N];
    int q[N];
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            memset(dp,0,sizeof(dp));
            int n,k,t;
            scanf("%d%d%d",&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/k]+1,dp[i]);
                while(l<r&&dp[q[r-1]]>=dp[i])r--;
                q[r++]=i;
            }
            //for(int i=1;i<=n;i++)
             //   printf("%d ",dp[i]);
            //printf("
    ");
            printf("%d
    ",dp[n]);
        }
        return 0;
    }
  • 相关阅读:
    消息中间件
    swagger2 接口文档,整个微服务接口文档
    Java并发编程笔记之基础总结(二)
    Java并发编程笔记之基础总结(一)
    Python3 web Crawler
    使用JetBrains Intellij IDEA 开发Java EE应用
    用 Tomcat 和 Eclipse 开发 Web 应用程序
    gvim背景配色
    COBOL学习(2)
    如何删除一个顽固的文件(win)
  • 原文地址:https://www.cnblogs.com/jhz033/p/6492535.html
Copyright © 2020-2023  润新知