range queries and one graph
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
// solution from usaco - need to understand this further. my approach was very similar
|
||||
|
||||
typedef long long ll;
|
||||
#define pb push_back
|
||||
#define pi pair<int, int>
|
||||
#define f first
|
||||
#define mp make_pair
|
||||
#define s second
|
||||
|
||||
ll dp[100001][101];
|
||||
const ll MOD = (10e8) + 7;
|
||||
int main() {
|
||||
ios_base::sync_with_stdio(0);
|
||||
cin.tie(0);
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
int arr[n];
|
||||
for (int i = 0; i < n; i++) { cin >> arr[i]; }
|
||||
if (arr[0] == 0) {
|
||||
fill(dp[0], dp[0] + 101, 1);
|
||||
} else {
|
||||
dp[0][arr[0]] = 1;
|
||||
}
|
||||
for (int i = 1; i < n; i++) {
|
||||
if (arr[i] == 0) {
|
||||
for (int j = 1; j <= m; j++) {
|
||||
dp[i][j] += dp[i - 1][j];
|
||||
if (j - 1 > 0) dp[i][j] += dp[i - 1][j - 1];
|
||||
if (j + 1 <= m) dp[i][j] += dp[i - 1][j + 1];
|
||||
dp[i][j] %= MOD;
|
||||
}
|
||||
} else {
|
||||
dp[i][arr[i]] += dp[i - 1][arr[i]];
|
||||
if (arr[i] - 1 > 0) dp[i][arr[i]] += dp[i - 1][arr[i] - 1];
|
||||
if (arr[i] + 1 <= m) dp[i][arr[i]] += dp[i - 1][arr[i] + 1];
|
||||
dp[i][arr[i]] %= MOD;
|
||||
}
|
||||
}
|
||||
ll ans = 0;
|
||||
for (int i = 1; i <= m; i++) {
|
||||
ans += dp[n - 1][i];
|
||||
ans %= MOD;
|
||||
}
|
||||
cout << ans;
|
||||
}
|
||||
24
CSES - CSES Problem Set/Dynamic_Programming/Book_Shop.cpp
Normal file
24
CSES - CSES Problem Set/Dynamic_Programming/Book_Shop.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
// Book Shop
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
int n, x; cin >> n >> x;
|
||||
vector<int> price(n);
|
||||
vector<int> pages(n);
|
||||
for (auto &e: price) cin >> e;
|
||||
for (auto &e: pages) cin >> e;
|
||||
|
||||
int dp[1000001];
|
||||
for (int i{}; i < n; i++) {
|
||||
for (int j = x; j >= price[i]; j--) {
|
||||
dp[j] = max(dp[j], dp[j - price[i]] + pages[i]);
|
||||
}
|
||||
}
|
||||
|
||||
cout << dp[x] << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// Coin Combinations I
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
using ll = long long;
|
||||
|
||||
const int MOD = 1e9 + 7;
|
||||
ll dp[(int)1e6 + 1];
|
||||
|
||||
int main() {
|
||||
int n; int x; cin >> n >> x;
|
||||
dp[0] = 1;
|
||||
vector<int> in(n);
|
||||
for (int i = 0; i < n; i++) cin >> in[i];
|
||||
for (int i = 0; i <= x; ++i) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
if (in[j] <= i) {
|
||||
dp[i] += dp[i - in[j]];
|
||||
dp[i] %= MOD;
|
||||
}
|
||||
}
|
||||
}
|
||||
cout << dp[x] << endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// Coin Combinations II
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
using ll = long long;
|
||||
|
||||
const int MOD = 1e9 + 7;
|
||||
ll dp[(int)1e6 + 1];
|
||||
|
||||
int main() {
|
||||
dp[0] = 1;
|
||||
int n, x;
|
||||
cin >> n >> x;
|
||||
for (int i{}; i < n; i++) {
|
||||
int c; cin >> c;
|
||||
for (int j = c; j <= x; j++) {
|
||||
dp[j] += dp[j-c];
|
||||
dp[j] %= MOD;
|
||||
}
|
||||
}
|
||||
cout << dp[x] << endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// Dice Combinations
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
using ll = long long;
|
||||
|
||||
const ll N = 1e6;
|
||||
const ll MOD = 1e9 + 7;
|
||||
|
||||
ll ways[N + 1];
|
||||
|
||||
int main() {
|
||||
ll n; cin >> n;
|
||||
ways[0] = 1;
|
||||
for (ll i = 1; i <= n; i++) {
|
||||
for (ll j = 1; j <= 6 && j <= i; j++) {
|
||||
ways[i] = (ways[i] + (ways[i - j])) % MOD;
|
||||
}
|
||||
}
|
||||
cout << ways[n];
|
||||
}
|
||||
31
CSES - CSES Problem Set/Dynamic_Programming/Grid_Paths.cpp
Normal file
31
CSES - CSES Problem Set/Dynamic_Programming/Grid_Paths.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
// Grid Paths
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
const int MOD = 1e9 + 7;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
ll dp[1001][1001];
|
||||
|
||||
int main() {
|
||||
int n; cin >> n;
|
||||
vector<string> in(n);
|
||||
for (int i{}; i < n; i++) cin >> in[i];
|
||||
dp[0][0] = in[0][0] != '*';
|
||||
for (int i = 1; i < n; i++) {
|
||||
if (in[0][i] == '*') continue;
|
||||
else dp[0][i] = dp[0][i - 1];
|
||||
}
|
||||
for (int i = 1; i < n; i++) {
|
||||
for (int j{}; j < n; j++) {
|
||||
if (in[i][j] == '*') continue;
|
||||
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
|
||||
dp[i][j] %= MOD;
|
||||
}
|
||||
}
|
||||
cout << dp[n-1][n-1] << endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// Minimizing Coins
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
int n, x;
|
||||
cin >> n >> x;
|
||||
vector<int> dp(x + 1, INT_MAX - 5);
|
||||
dp[0] = 0;
|
||||
for (int i{}; i < n; i++) {
|
||||
int c; cin >> c;
|
||||
for (int j = c; j <= x; j++) {
|
||||
if (j >= c) {
|
||||
dp[j] = min(dp[j - c] + 1, dp[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (dp[x] != INT_MAX - 5) cout << dp[x] << endl;
|
||||
else cout << -1 << endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// Removing Digits
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int dp[(int)1e6 + 1];
|
||||
|
||||
|
||||
int solve(int n) {
|
||||
if (dp[n]) return dp[n];
|
||||
int count = INT_MAX;
|
||||
if (n == 0) return 0;
|
||||
for (int i = 1; i <= 10 * n; i*= 10) {
|
||||
int remove = (n / i) - (n / (10 * i)) * 10;
|
||||
if (!remove) continue;
|
||||
count = min(count, 1 + solve(n - remove));
|
||||
}
|
||||
dp[n] = count;
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
int n; cin >> n;
|
||||
cout << solve(n) << endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user