Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 'AtCoder Educational DP' in Contest-Questions #181

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Contest-Questions/AtCoder Educational DP/chessmetric.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <bits/stdc++.h>
using namespace std;

#define fo(i, n) for(int i=0; i<n; i++)
#define Fo(i, k, n) for(int i=k; i<n; i++)
#define endl "\n"

int main() {

int t, a, b, n;
long long arr[105][105][50];
cin >> 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;
}
27 changes: 27 additions & 0 deletions Contest-Questions/AtCoder Educational DP/coin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <bits/stdc++.h>
using namespace std;

#define fo(i, n) for(int i=0; i<n; i++)
#define Fo(i, k, n) for(int i=k; i<n; i++)

int main() {

int n;
cin >> 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;
}
18 changes: 18 additions & 0 deletions Contest-Questions/AtCoder Educational DP/deque.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define fo(i, n) for(i=0; i<n; i++)
#define Fo(i, k, n) for(i=k; i<n; i++)

int main() {

int n, i, j;
cin >> 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;
}
23 changes: 23 additions & 0 deletions Contest-Questions/AtCoder Educational DP/frog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <bits/stdc++.h>
using namespace std;

#define fo(i, n) for(int i=0; i<n; i++)
#define Fo(i, k, n) for(int i=k; i<n; i++)
#define endl "\n"

int main() {

int n;
cin >> 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];
}
27 changes: 27 additions & 0 deletions Contest-Questions/AtCoder Educational DP/frog2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <bits/stdc++.h>
using namespace std;

#define fo(i, n) for(int i=0; i<n; i++)
#define endl "\n"

int main() {

int n, k, var;
cin >> 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;
}
18 changes: 18 additions & 0 deletions Contest-Questions/AtCoder Educational DP/grid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <iostream>
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;
}
47 changes: 47 additions & 0 deletions Contest-Questions/AtCoder Educational DP/kanpsack2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <bits/stdc++.h>
using namespace std;

#define fo(i, n) for(long i=0; i<n; i++)
#define Fo(i, k, n) for(long i=k; i<n; i++)
#define endl "\n"

int main() {

long long total = 0;
long n, w, temp1, temp2;
cin >> n >> w;

vector<pair<long, long>> 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;
}
40 changes: 40 additions & 0 deletions Contest-Questions/AtCoder Educational DP/knapsack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <bits/stdc++.h>
using namespace std;

#define fo(i, n) for(int i=0; i<n; i++)
#define Fo(i, k, n) for(int i=k; i<n; i++)
#define endl "\n"

int main() {

int n, w, temp1, temp2;
cin >> n >> w;

long dp[n + 1][w + 1];
fo(i, n + 1) fo(j, w + 1) dp[i][j] = 0;

vector<pair<int, int>> 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;
}
47 changes: 47 additions & 0 deletions Contest-Questions/AtCoder Educational DP/lcs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <bits/stdc++.h>
using namespace std;

#define fo(i, n) for(int i=0; i<n; i++)
#define Fo(i, k, n) for(int i=k; i<n; i++)
#define endl "\n"

int dp[3002][3002];

int main() {

string a, b;
cin >> 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;
}
41 changes: 41 additions & 0 deletions Contest-Questions/AtCoder Educational DP/longestPath.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <bits/stdc++.h>
using namespace std;

#define fo(i, n) for(int i=0; i<n; i++)
#define Fo(i, k, n) for(int i=k; i<n; i++)
#define endl "\n"

const int N = 100005;
bool visited[N];
vector<int> 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;
}
23 changes: 23 additions & 0 deletions Contest-Questions/AtCoder Educational DP/stones.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <bits/stdc++.h>
using namespace std;

#define fo(i, n) for(int i=0; i<n; i++)

int arr[105];
bool winner[100005];

int main() {

int n, k;
scanf("%d %d", &n, &k);

fo(i, n) scanf("%d", &arr[i]);

fo(i, k) if (!winner[i])
fo(j, n) if (i + arr[j] <= k)
winner[i + arr[j]] = true;

cout << (winner[k] ? "First" : "Second");

return 0;
}
33 changes: 33 additions & 0 deletions Contest-Questions/AtCoder Educational DP/sushi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <bits/stdc++.h>
using namespace std;

#define fo(i, n) for(int i=0; i<n; i++)
#define Fo(i, k, n) for(int i=k; i<n; i++)
#define endl "\n"

int n, val[4];
double dp[305][305][305];

double dfs(int a, int b, int c) {
if (a == 0 && b == 0 && c == 0) return 0.0;
else if (dp[a][b][c]) return dp[a][b][c];
double ans = (double)n / (a + b + c);
if (a) ans += (double)a / (a + b + c) * dfs(a - 1, b, c);
if (b) ans += (double)b / (a + b + c) * dfs(a + 1, b - 1, c);
if (c) ans += (double)c / (a + b + c) * dfs(a, b + 1, c - 1);
return dp[a][b][c] = ans;
}

int main() {

int temp;
cin >> n;
fo(i, n) {
cin >> temp;
val[temp]++;
}

cout << setprecision(14) << dfs(val[1], val[2], val[3]);

return 0;
}
Loading