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

 

转载于:https://www.cnblogs.com/wushuaiyi/p/4280514.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值