hdu(HDU5945)单调队列优化DP

#include
#include
#include
using namespace std;
const int maxn = 1000000+10;
int dp[maxn];
int q[maxn];
int main()
{
    int cases,x,k,t;
    scanf("%d",&cases);
    while(cases--)
    {
        memset(dp,0x3f3f3f3f,sizeof(dp));
        scanf("%d%d%d",&x,&k,&t);
        if(k==0)
        {
            if((x-1)%t) printf("%d\n",(x-1)/t+1);
            else printf("%d\n",(x-1)/t);
            continue;
        }
        if(t==0)
        {
            int ans = 0;
            while(x>1) x/=k,ans++;
            printf("%d\n",ans); continue;
        }
        dp[1] = 0; int l = 1,r = 1; q[1] = 1;
        for(int i=2;i<=x;i++)
        {
            while(l<=r&&q[l]+t=dp[i]) r--;
            q[++r] = i;
        }
        printf("%d\n",dp[x]);
    }
    return 0;
}

你可能感兴趣的:(dp)