非常感谢 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; }
======================================
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; }