"HDU 5945" Fxx and Game (dp+ monotone queue)
Ah ah ah ah ah ... BC The second question is so difficult ... Good food ... This game can't be played ....
Film A team of the main gold Qaq giant ... Now 2nd BC Gold. Orz ... @a1s4z5
The groove of the smooth spit ... Everyone BC page will be card ... Dorm into BC Card to die ... Exactly what the card looks like ... Campus network into the hot spot also card of the deadly, patiently waiting for the surface of a frame frame emerges ... And have been thrown out several streets ... And then...... Hey...... This a problem pit point is very fascinated, hack must be very happy ah, patiently waiting for the page after the appearance of ... Double-click a question in the room for one person ... I choose to give up ....
From this BC into a passer-by ...
The problem is still very good ... Even if it's a big cut off ... Fell..
Specific: Give x,t,k, to turn the number X into 1. There are two operations:
1. Turn the digital x into a digital y (0 <= x-y <= t), consuming one step.
2.x->x/k (If x%k = = 0), consumes one step.
The first idea was sharp ... Definition Dp[i] dp[i] is the minimum number of steps from X to I, then the answer is min (Dp[i]) (1<=i<k) min (Dp[i]) (1, because to K will definitely/k into 1, actually dp[1], so only consider 1~k-1.
Then for I (1 <= i < k), find the largest y, meet y%k = = 0 && y/k = i && y <= x
The calculation then changes from X to Y, then from Y to I, from I to 1. Before and after the T, there is a bit of violence in the middle. And then it exploded ... And then just found a place to write rub. And then changed it and measured some data to find that this greed is problematic ... And then it exploded ...
or behave yourself, dp[i] indicates that I becomes the minimum number of steps for 1.
So if i%k! = 0 Then Dp[i] = min (dp[j]) +1 (0 <= i-j <= t)
If i%k = = 0, take a dp[i/k in the minimum value].
Violent words will time out, maintain a and I distance t within the monotone queue, the queue values from left to right monotonically increment, the right side to the left is the value of the minimum number of steps within T.
The code is as follows:
#include <iostream> #include <cmath> #include <vector> #include <cstdlib> #include <cstdio > #include <climits> #include <ctime> #include <cstring> #include <queue> #include <stack&
Gt #include <list> #include <algorithm> #include <map> #include <set> #define LL Long Long #define Pr pair<int,int> #define FREAD (CH) freopen (CH, "R", stdin) #define FWRITE (CH) freopen (CH, "w", stdout) using namespace s
td
const int INF = 0X3F3F3F3F;
const int mod = 1E9+7;
Const double EPS = 1e-8;
const int MAXN = 1123456;
int S[MAXN];
int DP[MAXN];
int main () {//fread ("");
Fwrite ("");
int t,t,l,r,x,k;
scanf ("%d", &t);
while (t--) {scanf ("%d%d%d", &x,&k,&t);
DP[1] = 0;
L = 0;
r =-1;
S[++r] = 1;
for (int i = 2; i <= x; ++i) {if (i%k = = 0 && k! = 1) dp[i] = dp[i/k]+1;
if (l <= R) {if (i%k = = 0 && k! = 1) dp[i] = min (dp[i],dp[s[l]]+1);
else Dp[i] =dp[s[l]]+1;
} while (l <= R && Dp[i] <= dp[s[r]) r--;
S[++r] = i;
while (l <= R && i-s[l] >= t) ++l;
} printf ("%d\n", dp[x]);
} return 0; }