-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee6fcce
commit a4edd06
Showing
11 changed files
with
1,011 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
typedef long long ll; | ||
const int maxN = 11; | ||
const int maxM = 1001; | ||
const ll MOD = 1e9+7; | ||
|
||
int N, M; | ||
ll dp[maxN][maxM][1<<maxN]; | ||
|
||
int main(){ | ||
scanf("%d %d", &N, &M); | ||
dp[N][0][0] = 1; | ||
for(int i = 1; i <= M; i++){ | ||
for(int j = 0; j < (1<<N); j++) | ||
dp[0][i][j<<1] = dp[N][i-1][j]; | ||
|
||
for(int j = 1; j <= N; j++){ | ||
int x = 1<<(j-1); | ||
int y = 1<<j; | ||
for(int mask = 0; mask < (1<<(N+1)); mask++){ | ||
dp[j-1][i][mask] %= MOD; | ||
if((mask&x) && (mask&y)) continue; | ||
if(mask&x) dp[j][i][mask^x] += dp[j-1][i][mask]; | ||
else if(mask&y) dp[j][i][mask^y] += dp[j-1][i][mask]; | ||
else { | ||
dp[j][i][mask^x] += dp[j-1][i][mask]; | ||
dp[j][i][mask^y] += dp[j-1][i][mask]; | ||
} | ||
} | ||
} | ||
} | ||
printf("%lld\n", dp[N][M][0] % MOD); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
typedef long long ll; | ||
const int maxN = 5000; | ||
|
||
int N, x[maxN+1]; | ||
ll p[maxN+1], dp[maxN+1][maxN+1]; | ||
bool found[maxN+1][maxN+1]; | ||
|
||
ll sum(int l, int r){ | ||
return p[r] - p[l-1]; | ||
} | ||
|
||
ll solve(int l, int r){ | ||
if(found[l][r]) return dp[l][r]; | ||
if(l == r) return x[l]; | ||
found[l][r]=true; | ||
return dp[l][r] = max(x[l]+sum(l+1, r)-solve(l+1, r), x[r]+sum(l,r-1)-solve(l,r-1)); | ||
} | ||
|
||
int main(){ | ||
scanf("%d", &N); | ||
for(int i = 1; i <= N; i++){ | ||
scanf("%d", &x[i]); | ||
p[i] = p[i-1] + x[i]; | ||
} | ||
printf("%lld\n", solve(1, N)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <vector> | ||
#include <stack> | ||
#include <algorithm> | ||
|
||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
int largestRectangleArea(vector<int>& heights) { | ||
int ans = 0; | ||
stack<int> stk; | ||
|
||
for (int i = 0; i <= heights.size(); ++i) { | ||
while (!stk.empty() && (i == heights.size() || heights[stk.top()] > heights[i])) { | ||
int h = heights[stk.top()]; | ||
stk.pop(); | ||
int w = stk.empty() ? i : i - stk.top() - 1; | ||
ans = max(ans, h * w); | ||
} | ||
stk.push(i); | ||
} | ||
|
||
return ans; | ||
} | ||
|
||
int maximalRectangle(vector<vector<char>>& matrix) { | ||
if (matrix.empty()) | ||
return 0; | ||
|
||
int ans = 0; | ||
vector<int> hist(matrix[0].size(), 0); | ||
|
||
for (auto& row : matrix) { | ||
for (int i = 0; i < row.size(); ++i) | ||
hist[i] = row[i] == '0' ? 0 : hist[i] + 1; | ||
ans = max(ans, largestRectangleArea(hist)); | ||
} | ||
|
||
return ans; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
class Solution { | ||
public: | ||
bool isPalindrome(int i, int j, string& s) | ||
{ | ||
while (i < j) { | ||
if (s[i] != s[j]) | ||
return false; | ||
i++; | ||
j--; | ||
} | ||
return true; | ||
} | ||
|
||
int solve(int i, int n, string& str, vector<int>& dp) | ||
{ | ||
|
||
if (i == n) | ||
return 0; | ||
|
||
if (dp[i] != -1) | ||
return dp[i]; | ||
|
||
int minCost = INT_MAX; | ||
|
||
for (int j = i; j < n; j++) { | ||
if (isPalindrome(i, j, str)) { | ||
|
||
int cost = 1 + solve(j + 1, n, str, dp); | ||
minCost = min(minCost, cost); | ||
} | ||
} | ||
return dp[i] = minCost; | ||
} | ||
|
||
int palindromicPartition(string str) | ||
{ | ||
int n = str.size(); | ||
vector<int> dp(n, -1); | ||
|
||
return solve(0, n, str, dp) - 1; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
// Function to solve the unbounded knapsack problem | ||
int unboundedKnapsack(int n, int W, vector<int>& val, vector<int>& wt) { | ||
vector<int> cur(W + 1, 0); // Create a vector to store the current DP state | ||
|
||
// Base Condition | ||
for (int i = wt[0]; i <= W; i++) { | ||
cur[i] = (i / wt[0]) * val[0]; // Calculate the maximum value for the first item | ||
} | ||
|
||
for (int ind = 1; ind < n; ind++) { | ||
for (int cap = 0; cap <= W; cap++) { | ||
int notTaken = cur[cap]; // Maximum value without taking the current item | ||
|
||
int taken = INT_MIN; | ||
if (wt[ind] <= cap) | ||
taken = val[ind] + cur[cap - wt[ind]]; // Maximum value by taking the current item | ||
|
||
cur[cap] = max(notTaken, taken); // Store the maximum value in the current DP state | ||
} | ||
} | ||
|
||
return cur[W]; // Return the maximum value considering all items and the knapsack capacity | ||
} | ||
|
||
int main() { | ||
vector<int> wt = {2, 4, 6}; // Weight of items | ||
vector<int> val = {5, 11, 13}; // Value of items | ||
int W = 10; // Weight capacity of the knapsack | ||
int n = wt.size(); // Number of items | ||
|
||
// Call the function to calculate and output the maximum value the thief can steal | ||
cout << "The Maximum value of items the thief can steal is " << unboundedKnapsack(n, W, val, wt) << endl; | ||
|
||
return 0; // Return 0 to indicate successful program execution | ||
} | ||
|
||
// Refer to this link for detailed explanation - https://www.youtube.com/watch?v=mO8XpGoJwuo |
202 changes: 202 additions & 0 deletions
202
problem-of-the-day/day16/Dynamic_Range_Minimum_Queries.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
#include <algorithm> | ||
#include <bitset> | ||
#include <deque> | ||
#include <iostream> | ||
#include <queue> | ||
#include <set> | ||
#include <stack> | ||
#include <stdlib.h> | ||
#include <unordered_set> | ||
#include <vector> | ||
/// ordered sets support upperbound lowerbound too | ||
#include <map> | ||
#include <unordered_map> | ||
#define len(v) (int)v.size() | ||
#define all(v) v.begin(), v.end() | ||
#define rall(v) v.rbegin(), v.rend() | ||
using namespace std; | ||
#define int long long | ||
#define pb push_back | ||
#define rep(i, a, b) for (int i = a; i < b; i++) | ||
#define f first | ||
#define mk make_pair | ||
#define in insert | ||
#define py cout << "YES" << endl | ||
#define pn cout << "NO" << endl | ||
template <int MOD=998244353> | ||
struct Modular { | ||
int value; | ||
static const int MOD_value = MOD; | ||
|
||
Modular(long long v = 0) { value = v % MOD; if (value < 0) value += MOD;} | ||
Modular(long long a, long long b) : value(0){ *this += a; *this /= b;} | ||
|
||
Modular& operator+=(Modular const& b) {value += b.value; if (value >= MOD) value -= MOD; return *this;} | ||
Modular& operator-=(Modular const& b) {value -= b.value; if (value < 0) value += MOD;return *this;} | ||
Modular& operator*=(Modular const& b) {value = (long long)value * b.value % MOD;return *this;} | ||
|
||
friend Modular mexp(Modular a, long long e) { | ||
Modular res = 1; while (e) { if (e&1) res *= a; a *= a; e >>= 1; } | ||
return res; | ||
} | ||
friend Modular inverse(Modular a) { return mexp(a, MOD - 2); } | ||
|
||
Modular& operator/=(Modular const& b) { return *this *= inverse(b); } | ||
friend Modular operator+(Modular a, Modular const b) { return a += b; } | ||
friend Modular operator-(Modular a, Modular const b) { return a -= b; } | ||
friend Modular operator-(Modular const a) { return 0 - a; } | ||
friend Modular operator*(Modular a, Modular const b) { return a *= b; } | ||
friend Modular operator/(Modular a, Modular const b) { return a /= b; } | ||
friend std::ostream& operator<<(std::ostream& os, Modular const& a) {return os << a.value;} | ||
friend bool operator==(Modular const& a, Modular const& b) {return a.value == b.value;} | ||
friend bool operator!=(Modular const& a, Modular const& b) {return a.value != b.value;} | ||
}; | ||
template <typename T> void chkmin(T &x, T y) { | ||
if (x > y) | ||
x = y; | ||
} | ||
template <typename T> void chkmax(T &x, T y) { | ||
if (x < y) | ||
x = y; | ||
} | ||
|
||
void sigsev(int n, vector<int> &prime) { | ||
prime.push_back(2); | ||
vector<bool>ispm(n + 1, true); | ||
|
||
for(int j = 3; j <= n; j += 2) { | ||
if(!ispm[j])continue; | ||
else{ | ||
prime.push_back(j); | ||
for(int i = j*j; i <= n; i += j) { | ||
ispm[i] = false; | ||
} | ||
} | ||
} | ||
} | ||
|
||
class UF { | ||
public: | ||
int *id, cnt, *sz; | ||
// Create an empty union find data structure with N isolated sets. | ||
UF(int N) { | ||
cnt = N; id = new int[N]; sz = new int[N]; | ||
for (int i = 0; i<N; i++) id[i] = i, sz[i] = 1; } | ||
~UF() { delete[] id; delete[] sz; } | ||
// Return the id of component corresponding to object p. | ||
int find(int p) { | ||
int root = p; | ||
while (root != id[root]) root = id[root]; | ||
while (p != root) { int newp = id[p]; id[p] = root; p = newp; } | ||
return root; | ||
} | ||
// Replace sets containing x and y with their union. | ||
void merge(int x, int y) { | ||
int i = find(x); int j = find(y); if (i == j) return; | ||
// make smaller root point to larger one | ||
if (sz[i] < sz[j]) { id[i] = j, sz[j] += sz[i]; } | ||
else { id[j] = i, sz[i] += sz[j]; } | ||
cnt--; | ||
} | ||
// Are objects x and y in the same set? | ||
bool connected(int x, int y) { return find(x) == find(y); } | ||
// Return the number of disjoint sets. | ||
int count() { return cnt; } | ||
}; | ||
|
||
class SG { | ||
public: | ||
vector<int>data; | ||
int n; | ||
vector<int>tree; | ||
function<int(int, int)> operation; | ||
int default_val; | ||
SG(vector<int>&data , int default_val , function<int(int, int)> op):operation(op) { | ||
this->data = data; | ||
n = data.size(); | ||
this->default_val = default_val; | ||
tree.resize(4*n); | ||
build(0, n-1, 1); | ||
} | ||
void build(int l, int r, int node) { | ||
if(l == r) { | ||
tree[node] = data[l]; | ||
// cout<<l<<" "<<r<<" "<<tree[node]<<endl; | ||
return; | ||
} | ||
int mid = (l+r)/2; | ||
build(l, mid, 2*node); | ||
build(mid+1, r, 2*node+1); | ||
tree[node] = operation(tree[2*node], tree[2*node+1]); | ||
// cout<<l<<" "<<r<<" "<<tree[node]<<endl; | ||
} | ||
int query(int v, int tl, int tr, int l , int r){ | ||
if (l > r) return default_val; | ||
else if (l == tl && r == tr) { | ||
return tree[v]; | ||
} | ||
int tm = (tl + tr) / 2; | ||
return operation(query(v*2, tl, tm, l, min(r, tm)),query(v*2+1, tm+1, tr, max(l, tm+1), r)); | ||
} | ||
int query(int ql, int qr) { | ||
return query(1, 0, n-1, ql, qr); | ||
} | ||
void buildup(int v){ | ||
if(v==0)return; | ||
tree[v] = operation(tree[v*2], tree[v*2+1]); | ||
buildup(v/2); | ||
} | ||
void update(int v, int tl, int tr, int l, int r, int new_val) { | ||
if (l > r) | ||
return; | ||
if (l == tl && r == tr) { | ||
tree[v] = new_val; | ||
} else { | ||
int tm = (tl + tr) / 2; | ||
update(v*2, tl, tm, l, min(r, tm), new_val); | ||
update(v*2+1, tm+1, tr, max(l, tm+1), r, new_val); | ||
buildup(v); | ||
} | ||
} | ||
void update(int l, int r, int new_val){ | ||
update(1, 0, n-1, l, r, new_val); | ||
} | ||
}; | ||
|
||
#ifndef ONLINE_JUDGE | ||
#define debug(x) cout << #x << " = " << x << endl | ||
#define show1(v) \ | ||
for (auto i : v) \ | ||
cout << i << ' ' | ||
#define show(v) \ | ||
show1(v); \ | ||
cout << '\n' | ||
#define gcd __algo_gcd | ||
#else | ||
#define debug(x) | ||
#endif | ||
|
||
|
||
// ------------------------------------------***-------------------------------------------------- | ||
|
||
|
||
int32_t main() { | ||
int n,q; cin>>n>>q; | ||
vector<int>a(n); | ||
rep(i,0,n)cin>>a[i]; | ||
int qq,l,r,k,u; | ||
SG s = SG(a, INT32_MAX, [](int a, int b) { return min(a,b); }); | ||
while(q--){ | ||
cin>>qq; | ||
if(qq==1){ | ||
cin>>k>>u; | ||
s.update(k-1, k-1, u); | ||
} | ||
else if(qq==2){ | ||
cin>>l>>r; | ||
cout<<s.query(l-1,r-1)<<'\n'; | ||
} | ||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.