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 }
    未来是什么样,未来会发生什么,谁也不知道。 但是我知道, 起码从今天开始努力, 肯定比从明天开始努力, 要快一天实现梦想。 千里之行,始于足下! ——《那年那兔那些事儿》
  • 相关阅读:
    卡嘉mysql命令
    Go并发控制和超时控制
    sync包介绍
    Golang-RSA加密解密-数据无大小限制
    GO json 如何转化为 map 和 struct
    go之gorm
    go mod 生成 vendor
    go语言中找&和*区别
    Swoole的process通信的方式
    centos安装python3
  • 原文地址:https://www.cnblogs.com/keximeiruguo/p/7681668.html
Copyright © 2011-2022 走看看