Fxx and game
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 1076 Accepted Submission(s): 281
Total Submission(s): 1076 Accepted Submission(s): 281
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 3
Source
BestCoder Round #89
题意:给定整数n,或除k(必须整除),或减0~t,求能变成1的最小步数。
思路:当时做的的时候用贪心写了一波,可惜过不了终测,正确思路:
递推一下,由于对固定的i,需要在[i-t,i-1]中取个最小值,所以用单调队列维护一下就好了。
代码:
题意:给定整数n,或除k(必须整除),或减0~t,求能变成1的最小步数。
思路:当时做的的时候用贪心写了一波,可惜过不了终测,正确思路:
递推一下,由于对固定的i,需要在[i-t,i-1]中取个最小值,所以用单调队列维护一下就好了。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+100;
ll ans;
int d[maxn];
int X,k,t;
int f[maxn];
int T;
int main(){
scanf("%d",&T);
while (T--)
{
scanf("%d%d%d",&X,&k,&t);
for(int i=0;i<=X;i++)
f[i]=1e9;
int head=1,tail=0;
f[1]=0;
d[++tail]=1;
for(int i=2;i<=X;i++)
{
if (i % k==0)
f[i]=f[i / k]+1;
while (head<=tail && d[head]+t<i)
head++;
if(head<=tail)
f[i]=min(f[i],f[d[head]]+1);
d[++tail]=i;
while (head<tail && f[d[tail-1]]>f[d[tail]])
d[tail-1]=d[tail],tail--;
}
cout<<f[X]<<endl;
}
return 0;
}