zoukankan      html  css  js  c++  java
  • HDU5945 Fxx and game(DP+单调队列优化)

    Fxx and game

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


    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
     
    Recommend
    wange2014   |   We have carefully selected several similar problems for you:  6216 6215 6214 6213 6212 
     哇哈哈哈哈这题上数学课的时候发现忘记把第一个数存入队列了(上课的时候没事干思忖了一下单调队列的原理还有单调队列和单调栈的用途),然后下课飞奔至姬房,顺带发现忘记压入后面的数了,反正一个下课就把这题搞好咯~ 不虚此行,撤!
     1 #include "bits/stdc++.h"
     2 using namespace std;
     3 typedef long long LL;
     4 const int MAX=1e6+5;
     5 int T,n,k,t;
     6 int dp[MAX];
     7 deque <int> q;
     8 int main(){
     9     freopen ("fxx.in","r",stdin);
    10     freopen ("fxx.out","w",stdout);
    11     int i,j;
    12     scanf("%d",&T);
    13     int an;
    14     while (T--){
    15         scanf("%d%d%d",&n,&k,&t);
    16         if (t==0){
    17             an=0;
    18             while (n!=1){n/=k;an++;}
    19             printf("%d
    ",an);
    20             continue;
    21         }
    22         if (k==0){
    23             printf("%d
    ",(n-2)/t+1);
    24             continue;
    25         }
    26         dp[1]=0;
    27         while (q.size()) q.pop_back();
    28         q.push_back(1);
    29         for (i=2;i<=n;i++){
    30             dp[i]=999999999;
    31             if (i%k==0) dp[i]=min(dp[i],dp[i/k]+1);
    32             while (q.size() && (i-t)>q.front()) q.pop_front();
    33             if (q.size()) dp[i]=min(dp[i],dp[q.front()]+1);
    34             while (q.size() && dp[i]<dp[q.back()]) q.pop_back();
    35             q.push_back(i);
    36         }
    37         printf("%d
    ",dp[n]);
    38     }
    39     return 0;
    40 }
    未来是什么样,未来会发生什么,谁也不知道。 但是我知道, 起码从今天开始努力, 肯定比从明天开始努力, 要快一天实现梦想。 千里之行,始于足下! ——《那年那兔那些事儿》
  • 相关阅读:
    ffmpeg rtmp推流 视频转码
    java日志发展史 log4j slf4j log4j2 jul jcl 日志和各种桥接包的关系
    nginx stream 流转发,可以转发rtmp、mysql访问流,转发rtmp、jdbc请求
    java web http 转https 通过nginx代理访问
    linux 服务器磁盘挂载
    novnc 通过websockify代理 配置多点访问
    linux 文件服务 minio 安装部署配置
    AOP实现原理,手写aop
    java 泛型
    JAVA反射getGenericSuperclass()用法
  • 原文地址:https://www.cnblogs.com/keximeiruguo/p/7681668.html
Copyright © 2011-2022 走看看