Fxx and game
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 547 Accepted Submission(s): 129
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=X−i(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.
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=X−i(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(1≤T≤20)
indicating the number of test cases.
As for the following T lines, each line contains three integers X,k,t(0≤t≤106,1≤X,k≤106)
For each text case,we assure that it's possible to make X become 1。
As for the following T lines, each line contains three integers X,k,t(0≤t≤106,1≤X,k≤106)
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 3dp[i]=min(dp[i],dp[i/k]+1); dp[i]=min(dp[i],dp[i-t,i-1]+1); 对于第二个需要用单调队列优化dp; 用f[]来维护,f的最左边为[i-t,i-1]中dp最小的点,往右依次增大 , 新加入的点i应从最右边开始,当右边的大于dp[i],则从f[]数组中删去,加入i#include<stdio.h> #include<string.h> #define INF 0x3f3f3f int dp[1000010]; int f[1000010]; int min(int a,int b) { return a<b?a:b; } int main() { int tt,t,x,k,i; scanf("%d",&tt); while(tt--) { scanf("%d%d%d",&x,&k,&t); memset(dp,INF,sizeof(dp)); int l=1; int r=1; dp[1]=0; f[1]=1; for(i=2;i<=x;i++) { while(l<=r&&f[l]<i-t) l++; if(i%k==0) dp[i]=min(dp[i],dp[i/k]+1); if(l<=r) dp[i]=min(dp[i],dp[f[l]]+1); while(l<=r&&dp[i]<dp[f[r]]) r--; f[++r]=i; } printf("%d\n",dp[x]); } return 0; }