CodeForces 189A 166E 【DP ·水】

时间:2023-03-09 17:15:08
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那么跳过

来一张图,过程非常清晰

CodeForces 189A 166E 【DP ·水】

当然,初始化对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- ;
const int N = ;
const int M = ;
const ll P = 10000000097ll ;
const int INF = 0x3f3f3f3f ; int f[]; int main(){
int i, j, k, t, n, m, numCase = ;
int a, b, c;
while(cin >> n >> a >> b >> c){
for(i = ; i <= n; ++i){
f[i] = -INF;
}
f[] = ;
for(i = ; i <= n; ++i){
int tempa = i - a;
int tempb = i - b;
int tempc = i - c;
if(tempa >= ){
checkmax(f[i], + f[tempa]);
}
if(tempb >= ){
checkmax(f[i], + f[tempb]);
}
if(tempc >= ){
checkmax(f[i], + f[tempc]);
}
}
cout << f[n] << endl;
} return ;
}

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

166E: 这也是一道DP

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

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

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

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

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

CodeForces 189A 166E 【DP ·水】

//#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- ;
const int N = ;
const int M = ;
const ll P = 10000000097ll ;
const int INF = 0x3f3f3f3f ; int main(){
int i, j, k, t, n, m, numCase = ;
ll a[], b[];
while(EOF != scanf("%d",&n)){
memset(a, , sizeof(a));
memset(b, , sizeof(b));
a[] = ;
for(i = ; i <= n; ++i){
a[i & ] = ( * b[!(i & )]) % MOD;
b[i & ] = (( * b[!(i & )]) + a[!(i & )]) % MOD;
}
cout << a[n & ] << endl;
} return ;
}