• CodeForces 189A 166E 【DP ·水】


    非常感谢 Potaty 大大的援助使得我最后A出了这两题DP

    ==================================

    189A : 求切分后的ribbon最多的数目,不过要求切分后只能存在a or b or c 的长度

    O(n)的效率:遍历下来求 f[i - a]、f[i - b]、 f[i - c] 中的最大值

    如果i - a || b || c 的值小于0那么跳过

    来一张图,过程非常清晰

    当然,初始化对f 数组置-INF,否则可能出错

    //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
    #include <stdio.h>
    #include <iostream>
    #include <fstream>
    #include <cstring>
    #include <cmath>
    #include <stack>
    #include <string>
    #include <map>
    #include <set>
    #include <list>
    #include <queue>
    #include <vector>
    #include <algorithm>
    #define Max(a,b) (((a) > (b)) ? (a) : (b))
    #define Min(a,b) (((a) < (b)) ? (a) : (b))
    #define Abs(x) (((x) > 0) ? (x) : (-(x)))
    #define MOD 1000000007
    #define pi acos(-1.0)
    
    using namespace std;
    
    typedef long long           ll      ;
    typedef unsigned long long  ull     ;
    typedef unsigned int        uint    ;
    typedef unsigned char       uchar   ;
    
    template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
    template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;}
    
    const double eps = 1e-7      ;
    const int N = 1              ;
    const int M = 200000         ;
    const ll P = 10000000097ll   ;
    const int INF = 0x3f3f3f3f   ;
    
    int f[4100];
    
    int main(){
        int i, j, k, t, n, m, numCase = 0;
        int a, b, c;
        while(cin >> n >> a >> b >> c){
            for(i = 0; i <= n; ++i){
                f[i] = -INF;
            }
            f[0] = 0;
            for(i = 1; i <= n; ++i){
                int tempa = i - a;
                int tempb = i - b;
                int tempc = i - c;
                if(tempa >= 0){
                    checkmax(f[i], 1 + f[tempa]);
                }
                if(tempb >= 0){
                    checkmax(f[i], 1 + f[tempb]);
                }
                if(tempc >= 0){
                    checkmax(f[i], 1 + f[tempc]);
                }
            }
            cout << f[n] << endl;
        }
    
        return 0;
    }
    View Code

    ======================================

    166E: 这也是一道DP

    起点在D,然后这是一个四面体

    不难发现,其实A,B,C 是一样的,所以就不需要多开空间浪费了

    需要开两个数组a[2] , b[2] 就够了

    (通过 i & 1 来判断奇偶,还是头一次用TVT~)

    这道题目通过过程模拟可以一下子发现规律:

    //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
    #include <stdio.h>
    #include <iostream>
    #include <fstream>
    #include <cstring>
    #include <cmath>
    #include <stack>
    #include <string>
    #include <map>
    #include <set>
    #include <list>
    #include <queue>
    #include <vector>
    #include <algorithm>
    #define Max(a,b) (((a) > (b)) ? (a) : (b))
    #define Min(a,b) (((a) < (b)) ? (a) : (b))
    #define Abs(x) (((x) > 0) ? (x) : (-(x)))
    #define MOD 1000000007
    #define pi acos(-1.0)
    
    using namespace std;
    
    typedef long long           ll      ;
    typedef unsigned long long  ull     ;
    typedef unsigned int        uint    ;
    typedef unsigned char       uchar   ;
    
    template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
    template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;}
    
    const double eps = 1e-7      ;
    const int N = 1              ;
    const int M = 200000         ;
    const ll P = 10000000097ll   ;
    const int INF = 0x3f3f3f3f   ;
    
    
    int main(){
        int i, j, k, t, n, m, numCase = 0;
        ll a[2], b[2];
        while(EOF != scanf("%d",&n)){
            memset(a, 0, sizeof(a));
            memset(b, 0, sizeof(b));
            a[0] = 1;
            for(i = 1; i <= n; ++i){
                a[i & 1] = (3 * b[!(i & 1)]) % MOD;
                b[i & 1] = ((2 * b[!(i & 1)]) + a[!(i & 1)]) %  MOD;
            }
            cout << a[n & 1] << endl;
        }
    
        return 0;
    }
    View Code
  • 相关阅读:
    模仿Linux内核kfifo实现的循环缓存
    FFmpeg + SoundTouch实现音频的变调变速
    C++标准库实现WAV文件读写
    PHP写的一个轻量级的DI容器类(转)
    android App抓包工具的应用(转)
    Dell 服务器阵列扩容【经验分享(转)】
    hexo静态博客的安装及应用实践记录
    centos 6.5 升级php到5.6.17版本
    前端框架记录
    Virtual DOM 虚拟DOM的理解(转)
  • 原文地址:https://www.cnblogs.com/wushuaiyi/p/4280514.html
Copyright © 2020-2023  润新知