HDU 5945 maintains a monotonic queue DP

Source: Internet
Author: User
Tags cas

Fxx and Game

Time limit:3000/1500 MS (java/others) Memory limit:131072/65536 K (java/others)
Total submission (s): 688 Accepted Submission (s): 162


Problem Descriptionyoung Theoretical computer scientist Fxx designed a game for his students.

In each game, you'll get three integersX,k,t . In each step, you can only do one of the following moves:

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

2.Ifk| x,x=x/k .

Now Fxx wants-him the minimum steps to makeX become 1.

Inputin the first line, there was an integerT(1≤t≤) indicating the number of test cases.

As for the followingTLines, each line contains three integersX,K,T(0≤t≤6,1≤X,k≤6 )

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

Outputfor each test case, output the answer.

Sample Input29 2 111 3 3

Sample Output43

Source bestcoder Round #89 Test instructions: give you three numbers x,k,t have two operations1.X=X−I(0<=I<=T) .2. If k| x,x=x/K. Ask at least how many steps to make x->1 Solving: Maintaining a monotone queue with increasing number of transformations
1 /******************************2 code by drizzle3 blog:www.cnblogs.com/hsd-/4 ^ ^    ^ ^5 o o6 ******************************/7#include <bits/stdc++.h>8#include <map>9#include <Set>Ten#include <cmath> One#include <queue> A#include <bitset> -#include <math.h> -#include <vector> the#include <string> -#include <stdio.h> -#include <cstring> -#include <iostream> +#include <algorithm> - #pragmaComment (linker, "/stack:102400000,102400000") + using namespacestd; A #defineA First at #defineB Second - Const intMod=1000000007; - Const intmod1=1000000007; - Const intMod2=1000000009; - Const Doubleeps=0.00000001; - typedef __int64 LL; in Constll mod=1000000007; - Const intinf=1000000010; to Constll max=1ll<< -; + Const Doubleeps=1e-8; - Const Doubleinf=~0u>>1; the Const DoublePi=acos (-1.0); *typedefDoubledb; $typedef unsignedint UINT;Panax Notoginsengtypedef unsignedLong Longull; - intCAs; the intX,k,t,l,r; + intd[1000006]; A intdis[1000006]; the intMain () + { -scanf"%d",&CAs); $          for(intI=1; i<=cas; i++) $         { -scanf" %d%d%d",&x,&k,&t); -L=r=1; thed[1]=1; -              for(intj=2; j<=x;j++) dis[j]=INF;Wuyidis[1]=0; the              for(intj=2; j<=x; J + +) -             { Wu                  while(L&LT;=R&AMP;&AMP;D[L]&LT;J-T) l++;//Remove unused points from the queue -                 if(l<=r) dis[j]=dis[d[l]]+1; About                 if(j%k==0) Dis[j]=min (dis[j],dis[j/k]+1); $                  while(L<=r&&dis[d[r]]>=dis[j]) r--;//Remove the number of transitions in the queue -d[++r]=J; -             } -printf"%d\n", dis[x]); A         } +     return 0; the}

HDU 5945 maintains a monotonic queue DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.