zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    RedHat5.8 编译内核驱动 合成initrd.img
    matrix-gui-2.0 将javascript文件夹改成js文件夹
    使用PHP配置文件
    Ubuntu 16.10 Apache PHP Server
    Ubuntu 16.10 中文环境 Shell输出英文提示
    制作SD卡img文件,并扩容
    Linux syslogd
    Windows cmd findstr
    jquery ztree异步搜索
    怎样在点击li时添加样式,移除兄弟样式
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/6016334.html
Copyright © 2011-2022 走看看