"Hdu 5945" Fxx and Game (recursive |dp+ monotone queue)

Source: Internet
Author: User
Fxx and Game Time limit:3000/1500 MS (java/others) Memory limit:131072/65536 K (java/others)
Total submission (s): 347 Accepted Submission (s):

Problem Description Young theoretical computer scientist Fxx designed a game for his students.
In each game, you'll get three integers x,k,t.in each step and you can have only do one of the following moves:

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

2.if k| X,x=x/k.
Now Fxx wants-him the minimum steps to make X become 1.
Input in the first line, there are 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 the 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
Recommend wange2014 | We have carefully selected several similar problems for you:5947 5946 5943 5942 5941 "Solving" " recursive |dp+ monotone queue" "from small to large processing: from 1 to n,f[i] means I at least a few transformations can change to 1" "F[i]=max (F[j] (max (i-t,1) <=j<i), [k|i]f[i/k]) +1, each time first to determine whether the current number divisible by K, and then F[j] (max (i-t,1) <=j<i) in each of the comparison, Maximum Value " "Then, we can find that the actual maximum value of f[i/k] (I can divide K) and F[j] (max (i-t,1) <=j<i) is a large one. Then, we can use the monotone queue to maintain the maximum value of F[j] (max (i-t,1) <=j<i), so that we can dispense with a heavy cycle, it is possible to AC. " [during the exam, use DFS to implement the above process, resulting in a stack overflow. ]

#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;
 



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.