【hdu 5945】Fxx and game(递推|dp+单调队列)

30 篇文章 0 订阅
11 篇文章 0 订阅

Fxx and game

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

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:   5947  5946  5943  5942  5941 
 
【题解】【递推|dp+单调队列】
【从小往大处理:从1枚举到n,f[i]表示i最少经几次变换能变到1】
【f[i]=max(f[j](max(i-t,1)<=j<i),[k|i]f[i/k])+1,每次先判断当前数是否能整除k,再与f[j](max(i-t,1)<=j<i)中的每一个相比较,取最大值
【那么,我们可以发现,实际就是在f[i/k](i能整除k)和f[j](max(i-t,1)<=j<i)的最大值中选一个大的。那么,我们可以用单调队列来维护f[j](max(i-t,1)<=j<i)的最大值,这样就能省掉一重循环,就可以AC!
[考试时,用dfs实现上述过程,结果栈溢出!]

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int t,k,n,T;
int f[1000010],que[1000010],minn[1000010];
int main()
{
//	freopen("int.txt","r",stdin);
//	freopen("my.txt","w",stdout);
	int i,j;
	scanf("%d",&T);
	while(T--)
	 {
	 	memset(f,127,sizeof(f));
	 	int head,tail; head=tail=1;
	 	f[1]=0; minn[tail]=0; que[tail]=1;
	 	scanf("%d%d%d",&n,&k,&t);
	 	for(i=2;i<=n;++i)
	     {
	     	if(!(i%k)) f[i]=f[i/k]+1;
	     	int m=max(i-t,1);
	     	while(tail>head&&que[head]<m) head++;
	     	f[i]=min(f[i],minn[head]+1);
	     	while(tail>=head&&f[i]<=minn[tail]) tail--;
			tail++; minn[tail]=f[i]; que[tail]=i; 
		 }
		printf("%d\n",f[n]);
	 }
	return 0;
 } 



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值