hdu 5945 Fxx and game【dp+单调队列】
来源:互联网 发布:森马价格知乎 编辑:程序博客网 时间:2024/06/07 07:11
Fxx and game
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 651 Accepted Submission(s): 155
Problem Description
Young theoretical computer scientist Fxx designed a game for his students.
In each game, you will 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. if k|X,X=X/k .
Now Fxx wants you to tell him the minimum steps to makeX become 1.
In each game, you will get three integers
Now Fxx wants you to tell him the minimum steps to make
Input
In the first line, there is an integer T(1≤T≤20) indicating the number of test cases.
As for the followingT 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 makeX become 1。
As for the following
For each text case,we assure that it's possible to make
Output
For each test case, output the answer.
Sample Input
29 2 111 3 3
Sample Output
43
题目大意:
一共有两种操作,问将x变成1需要的最少操作次数:
①X=X-I(0<=I<=tt)
②X=X/K(X%k==0)
思路:
1、考虑dp,设定dp【i】表示我们从数字1变成数字i使用的最少操作次数,那么不难推出其状态转移方程:
dp【i】=min(dp【i/k】+1,dp【i】)(i此时是k的倍数)
dp【i】=min(dp【i-j】+1,dp【i】)【0<=j<=tt】
2、根据数据范围可知,此时第二种状态转移方程的时间复杂度将会是:O(x*t),最大能达到10^12,是一个很大的操作数量级,那么考虑单调队列优化即可。
Ac代码:
#include<stdio.h>#include<string.h>#include<iostream>using namespace std;int dp[1000500];int que[1000500];int pos[1000500];int main(){ int tt; scanf("%d",&tt); while(tt--) { int x,k,t; scanf("%d%d%d",&x,&k,&t); int head=1; int tot=1; que[head]=0; pos[head]=1; memset(dp,0x3f3f3f3f,sizeof(dp)); dp[1]=0; for(int i=2;i<=x;i++) { if(i%k==0) { dp[i]=min(dp[i/k]+1,dp[i]); } while(head<=tot&&pos[head]<i-t)head++; dp[i]=min(dp[i],que[head]+1); while(head<=tot&&dp[i]<que[tot])tot--; que[++tot]=dp[i],pos[tot]=i; } printf("%d\n",dp[x]); }}
0 0
- hdu 5945 Fxx and Game dp(单调队列优化)
- 【HDU 5945】 Fxx and game 【单调队列优化dp】
- [hdu 5945 Fxx and game] dp+单调队列
- hdu 5945 Fxx and game (dp+单调队列)
- hdu - 5945 Fxx and game 【dp + 单调队列】
- hdu 5945 Fxx and game【dp+单调队列】
- [hdu 5945 Fxx and game] dp+单调队列
- BestCoderRound#89 HDU 5945 Fxx and game【单调队列+DP】
- HDU 5945 Fxx and game [单调队列+dp]【动态规划】
- 【HDU 5945】Fxx and game(DP+单调队列)
- hdu 5945 Fxx and game(dp+单调队列)
- HDU 5945 Fxx and game(DP+单调队列)
- HDU-5945 Fxx and game (dp+单调队列)
- HDU 5945 Fxx and game 单调队列优化DP
- HDU 5945 Fxx and game(dp+单调队列优化)
- HDU 5945 Fxx and game (DP+单调队列优化)
- hdu 5945 Fxx and game(单调队列,dp)
- 【HDU 5945 Fxx and game】+ 单调队列
- Problem 43 Sub-string divisibility (暴力...)
- sudo gem install cocoapods 没反应问题
- 解决java.sql.SQLException: Value '0000-00-00' can not be represented as java.sql.Date
- 求方程的根/按从小到大输出三个整数a,b,c
- HDU 5642King's Order 递推
- hdu 5945 Fxx and game【dp+单调队列】
- C#-Stmp发邮件
- 关于mouseout与mouseleave的区别
- python open()
- 【HDU】4324 - Triangle LOVE(拓扑)
- 1024. Palindromic Number (25)
- C#-string生成图片
- UVA 10328 Coin Toss 递推
- 跨进程通信之Messenger