From d041dae604443eeaa7ccb5d4be54588cfa156dc3 Mon Sep 17 00:00:00 2001 From: shaw8wit Date: Sun, 4 Oct 2020 17:28:14 +0530 Subject: [PATCH] AtCoder Educational DP added --- .../AtCoder Educational DP/chessmetric.cpp | 26 ++++++++++ .../AtCoder Educational DP/coin.cpp | 27 +++++++++++ .../AtCoder Educational DP/deque.cpp | 18 +++++++ .../AtCoder Educational DP/frog.cpp | 23 +++++++++ .../AtCoder Educational DP/frog2.cpp | 27 +++++++++++ .../AtCoder Educational DP/grid.cpp | 18 +++++++ .../AtCoder Educational DP/kanpsack2.cpp | 47 +++++++++++++++++++ .../AtCoder Educational DP/knapsack.cpp | 40 ++++++++++++++++ .../AtCoder Educational DP/lcs.cpp | 47 +++++++++++++++++++ .../AtCoder Educational DP/longestPath.cpp | 41 ++++++++++++++++ .../AtCoder Educational DP/stones.cpp | 23 +++++++++ .../AtCoder Educational DP/sushi.cpp | 33 +++++++++++++ .../AtCoder Educational DP/vacation.cpp | 30 ++++++++++++ 13 files changed, 400 insertions(+) create mode 100644 Contest-Questions/AtCoder Educational DP/chessmetric.cpp create mode 100644 Contest-Questions/AtCoder Educational DP/coin.cpp create mode 100644 Contest-Questions/AtCoder Educational DP/deque.cpp create mode 100644 Contest-Questions/AtCoder Educational DP/frog.cpp create mode 100644 Contest-Questions/AtCoder Educational DP/frog2.cpp create mode 100644 Contest-Questions/AtCoder Educational DP/grid.cpp create mode 100644 Contest-Questions/AtCoder Educational DP/kanpsack2.cpp create mode 100644 Contest-Questions/AtCoder Educational DP/knapsack.cpp create mode 100644 Contest-Questions/AtCoder Educational DP/lcs.cpp create mode 100644 Contest-Questions/AtCoder Educational DP/longestPath.cpp create mode 100644 Contest-Questions/AtCoder Educational DP/stones.cpp create mode 100644 Contest-Questions/AtCoder Educational DP/sushi.cpp create mode 100644 Contest-Questions/AtCoder Educational DP/vacation.cpp diff --git a/Contest-Questions/AtCoder Educational DP/chessmetric.cpp b/Contest-Questions/AtCoder Educational DP/chessmetric.cpp new file mode 100644 index 00000000..1929ba40 --- /dev/null +++ b/Contest-Questions/AtCoder Educational DP/chessmetric.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; + +#define fo(i, n) for(int i=0; i> t >> a >> b; + arr[a + 2][b + 2][0] = 1; + cin >> a >> b >> n; + + Fo(m, 1, n + 1) { + Fo(i, 2, t + 2) { + Fo(j, 2, t + 2) { + arr[i][j][m] += arr[i - 2][j - 1][m - 1] + arr[i - 2][j + 1][m - 1] + arr[i + 2][j - 1][m - 1] + arr[i + 2][j + 1][m - 1] + arr[i - 1][j - 2][m - 1] + arr[i - 1][j + 2][m - 1] + arr[i + 1][j - 2][m - 1] + arr[i + 1][j + 2][m - 1] + arr[i - 1][j - 1][m - 1] + arr[i - 1][j][m - 1] + arr[i - 1][j + 1][m - 1] + arr[i][j - 1][m - 1] + arr[i][j + 1][m - 1] + arr[i + 1][j - 1][m - 1] + arr[i + 1][j][m - 1] + arr[i + 1][j + 1][m - 1]; + } + } + } + + cout << arr[a + 2][b + 2][n]; + return 0; +} \ No newline at end of file diff --git a/Contest-Questions/AtCoder Educational DP/coin.cpp b/Contest-Questions/AtCoder Educational DP/coin.cpp new file mode 100644 index 00000000..81ad4ac5 --- /dev/null +++ b/Contest-Questions/AtCoder Educational DP/coin.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; + +#define fo(i, n) for(int i=0; i> n; + long double dp[n + 2] = {0}, p; + dp[1] = 1; + + fo(i, n) { + cin >> p; + for (int j = i + 2; j > 0; j--) { + dp[j] = dp[j] * (1 - p) + dp[j - 1] * p; + } + } + + p = 0; + Fo(i, n / 2 + 2, n + 2) p += dp[i]; + + cout << setprecision(10) << p; + + return 0; +} \ No newline at end of file diff --git a/Contest-Questions/AtCoder Educational DP/deque.cpp b/Contest-Questions/AtCoder Educational DP/deque.cpp new file mode 100644 index 00000000..fe8e8fe8 --- /dev/null +++ b/Contest-Questions/AtCoder Educational DP/deque.cpp @@ -0,0 +1,18 @@ +#include +using namespace std; + +#define ll long long +#define fo(i, n) for(i=0; i> n; + ll dp[n], a[n]; + fo(i, n) cin >> a[i], dp[i] = a[i]; + Fo(i, 1, n) fo(j, (n - i)) dp[j] = max(a[i + j] - dp[j], a[j] - dp[j + 1]); + cout << dp[0]; + + return 0; +} \ No newline at end of file diff --git a/Contest-Questions/AtCoder Educational DP/frog.cpp b/Contest-Questions/AtCoder Educational DP/frog.cpp new file mode 100644 index 00000000..3e77e72b --- /dev/null +++ b/Contest-Questions/AtCoder Educational DP/frog.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; + +#define fo(i, n) for(int i=0; i> n; + + long arr[n], temp[n]; + fo(i, n) cin >> arr[i]; + + temp[0] = 0; + temp[1] = abs(arr[0] - arr[1]); + + Fo(i, 2, n) + temp[i] = min(abs(arr[i] - arr[i - 1]) + temp[i - 1], abs(arr[i] - arr[i - 2]) + temp[i - 2]); + + cout << temp[n - 1]; +} \ No newline at end of file diff --git a/Contest-Questions/AtCoder Educational DP/frog2.cpp b/Contest-Questions/AtCoder Educational DP/frog2.cpp new file mode 100644 index 00000000..e70ad4ff --- /dev/null +++ b/Contest-Questions/AtCoder Educational DP/frog2.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; + +#define fo(i, n) for(int i=0; i> n >> k; + + long arr[n], temp[n], mi; + fo(i, n) cin >> arr[i]; + + fo(i, n) { + var = i - 1; + mi = INT_MAX; + while (var >= 0 && var >= i - k) { + mi = min(abs(arr[i] - arr[var]) + temp[var], mi); + var--; + } + temp[i] = mi == INT_MAX ? 0 : mi; + } + + cout << temp[n - 1]; + return 1; +} \ No newline at end of file diff --git a/Contest-Questions/AtCoder Educational DP/grid.cpp b/Contest-Questions/AtCoder Educational DP/grid.cpp new file mode 100644 index 00000000..5f6aaf32 --- /dev/null +++ b/Contest-Questions/AtCoder Educational DP/grid.cpp @@ -0,0 +1,18 @@ +#include +using namespace std; +const int MAXN = 1e3 + 7; +const long long MOD = 1e9 + 7; +int h, w, f[MAXN][MAXN]; +char a[MAXN][MAXN]; +int main() { + ios_base::sync_with_stdio(false), cin.tie(0); + cin >> h >> w; + for (int i = 1; i <= h; i++) cin >> (a[i] + 1); + f[0][1] = 1; + for (int i = 1; i <= h; i++) + for (int j = 1; j <= w; j++) + if (a[i][j] != '#') + f[i][j] = (f[i - 1][j] + f[i][j - 1]) % MOD; + cout << f[h][w] << endl; + return 0; +} \ No newline at end of file diff --git a/Contest-Questions/AtCoder Educational DP/kanpsack2.cpp b/Contest-Questions/AtCoder Educational DP/kanpsack2.cpp new file mode 100644 index 00000000..99182819 --- /dev/null +++ b/Contest-Questions/AtCoder Educational DP/kanpsack2.cpp @@ -0,0 +1,47 @@ +#include +using namespace std; + +#define fo(i, n) for(long i=0; i> n >> w; + + vector> item; + fo(i, n) { + cin >> temp1 >> temp2; + item.push_back(make_pair(temp1, temp2)); + total += temp2; + } + + long dp[n + 1][total + 1]; + fo(i, n + 1) fo(j, total + 1) dp[i][j] = 0; + + Fo(i, 1, n + 1) { + Fo(j, 1, total + 1) { + temp1 = item[i - 1].second; + if (j >= temp1) { + if (dp[i - 1][j] == 0) { + dp[i][j] = item[i - 1].first + dp[i - 1][j - temp1]; + } else { + dp[i][j] = min(dp[i - 1][j], item[i - 1].second + dp[i - 1][j - temp1]); + } + } else { + dp[i][j] = dp[i - 1][j]; + } + if (dp[i][j] != 0 && dp[i][j] <= w && temp2 <= j) { + temp1 = dp[i][j]; + temp2 = j; + } + } + } + + // cout << dp[n][total]; + cout << temp2; + + return 0; +} \ No newline at end of file diff --git a/Contest-Questions/AtCoder Educational DP/knapsack.cpp b/Contest-Questions/AtCoder Educational DP/knapsack.cpp new file mode 100644 index 00000000..690fc700 --- /dev/null +++ b/Contest-Questions/AtCoder Educational DP/knapsack.cpp @@ -0,0 +1,40 @@ +#include +using namespace std; + +#define fo(i, n) for(int i=0; i> n >> w; + + long dp[n + 1][w + 1]; + fo(i, n + 1) fo(j, w + 1) dp[i][j] = 0; + + vector> item; + fo(i, n) { + cin >> temp1 >> temp2; + item.push_back(make_pair(temp1, temp2)); + } + + Fo(i, 1, n + 1) { + Fo(j, 1, w + 1) { + temp1 = item[i - 1].first; + if (j >= temp1) { + dp[i][j] = max(dp[i - 1][j], item[i - 1].second + dp[i - 1][j - temp1]); + } else { + dp[i][j] = dp[i - 1][j]; + } + } + } + + cout << dp[n][w]; + // fo(i, n + 1) { + // fo(j, w + 1) cout << dp[i][j] << " "; + // cout << endl; + // } + + return 0; +} \ No newline at end of file diff --git a/Contest-Questions/AtCoder Educational DP/lcs.cpp b/Contest-Questions/AtCoder Educational DP/lcs.cpp new file mode 100644 index 00000000..3e015b43 --- /dev/null +++ b/Contest-Questions/AtCoder Educational DP/lcs.cpp @@ -0,0 +1,47 @@ +#include +using namespace std; + +#define fo(i, n) for(int i=0; i> a >> b; + + int al = a.length(), bl = b.length(), find; + fo(i, al + 1) fo(j, bl + 1) dp[i][j] = 0; + + Fo(i, 1, al + 1) + Fo(j, 1, bl + 1) { + dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); + if (a[i - 1] == b[j - 1]) { + dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1); + } + } + + find = dp[al][bl]; + int i = al, j = bl; + char ans[find + 1]; + ans[find] = '\0'; + + while (i > 0 && j > 0) { + if (a[i - 1] == b[j - 1]) { + ans[find - 1] = a[i - 1]; + find--; + j--; + i--; + } else if (dp[i - 1][j] < dp[i][j - 1]) { + j--; + } else { + i--; + } + } + + cout << ans; + + return 0; +} \ No newline at end of file diff --git a/Contest-Questions/AtCoder Educational DP/longestPath.cpp b/Contest-Questions/AtCoder Educational DP/longestPath.cpp new file mode 100644 index 00000000..f7eac5e4 --- /dev/null +++ b/Contest-Questions/AtCoder Educational DP/longestPath.cpp @@ -0,0 +1,41 @@ +#include +using namespace std; + +#define fo(i, n) for(int i=0; i adj[N]; +int memo[N]; + +int traverse(int a) { + if (visited[a]) return memo[a]; + int ans = 0; + for (int j : adj[a]) { + ans = max(ans, 1 + traverse(j)); + } + visited[a] = true; + return memo[a] = ans; + +} + +int main() { + + int e, v, temp1, temp2; + cin >> v >> e; + + fo(i, e) { + cin >> temp1 >> temp2; + adj[temp1].emplace_back(temp2); + } + temp2 = 0; + + fo(i, v) + temp2 = max(temp2, traverse(i + 1)); + + cout << temp2; + + return 0; +} \ No newline at end of file diff --git a/Contest-Questions/AtCoder Educational DP/stones.cpp b/Contest-Questions/AtCoder Educational DP/stones.cpp new file mode 100644 index 00000000..9165fdb1 --- /dev/null +++ b/Contest-Questions/AtCoder Educational DP/stones.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; + +#define fo(i, n) for(int i=0; i +using namespace std; + +#define fo(i, n) for(int i=0; i> n; + fo(i, n) { + cin >> temp; + val[temp]++; + } + + cout << setprecision(14) << dfs(val[1], val[2], val[3]); + + return 0; +} \ No newline at end of file diff --git a/Contest-Questions/AtCoder Educational DP/vacation.cpp b/Contest-Questions/AtCoder Educational DP/vacation.cpp new file mode 100644 index 00000000..725e8d1d --- /dev/null +++ b/Contest-Questions/AtCoder Educational DP/vacation.cpp @@ -0,0 +1,30 @@ +#include +using namespace std; + +#define fo(i, n) for(int i=0; i> n; + + long arr[n][3], dp[n][3]; + fo(i, n) fo(j, 3) cin >> arr[i][j]; + + fo(i, n) { + if (i == 0) { + dp[i][0] = arr[i][0]; + dp[i][1] = arr[i][1]; + dp[i][2] = arr[i][2]; + } else { + dp[i][0] = arr[i][0] + max(dp[i - 1][1], dp[i - 1][2]); + dp[i][1] = arr[i][1] + max(dp[i - 1][2], dp[i - 1][0]); + dp[i][2] = arr[i][2] + max(dp[i - 1][0], dp[i - 1][1]); + } + } + + cout << max(dp[n - 1][1], max(dp[n - 1][0], dp[n - 1][2])); + + return 0; +} \ No newline at end of file