• HDU 5945 Fxx and game (DP+单调队列)


    题意:给定一个 x, k, t,你有两种操作,一种是 x - i (0 <= i <= t),另一种是 x / k  (x % k == 0)。问你把x变成1需要的最少操作。

    析:这肯定是DP,也想到可能是单调队列,但是不会啊。。。。就是胡搞了一发,虽然AC了,但是效率极低,比用单调队列少10倍。

    dp[i] 表示把 i 变成 1,要用的最少步骤,然后每次取最优。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    //#include <tr1/unordered_map>
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    //using namespace std :: tr1;
    
    typedef long long LL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const LL LNF = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 1e6 + 5;
    const LL mod = 10000000000007;
    const int N = 1e6 + 5;
    const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
    const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
    const int hr[]= {-2, -2, -1, -1, 1, 1, 2, 2};
    const int hc[]= {-1, 1, -2, 2, -2, 2, -1, 1};
    const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    inline LL gcd(LL a, LL b){  return b == 0 ? a : gcd(b, a%b); }
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline int Min(int a, int b){ return a < b ? a : b; }
    inline int Max(int a, int b){ return a > b ? a : b; }
    inline LL Min(LL a, LL b){ return a < b ? a : b; }
    inline LL Max(LL a, LL b){ return a > b ? a : b; }
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
    }
    int dp[maxn];
    int q[maxn];
    
    int solve(int k, int x){
        int cnt = 0;
        while(x){
            x /= k;
            ++cnt;
        }
        return cnt-1;
    }
    
    int main(){
        int T;  cin >> T;
        while(T--){
            int x, k, t;
            scanf("%d %d %d", &x, &k, &t);
            if(k == 1){ printf("%d
    ", (x-1)%t == 0 ? (x-1)/t : (x-1)/t+1);  continue; }
            else if(!t){ printf("%d
    ", solve(k, x));   continue; }
            dp[1] = 0;  q[1] = 1;
            int l = 1, r = 1;
            for(int i = 2; i <= x; ++i){
                while(q[l] < i-t)  ++l;
                dp[i] = dp[q[l]] + 1;
                if(i % k == 0)  dp[i] = Min(dp[i], dp[i/k]+1);
                while(l <= r && dp[q[r]] >= dp[i])  --r;
                q[++r] = i;
            }
            printf("%d
    ", dp[x]);
        }
        return 0;
    }
    
  • 相关阅读:
    debian下使用mplayer
    Linux缺点要挟网银平安 SSL证书遽需改换
    linux用C如何鉴别一个目录能否为空
    Ubuntu旗舰版(Ultimate)1.8
    Linux系统下IP以及DNS设置方法
    升级firefox 3.0 beta 到RC1
    微星下月推Wind超低价NB 可运转XP和Linux
    Linux下hosts、host.conf、resolv.conf的区别
    传华硕将在6月3日闪现EBox台式电脑
    linux批改ssh端口和避免root远程上岸设置
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/6016334.html
Copyright © 2020-2023  润新知