Skip to content

Commit de8b2af

Browse files
committed
some dp problems
1 parent b18e346 commit de8b2af

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed

Education/DP_Fibonacci.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <bits/stdc++.h>
2+
3+
#define forr(i,a,b) for(int i=a; i<b; i++)
4+
#define rfor(i,a,b) for(int i=a-1; i>=b; i--)
5+
6+
#define MX 1e4+5
7+
8+
using namespace std;
9+
10+
typedef long long ll;
11+
typedef pair<int, int> pii;
12+
typedef vector<int> vi;
13+
typedef vector<vi> vvi;
14+
15+
16+
vi fArr(40, 0);
17+
18+
int fib(int x) {
19+
// Fibonacci number, dp
20+
if(x < 2)
21+
return x;
22+
23+
if(fArr[x])
24+
return fArr[x];
25+
26+
int val = fib(x-1) + fib(x-2);
27+
fArr[x] = val;
28+
return val;
29+
}
30+
31+
void run_case() {
32+
//
33+
int x;
34+
cin >> x;
35+
36+
cout << fib(x) << "\n";
37+
}
38+
39+
40+
int main() {
41+
//
42+
ios::sync_with_stdio(false);
43+
44+
int tests;
45+
cin >> tests;
46+
47+
while (tests-- > 0)
48+
run_case();
49+
}

Education/DP_Min_Coin_Change.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <bits/stdc++.h>
2+
3+
#define forr(i,a,b) for(int i=a; i<b; i++)
4+
#define rfor(i,a,b) for(int i=a-1; i>=b; i--)
5+
#define pb push_back
6+
7+
#define MX 1e4+5
8+
9+
using namespace std;
10+
11+
typedef long long ll;
12+
typedef pair<int, int> pii;
13+
typedef vector<int> vi;
14+
typedef vector<vi> vvi;
15+
16+
17+
18+
void run_case() {
19+
//
20+
int n, target;
21+
cin >> n >> target;
22+
23+
vi coins(n);
24+
for(int &x: coins)
25+
cin >> x;
26+
27+
vi dp(target+1, INT_MAX);
28+
dp[0] = 0;
29+
forr(i, 1, target+1)
30+
for(int x: coins)
31+
dp[i] = min(dp[i], (i < x ? 0 : dp[i-x]) + 1);
32+
33+
//cout << "dp: ";
34+
//for(int x: dp)
35+
//cout << x << " ";
36+
//cout << "\n";
37+
38+
cout << dp[target] << "\n";
39+
}
40+
41+
42+
int main() {
43+
//
44+
ios::sync_with_stdio(false);
45+
46+
int tests;
47+
cin >> tests;
48+
49+
while (tests-- > 0)
50+
run_case();
51+
}

Education/DP_Min_Path_Sum.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <bits/stdc++.h>
2+
3+
#define forr(i,a,b) for(int i=a; i<b; i++)
4+
#define rfor(i,a,b) for(int i=a-1; i>=b; i--)
5+
#define pb push_back
6+
7+
#define MX 1e4+5
8+
9+
using namespace std;
10+
11+
typedef long long ll;
12+
typedef pair<int, int> pii;
13+
typedef vector<int> vi;
14+
typedef vector<vi> vvi;
15+
16+
17+
18+
void run_case() {
19+
//
20+
int n, m;
21+
cin >> n >> m;
22+
23+
vvi a;
24+
forr(i, 0, n) {
25+
vi la(m);
26+
forr(j, 0, m)
27+
cin >> la[j];
28+
a.pb(la);
29+
}
30+
31+
vvi dp(n, vi(m, 0));
32+
dp[0][0] = a[0][0];
33+
forr(i, 1, m)
34+
dp[0][i] = dp[0][i-1] + a[0][i];
35+
forr(i, 1, n)
36+
dp[i][0] = dp[i-1][0] + a[i][0];
37+
38+
forr(i, 1, n)
39+
forr(j, 1, m)
40+
dp[i][j] = min(dp[i][j-1], dp[i-1][j]) + a[i][j];
41+
42+
cout << "dp\n";
43+
for(vi idp: dp) {
44+
for(int x: idp)
45+
cout << x << " ";
46+
cout << "\n";
47+
}
48+
49+
cout << dp[n-1][m-1] << "\n";
50+
}
51+
52+
53+
int main() {
54+
//
55+
ios::sync_with_stdio(false);
56+
57+
int tests;
58+
cin >> tests;
59+
60+
while (tests-- > 0)
61+
run_case();
62+
}

0 commit comments

Comments
 (0)