-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit af1b3fe
Showing
679 changed files
with
14,938 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,10 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
typedef long long ll; | ||
|
||
int32_t main() { | ||
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | ||
int a, b, c; cin >> a >> b >> c; | ||
string s; cin >> s; | ||
cout << a + b + c << ' ' << s; | ||
} |
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,20 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
typedef long long ll; | ||
|
||
int32_t main() { | ||
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | ||
int n, m; cin >> n >> m; | ||
pair<int, int> a[n]; | ||
for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second; | ||
ll dp[n+1][m+1]; | ||
for (int i = 0; i <= n; i++) { | ||
int w = a[i-1].first, v = a[i-1].second; | ||
for (int j = 0; j <= m; j++) { | ||
if (i == 0) dp[i][j] = 0; | ||
else if (j < w) dp[i][j] = dp[i-1][j]; | ||
else dp[i][j] = max(dp[i-1][j], dp[i-1][j-w] + v); | ||
} | ||
} | ||
cout << dp[n][m]; | ||
} |
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,141 @@ | ||
// Program to find maximum sum subarray | ||
// in a given 2D array | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
#define ROW 4 | ||
#define COL 5 | ||
|
||
// Implementation of Kadane's algorithm for | ||
// 1D array. The function returns the maximum | ||
// sum and stores starting and ending indexes | ||
// of the maximum sum subarray at addresses | ||
// pointed by start and finish pointers | ||
// respectively. | ||
int kadane(int* arr, int* start, int* finish, int n) | ||
{ | ||
// initialize sum, maxSum and | ||
int sum = 0, maxSum = INT_MIN, i; | ||
|
||
// Just some initial value to check | ||
// for all negative values case | ||
*finish = -1; | ||
|
||
// local variable | ||
int local_start = 0; | ||
|
||
for (i = 0; i < n; ++i) | ||
{ | ||
sum += arr[i]; | ||
if (sum < 0) | ||
{ | ||
sum = 0; | ||
local_start = i + 1; | ||
} | ||
else if (sum > maxSum) | ||
{ | ||
maxSum = sum; | ||
*start = local_start; | ||
*finish = i; | ||
} | ||
} | ||
|
||
// There is at-least one | ||
// non-negative number | ||
if (*finish != -1) | ||
return maxSum; | ||
|
||
// Special Case: When all numbers | ||
// in arr[] are negative | ||
maxSum = arr[0]; | ||
*start = *finish = 0; | ||
|
||
// Find the maximum element in array | ||
for (i = 1; i < n; i++) | ||
{ | ||
if (arr[i] > maxSum) | ||
{ | ||
maxSum = arr[i]; | ||
*start = *finish = i; | ||
} | ||
} | ||
return maxSum; | ||
} | ||
|
||
// The main function that finds | ||
// maximum sum rectangle in M[][] | ||
void findMaxSum(int M[][COL]) | ||
{ | ||
// Variables to store the final output | ||
int maxSum = INT_MIN, | ||
finalLeft, | ||
finalRight, | ||
finalTop, | ||
finalBottom; | ||
|
||
int left, right, i; | ||
int temp[ROW], sum, start, finish; | ||
|
||
// Set the left column | ||
for (left = 0; left < COL; ++left) { | ||
// Initialize all elements of temp as 0 | ||
memset(temp, 0, sizeof(temp)); | ||
|
||
// Set the right column for the left | ||
// column set by outer loop | ||
for (right = left; right < COL; ++right) { | ||
|
||
// Calculate sum between current left | ||
// and right for every row 'i' | ||
for (i = 0; i < ROW; ++i) | ||
temp[i] += M[i][right]; | ||
|
||
// Find the maximum sum subarray in temp[]. | ||
// The kadane() function also sets values | ||
// of start and finish. So 'sum' is sum of | ||
// rectangle between (start, left) and | ||
// (finish, right) which is the maximum sum | ||
// with boundary columns strictly as left | ||
// and right. | ||
sum = kadane(temp, &start, &finish, ROW); | ||
|
||
// Compare sum with maximum sum so far. | ||
// If sum is more, then update maxSum and | ||
// other output values | ||
if (sum > maxSum) { | ||
maxSum = sum; | ||
finalLeft = left; | ||
finalRight = right; | ||
finalTop = start; | ||
finalBottom = finish; | ||
} | ||
} | ||
} | ||
|
||
// Print final values | ||
cout << "(Top, Left) (" | ||
<< finalTop << ", " | ||
<< finalLeft | ||
<< ")" << endl; | ||
cout << "(Bottom, Right) (" | ||
<< finalBottom << ", " | ||
<< finalRight << ")" << endl; | ||
cout << "Max sum is: " << maxSum << endl; | ||
} | ||
|
||
// Driver Code | ||
int main() | ||
{ | ||
int M[ROW][COL] = { { 1, 2, -1, -4, -20 }, | ||
{ -8, -3, 4, 2, 1 }, | ||
{ 3, 8, 10, 1, 3 }, | ||
{ -4, -1, 1, 7, -6 } }; | ||
|
||
// Function call | ||
findMaxSum(M); | ||
|
||
return 0; | ||
} | ||
|
||
// This code is contributed by | ||
// rathbhupendra |
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,25 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
typedef long long ll; | ||
|
||
int dp[5005][5005][2], c[5005], s[5005]; | ||
|
||
int solve(int n, int k, bool f) { | ||
if (n == 0 && !f) return 0; | ||
if ((n == 0 && f) || k < 0) return -1e9; | ||
if (dp[n][k][f] != -1) return dp[n][k][f]; | ||
if (f) dp[n][k][f] = max(solve(n - 1, k - n, 1) + s[n], solve(n - 1, k - 1, 0) + s[n] - c[n] * c[n - 1]); | ||
else dp[n][k][f] = max(solve(n - 1, k, 0), solve(n, k, 1) - c[n] * c[n + 1]); | ||
return dp[n][k][f]; | ||
} | ||
|
||
int32_t main() { | ||
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | ||
int n, k; cin >> n >> k; | ||
c[0] = c[n + 1] = s[0] = 0; | ||
memset(dp, -1, sizeof(dp)); | ||
for (int i = 1; i <= n; i++) cin >> c[i]; | ||
for (int i = 1; i <= n; i++) cin >> s[i]; | ||
|
||
cout << max(solve(n, k, 0), solve(n, k, 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,22 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
typedef long long ll; | ||
|
||
int32_t main() { | ||
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | ||
int a, b, c, d; cin >> a >> b >> c >> d; | ||
int e[a], f[b], g[c], h[d]; | ||
for (int i = 0; i < a; i++) cin >> e[i]; | ||
for (int i = 0; i < b; i++) cin >> f[i]; | ||
for (int i = 0; i < c; i++) cin >> g[i]; | ||
for (int i = 0; i < d; i++) cin >> h[i]; | ||
|
||
unordered_map<int, pair<int, int>> m; | ||
for (int i = 0; i < a; i++) | ||
for (int j = 0; j < b; j++) | ||
m[e[i] + f[j]] = make_pair(e[i], f[j]); | ||
|
||
for (int i = 0; i < c; i++) | ||
for (int j = 0; j < d; j++) | ||
if (m.find(-g[i]-h[j]) != m.end()) { cout << m[-g[i]-h[j]].first << ' ' << m[-g[i]-h[j]].second << ' ' << g[i] << ' ' << h[j]; return 0; } | ||
} |
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,16 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
typedef long long ll; | ||
|
||
int32_t main() { | ||
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | ||
int n, k, x; cin >> n >> k >> x; | ||
int cur = x; | ||
while (k--) { | ||
string s = to_string(cur); | ||
if (cur % 7 == 0 || s.find('7') < s.length()) cout << "UP!"; | ||
else cout << cur; | ||
cout << "\n"; | ||
cur += 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,19 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
typedef long long ll; | ||
|
||
int32_t main() { | ||
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | ||
int n; cin >> n; | ||
while (n--) { | ||
string s; cin >> s; | ||
stack<char> st; | ||
for (int i = 0; i < s.size(); i++) { | ||
if (s[i] == 'B') { | ||
if (st.empty()) st.push('B'); | ||
else st.pop(); | ||
} else st.push('A'); | ||
} | ||
cout << st.size() << endl; | ||
} | ||
} |
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,23 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
typedef long long ll; | ||
|
||
int32_t main() { | ||
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | ||
int n; cin >> n; | ||
vector<int> v; map<int, int> m; | ||
int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; m[a[i]] = i; } | ||
int cnt = 0; | ||
for (int i = 1; i < n; i++) { | ||
int leng = m[i]; | ||
for (int j = leng - 1; j >= i - 1; j--) { | ||
m[a[j]]++; | ||
swap(a[j], a[j + 1]); | ||
v.push_back(j + 1); | ||
cnt++; | ||
} | ||
} | ||
|
||
cout << cnt << '\n'; | ||
for (const int x : v) cout << x << '\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,31 @@ | ||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
|
||
int V, E, start, A, B; | ||
bool visited[10]; | ||
vector<int> g[10]; | ||
|
||
void dfs(int cur) { | ||
cout << cur << " "; | ||
for (const int &n : g[cur]) { | ||
if (!visited[n]) { | ||
visited[n] = true; | ||
dfs(n); | ||
} | ||
} | ||
} | ||
|
||
|
||
int main() { | ||
cin >> V >> E >> start; | ||
|
||
for (int i = 0; i < E; i++) { | ||
cin >> A >> B; | ||
g[A].push_back(B); | ||
g[B].push_back(A); | ||
} | ||
|
||
visited[start] = true; | ||
dfs(start); | ||
} |
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,39 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
typedef long long ll; | ||
const int MAXN = 100005, LOG = 20; | ||
|
||
int n, q, up[MAXN][LOG], depth[MAXN]; | ||
vector<int> adj[MAXN]; | ||
|
||
void dfs(int v, int p) { | ||
up[v][0] = p; | ||
if (v != 0) depth[v] = depth[p] + 1; | ||
for (int i = 1; i < LOG; i++) | ||
if (depth[v] >= (1 << i)) up[v][i] = up[up[v][i - 1]][i - 1]; | ||
else up[v][i] = -1; | ||
for (const int u : adj[v]) | ||
if (u != p) dfs(u, v); | ||
|
||
} | ||
|
||
int32_t main() { | ||
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | ||
cin >> n; | ||
for (int i = 1; i < n; i++) { | ||
int u, v; cin >> u >> v; | ||
adj[u].push_back(v); | ||
adj[v].push_back(u); | ||
} | ||
|
||
dfs(0, 0); | ||
|
||
cin >> q; | ||
while (q--) { | ||
int x, k; cin >> x >> k; | ||
if (depth[x] < k) { cout << -1 << "\n"; continue; } | ||
for (int i = 0; i < LOG; i++) | ||
if (k & (1 << i)) x = up[x][i]; | ||
cout << x << "\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,14 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
typedef long long ll; | ||
|
||
int32_t main() { | ||
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | ||
int n; cin >> n; | ||
int ans = 0; | ||
while (n--) { | ||
int h, w; cin >> h >> w; | ||
ans = max(ans, h * w); | ||
} | ||
cout << 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,19 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
#define range(it, start, end) for (int it = start; it < end; it++) | ||
#define arrPut(var) for (auto &inVar : var) {cin >> inVar;} | ||
#define arrPrint(var) for (auto outVar : var) {cout << outVar << ' ';} cout << endl | ||
#define setup() ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) | ||
|
||
int32_t main() { | ||
setup(); | ||
int l, q; cin >> l; | ||
int a[l]; arrPut(a); | ||
sort(a, a + l); | ||
cin >> q; | ||
while (q--) { | ||
int n; cin >> n; | ||
int *low = lower_bound(a, a + l, n), *high = upper_bound(a, a + l, n); | ||
cout << "Smaller: " << low - a << ", Greater: " << a + l - high << endl; | ||
} | ||
} |
Oops, something went wrong.