diff --git a/.gdb_history b/.gdb_history new file mode 100644 index 0000000..4b3ede3 --- /dev/null +++ b/.gdb_history @@ -0,0 +1,256 @@ +p b +next +run +next +p pref[i - a] +next +next +p vaar +p var +p pref[i] +p pref[i] - *var.begin() +next +break 94 +run +next +p i - b + 1 +p i - b + 1 +p i +break 94 +run +run +next +p i - b + 1 +p a +p b +p i - b + 1 +p i +p i - b - 1 +break 94 +run +next +next +p ans +p var +p pref[i - a] +break 94 +run +next +p anms +p ans +next +p i - a +p pref[i - a] +p i +next +p ansd +p ans +next +p var +break 127 +ru +p log +next +p log +p l +p r +p r - (1 << log) +sparse +p sparse +break 128 +run +p sparse +continue +run +p sparse[l][log] +p l +p r +break 128 +run +break 154 +run +p seg[0 + fds.size()] +p segl[0 + fds.size()] +p segl[1 + fds.size()] +p segl[1 + fds.size()] +p segl[2 + fds.size()] +p segl[3 + fds.size()] +p segl[4 + fds.size()] +p segl[5 + fds.size()] +next +p i +p fds[i] +p sparsequery(i, i) +p ans +p i +next +p segr[8 + fds.size()] +p segr[7 + fds.size()] +p segr[8 + fds.size()] +p segr[9 + fds.size()] +break 178 +ru +p segr +p segr[5 + fds.size()] +break 138 +run +nextr +next +pi +p i +p i +p i +continue +necxt +nexty +next +continue +next +continue +next +next +p seg[6 + fds.size()] +break 139 +run +continue +next +p ans +break 126 +run +p count +p n +break 109 +run +next +p ans +p count +break 90 +run +next +p sum +next +p i +next +break 110 +run +p sum +p count +break 86 +run +break 87 +run +next +next +p tmp +next +next +p i +p s.top().second +p +p s +next +p s +p tmp +next +p s +p i +p fds[i] +next +break 135 +run +next +next +step +next +break 157 +run +next +p pdis[i +p pdis[i +break 120 +run +p parent +break 121 +run +p reachable +break 121 +run +p reachable +break 121 +ru +run +p reachable +break 94 +run +next +break 25 +break 120 +run +next +p j +next +p res +break 120 +run +ruk +run +next +p i +next +p ans +next +p i +p res +break 183 +run +1 2 +1 3 +p loop +p cyclesize +groupsize +p cyclepos +p groupsize +run +p loop +p groupsize +p groups +p group +p cyclechoice +next +next +p choice +next +p pos[a] +p ((cyclepos[b] - cyclepos[a]) % groupsize[g] + groupsize[g]) % groupsize[g] +p b +p ((cyclepos[b] - cyclepos[choice]) % groupsize[g] + groupsize[g]) % groupsize[g] +next +run +next +next +p j +run +run +continue +next +next +p ff +p F +step +next +p ff +step +break 1154 +break 115 +continue +p f +next +p f +p e.cap - e.flow +p e.cap +p e.flow +p f +p e.cap - e.flow +continue +p v +p f diff --git a/CSES Problem Set/Advertisement.cpp b/CSES Problem Set/Advertisement.cpp new file mode 100644 index 0000000..67c61f7 --- /dev/null +++ b/CSES Problem Set/Advertisement.cpp @@ -0,0 +1,105 @@ +/* Problem URL: https://cses.fi/problemset/task/1142/ */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vl fds(n); + cin >> fds; + + ll ans = 0; + + stack> s; + rep(i, n) { + rmax(ans, fds[i]); + int tmp = i; + while (!s.empty() && s.top().first >= fds[i]) { + rmax(ans, s.top().first * (i - s.top().second)); + rmax(ans, fds[i] * (i - s.top().second + 1)); + tmp = s.top().second; + s.pop(); + } + + s.emplace(fds[i], tmp); + } + + while (!s.empty()) { + rmax(ans, s.top().first * (n - s.top().second)); + s.pop(); + } + + cout << ans << '\n'; +} diff --git a/CSES Problem Set/Apartments.cpp b/CSES Problem Set/Apartments.cpp new file mode 100644 index 0000000..2348462 --- /dev/null +++ b/CSES Problem Set/Apartments.cpp @@ -0,0 +1,124 @@ +/* Problem URL: https://cses.fi/problemset/task/1084 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll n, m, k; + cin >> n >> m >> k; + + vl a(n); + cin >> a; + vl b(m); + cin >> b; + + sortv(a); + sortv(b); + + int i = 0; + int ans = 0; + + repv(now, b) { + int low = i; + int high = a.size() - 1; + + int choice = -1; + + while (low <= high) { + int mid = (low + high) / 2; + + if (a[mid] + k < now) { + low = mid + 1; + continue; + } + + if (a[mid] - k > now) { + high = mid - 1; + continue; + } + + choice = mid; + high = mid - 1; + } + + if (choice == -1) { + continue; + } + + i = choice + 1; + + ans++; + } + + cout << ans << '\n'; +} diff --git a/CSES Problem Set/Apple Division.cpp b/CSES Problem Set/Apple Division.cpp new file mode 100644 index 0000000..948c005 --- /dev/null +++ b/CSES Problem Set/Apple Division.cpp @@ -0,0 +1,101 @@ +/* Problem URL: https://cses.fi/problemset/task/1623 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll ans = INT64_MAX >> 1; + + int n; + cin >> n; + + vl fds(n); + cin >> fds; + + rep(i, (1 << n)) { + ll g1 = 0; + ll g2 = 0; + + rep(j, n) { + if ((i >> j) & 1) { + g1 += fds[j]; + } else { + g2 += fds[j]; + } + } + + rmin(ans, abs(g1 - g2)); + } + + cout << ans << '\n'; +} diff --git a/CSES Problem Set/Array Division.cpp b/CSES Problem Set/Array Division.cpp new file mode 100644 index 0000000..e99dfc0 --- /dev/null +++ b/CSES Problem Set/Array Division.cpp @@ -0,0 +1,110 @@ +/* Problem URL: https://cses.fi/problemset/task/1085 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, k; + cin >> n >> k; + + vl fds(n); + cin >> fds; + + ll low = *max_element(all(fds)); + ll high = INT64_MAX >> 1; + ll ans = INT64_MAX >> 1; + + while (low <= high) { + ll mid = (low + high) / 2; + + int tmp = 1; + ll total = 0; + repv(i, fds) { + if (total + i > mid) { + total = 0; + tmp++; + } + total += i; + } + + if (tmp > k) { + low = mid + 1; + continue; + } + + ans = mid; + high = mid - 1; + } + + cout << ans << '\n'; +} diff --git a/CSES Problem Set/Bit Strings.cpp b/CSES Problem Set/Bit Strings.cpp new file mode 100644 index 0000000..f67fa32 --- /dev/null +++ b/CSES Problem Set/Bit Strings.cpp @@ -0,0 +1,101 @@ +/* Problem URL: https://cses.fi/problemset/task/1617 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +ll mod = 1e9 + 7; + +ll fastpow(ll v, ll p) +{ + ll ans = 1; + ll now = v; + + rep(i, 20) { + if (p & (1 << i)) { + ans *= now; + ans %= mod; + } + + now *= now; + now %= mod; + } + + return ans; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + cout << fastpow(2, n) << '\n'; +} diff --git a/CSES Problem Set/Building Roads.cpp b/CSES Problem Set/Building Roads.cpp new file mode 100644 index 0000000..1fd3128 --- /dev/null +++ b/CSES Problem Set/Building Roads.cpp @@ -0,0 +1,132 @@ +/* Problem URL: https://cses.fi/problemset/task/1666 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vvi graph(n); + while (m--) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + graph[b].push_back(a); + } + + vi group(n); + + int now = 1; + V vis(n, false); + + function dfs = [&](int i, int g){ + vis[i] = true; + + group[i] = g; + + for (auto j : graph[i]) { + if (vis[j]) { + continue; + } + + dfs(j, g); + } + }; + + rep(i, n) { + if (!vis[i]) { + dfs(i, now); + now++; + } + } + + V> ans; + now = 1; + int node = 0; + + nrep(i, 1, n) { + if (group[i] > now) { + ans.emplace_back(node + 1, i + 1); + now = group[i]; + node = i; + } + } + + cout << ans.size() << '\n'; + for (auto [i, j] : ans) { + cout << i << ' ' << j << '\n'; + } +} diff --git a/CSES Problem Set/Building Teams.cpp b/CSES Problem Set/Building Teams.cpp new file mode 100644 index 0000000..4256b0e --- /dev/null +++ b/CSES Problem Set/Building Teams.cpp @@ -0,0 +1,128 @@ +/* Problem URL: https://cses.fi/problemset/task/1668 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vvi graph(n); + while (m--) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + graph[b].push_back(a); + } + + V group(n); + V vis(n); + + function dfs = [&](int i, bool g) { + vis[i] = true; + group[i] = g; + + repv(j, graph[i]) { + if (vis[j]) { + if (group[j] == g) { + return false; + } + continue; + } + + if (!dfs(j, g ^ 1)) { + return false; + } + } + + return true; + }; + + rep(i, n) { + if (vis[i]) { + continue; + } + + if (!dfs(i, false)) { + cout << "IMPOSSIBLE\n"; + return 0; + } + } + + rep(i, n) { + cout << group[i] + 1 << ' '; + } + cout << '\n'; +} diff --git a/CSES Problem Set/Chessboard and Queens.cpp b/CSES Problem Set/Chessboard and Queens.cpp new file mode 100644 index 0000000..0a924b5 --- /dev/null +++ b/CSES Problem Set/Chessboard and Queens.cpp @@ -0,0 +1,109 @@ +/* Problem URL: https://cses.fi/problemset/task/1624 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + V fds(8); + cin >> fds; + + vi perm = {0, 1, 2, 3, 4, 5, 6, 7}; + + int ans = 0; + do { + auto valid = [&](){ + rep(i, 8) { + if (fds[i][perm[i]] == '*') { + return false; + } + rep(j, 8) { + if (i == j) { + continue; + } + + if (abs(i - j) == abs(perm[i] - perm[j])) { + return false; + } + } + } + + return true; + }; + + if (valid()) { + ans++; + } + } while (next_permutation(all(perm))); + + cout << ans << '\n'; +} diff --git a/CSES Problem Set/Coin Collector.cpp b/CSES Problem Set/Coin Collector.cpp new file mode 100644 index 0000000..4bf0459 --- /dev/null +++ b/CSES Problem Set/Coin Collector.cpp @@ -0,0 +1,185 @@ +/* Problem URL: https://cses.fi/problemset/task/1686 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vl val(n); + cin >> val; + + vvi graph(n); + vvi inv(n); + + while (m--) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + inv[b].push_back(a); + } + + vi order; + V vis(n); + + function preorder = [&](int i) { + vis[i] = true; + + for (auto j : graph[i]) { + if (vis[j]) { + continue; + } + + preorder(j); + } + + order.push_back(i); + }; + + rep(i, n) { + if (vis[i]) { + continue; + } + + preorder(i); + } + + reverse(all(order)); + fill(all(vis), false); + + vi group(n); + vl costs; + V> cond_graph; + int g = 0; + + function scc = [&](int i) { + vis[i] = true; + + group[i] = g; + costs[g] += val[i]; + + for (auto j : inv[i]) { + if (vis[j]) { + if (group[j] != group[i]) { + cond_graph[group[j]].insert(group[i]); + } + continue; + } + + scc(j); + } + }; + + repv(i, order) { + if (vis[i]) { + continue; + } + + cond_graph.emplace_back(); + costs.emplace_back(); + scc(i); + g++; + } + + vl memo(g, -1); + + function dfs = [&](int i) { + ll &ans = memo[i]; + if (ans != -1) { + return ans; + } + + ans = costs[i]; + for (auto j : cond_graph[i]) { + rmax(ans, dfs(j) + costs[i]); + } + + return ans; + }; + + ll ans = 0; + rep(i, g) { + rmax(ans, dfs(i)); + } + + cout << ans << '\n'; +} diff --git a/CSES Problem Set/Coin Piles.cpp b/CSES Problem Set/Coin Piles.cpp new file mode 100644 index 0000000..417b474 --- /dev/null +++ b/CSES Problem Set/Coin Piles.cpp @@ -0,0 +1,93 @@ +/* Problem URL: https://cses.fi/problemset/task/1754 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll a, b; + cin >> a >> b; + + if (a < b) { + swap(a, b); + } + + if ((2 * b - a) >= 0 && (2 * b - a) % 3 == 0) { + cout << "YES\n"; + } else { + cout << "NO\n"; + } + } +} diff --git a/CSES Problem Set/Collecting Numbers II.cpp b/CSES Problem Set/Collecting Numbers II.cpp new file mode 100644 index 0000000..63b5929 --- /dev/null +++ b/CSES Problem Set/Collecting Numbers II.cpp @@ -0,0 +1,139 @@ +/* Problem URL: https://cses.fi/problemset/task/2217 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int inf = INT32_MAX >> 1; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vi adj(n + 2, inf); + adj[0] = -1; + adj[n + 1] = n; + + vi nums(n); + cin >> nums; + + ll rounds = 1; + rep(i, n) { + if (adj[nums[i] - 1] == inf) { + rounds++; + } + adj[nums[i]] = i; + } + + vi prev = adj; + + while (m--) { + int a, b; + cin >> a >> b; + a--, b--; + + swap(adj[nums[a]], adj[nums[b]]); + swap(nums[a], nums[b]); + + if (adj[nums[a] - 1] > adj[nums[a]] && prev[nums[a] - 1] < prev[nums[a]]) { + rounds++; + } + if (adj[nums[a] - 1] < adj[nums[a]] && prev[nums[a] - 1] > prev[nums[a]]) { + rounds--; + } + if (nums[b] != nums[a] + 1) { + if (adj[nums[b] - 1] > adj[nums[b]] && prev[nums[b] - 1] < prev[nums[b]]) { + rounds++; + } + if (adj[nums[b] - 1] < adj[nums[b]] && prev[nums[b] - 1] > prev[nums[b]]) { + rounds--; + } + } + if (adj[nums[a] + 1] < adj[nums[a]] && prev[nums[a] + 1] > prev[nums[a]]) { + rounds++; + } + if (adj[nums[a] + 1] > adj[nums[a]] && prev[nums[a] + 1] < prev[nums[a]]) { + rounds--; + } + if (nums[b] != nums[a] - 1) { + if (adj[nums[b] + 1] < adj[nums[b]] && prev[nums[b] + 1] > prev[nums[b]]) { + rounds++; + } + if (adj[nums[b] + 1] > adj[nums[b]] && prev[nums[b] + 1] < prev[nums[b]]) { + rounds--; + } + } + cout << rounds << '\n'; + + swap(prev[nums[a]], prev[nums[b]]); + } +} diff --git a/CSES Problem Set/Collecting Numbers.cpp b/CSES Problem Set/Collecting Numbers.cpp new file mode 100644 index 0000000..56e7fc5 --- /dev/null +++ b/CSES Problem Set/Collecting Numbers.cpp @@ -0,0 +1,93 @@ +/* Problem URL: https://cses.fi/problemset/task/2216 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + V nums(n + 1, false); + nums[0] = true; + ll count = 1; + rep(i, n) { + int num; + cin >> num; + nums[num] = true; + if (!nums[num - 1]) { + count++; + } + } + + cout << count << '\n'; +} diff --git a/CSES Problem Set/Common Divisors.cpp b/CSES Problem Set/Common Divisors.cpp new file mode 100644 index 0000000..520e87e --- /dev/null +++ b/CSES Problem Set/Common Divisors.cpp @@ -0,0 +1,104 @@ +/* Problem URL: https://cses.fi/problemset/task/1081 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int divs[(ll)1e6 + 1]; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + rep(i, n) { + int num; + cin >> num; + + const size_t lim = sqrt(num); + for (size_t j = 1; j <= lim; j++) { + if (num % j == 0) { + divs[j]++; + if (j != num / j) { + divs[num / j]++; + } + } + } + } + + size_t fds = 1; + for (size_t i = 1e6; i >= fds; i--) { + if (divs[i] >= 2) { + cout << i << '\n'; + break; + } + } +} diff --git a/CSES Problem Set/Concert Tickets.cpp b/CSES Problem Set/Concert Tickets.cpp new file mode 100644 index 0000000..cb9e590 --- /dev/null +++ b/CSES Problem Set/Concert Tickets.cpp @@ -0,0 +1,103 @@ +/* Problem URL: https://cses.fi/problemset/task/1091 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + multiset tickets; + + while (n--) { + ll num; + cin >> num; + + tickets.insert(num); + } + + while (m--) { + ll man; + cin >> man; + + auto itr = tickets.upper_bound(man); + if (itr == tickets.begin()) { + cout << "-1\n"; + continue; + } + + itr--; + cout << *itr << '\n'; + tickets.erase(itr); + } +} diff --git a/CSES Problem Set/Counting Rooms.cpp b/CSES Problem Set/Counting Rooms.cpp new file mode 100644 index 0000000..3564947 --- /dev/null +++ b/CSES Problem Set/Counting Rooms.cpp @@ -0,0 +1,112 @@ +/* Problem URL: https://cses.fi/problemset/task/1192 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + V mat(n); + cin >> mat; + + V> vis(n, V(m, false)); + + function floodfill = [&](int i, int j) + { + vis[i][j] = true; + + int add[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}}; + + for (auto &k : add) { + int id = i + k[0]; + int jd = j + k[1]; + + if (id >= 0 && id < n && jd >= 0 && jd < m && !vis[id][jd] && mat[id][jd] != '#') { + floodfill(id, jd); + } + } + }; + + int ans = 0; + rep(i, n) { + rep(j, m) { + if (!vis[i][j] && mat[i][j] == '.') { + ans++; + floodfill(i, j); + } + } + } + + cout << ans << '\n'; +} diff --git a/CSES Problem Set/Course Schedule.cpp b/CSES Problem Set/Course Schedule.cpp new file mode 100644 index 0000000..d20ede7 --- /dev/null +++ b/CSES Problem Set/Course Schedule.cpp @@ -0,0 +1,128 @@ +/* Problem URL: https://cses.fi/problemset/task/1679 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vvi graph(n); + + while (m--) { + int a, b; + cin >> a >> b; + graph[a - 1].push_back(b - 1); + } + + V frame(n); + V vis(n); + + vi ans; + + function dfs = [&](int i) { + vis[i] = true; + frame[i] = true; + + for (auto j : graph[i]) { + if (frame[j]) { + return true; + } + + if (vis[j]) { + continue; + } + + if (dfs(j)) { + return true; + } + } + + ans.push_back(i + 1); + + frame[i] = false; + return false; + }; + + rep(i, n) { + if (!vis[i]) { + if (dfs(i)) { + cout << "IMPOSSIBLE\n"; + return 0; + } + } + } + + reverse(all(ans)); + cout << ans; +} diff --git a/CSES Problem Set/Creating Strings II.cpp b/CSES Problem Set/Creating Strings II.cpp new file mode 100644 index 0000000..be841c2 --- /dev/null +++ b/CSES Problem Set/Creating Strings II.cpp @@ -0,0 +1,123 @@ +/* Problem URL: https://cses.fi/problemset/task/1715 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + string a; + cin >> a; + + const ll mod = 1e9 + 7; + + vl count(26); + repv(i, a) { + count[i - 'a']++; + } + + auto fpow = [&](ll a, ll p) { + ll ans = 1; + ll now = a; + + rep(i, 30) { + if ((p >> i) & 1) { + ans *= now; + ans %= mod; + } + + now *= now; + now %= mod; + } + + return ans; + }; + + vl fact(a.size() + 1); + fact[0] = 1; + nrep(i, 1, a.size() + 1) { + fact[i] = (fact[i - 1] * i) % mod; + } + + ll ans = fact[a.size()]; + + rep(i, 26) { + ans = (ans * fpow(fact[count[i]], mod - 2)) % mod; + } + + cout << ans << '\n'; +} diff --git a/CSES Problem Set/Creating Strings.cpp b/CSES Problem Set/Creating Strings.cpp new file mode 100644 index 0000000..eb67937 --- /dev/null +++ b/CSES Problem Set/Creating Strings.cpp @@ -0,0 +1,93 @@ +/* Problem URL: https://cses.fi/problemset/task/1622 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + string a; + cin >> a; + + sortv(a); + + V fds; + + do { + fds.emplace_back(a); + } while (next_permutation(all(a))); + + cout << fds.size() << '\n'; + + repv(i, fds) { + cout << i << '\n'; + } +} diff --git a/CSES Problem Set/Cut and Paste.cpp b/CSES Problem Set/Cut and Paste.cpp new file mode 100644 index 0000000..4f4afee --- /dev/null +++ b/CSES Problem Set/Cut and Paste.cpp @@ -0,0 +1,78 @@ +/* Problem URL: https://cses.fi/problemset/task/2072 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + +} diff --git a/CSES Problem Set/Cycle Finding.cpp b/CSES Problem Set/Cycle Finding.cpp new file mode 100644 index 0000000..7bfad5c --- /dev/null +++ b/CSES Problem Set/Cycle Finding.cpp @@ -0,0 +1,130 @@ +/* Problem URL: https://cses.fi/problemset/task/1197 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + V> edges; + while (m--) { + int a, b, c; + cin >> a >> b >> c; + a--, b--; + + edges.emplace_back(a, b, c); + } + + vl dis(n, 0); + vi parent(n, -1); + + rep(i, n - 1) { + for (auto [a, b, c] : edges) { + if (dis[b] > dis[a] + c) { + dis[b] = dis[a] + c; + parent[b] = a; + } + } + } + + int now = -1; + for (auto [a, b, c] : edges) { + if (dis[b] > dis[a] + c) { + parent[b] = a; + now = b; + break; + } + } + + if (now == -1) { + cout << "NO\n"; + return 0; + } + + rep(i, n) { + now = parent[now]; + } + + vi path = {now + 1}; + + int tmp = parent[now]; + while (tmp != now) { + path.push_back(tmp + 1); + tmp = parent[tmp]; + } + + path.push_back(now + 1); + reverse(all(path)); + cout << "YES\n" << path; +} diff --git a/CSES Problem Set/De Bruijn Sequence.cpp b/CSES Problem Set/De Bruijn Sequence.cpp new file mode 100644 index 0000000..3810bfc --- /dev/null +++ b/CSES Problem Set/De Bruijn Sequence.cpp @@ -0,0 +1,113 @@ +/* Problem URL: https://cses.fi/problemset/task/1692 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +#define INF INT64_MAX >> 1 + +vector de_brujin(int n, int k, int lim = INF) { + if (k == 1) return vector(lim == INF ? 1 : n, 0); + vector l = {0}, ret; // l eh lyndon word + while (true) { + if (l.size() == 0) { + if (lim == INF) break; + l.push_back(0); + } + if (n % l.size() == 0) for (int i : l) { + ret.push_back(i); + if (ret.size() == n+lim-1) return ret; + } + int p = l.size(); + while (l.size() < n) l.push_back(l[l.size()%p]); + while (l.size() and l.back() == k-1) l.pop_back(); + if (l.size()) l.back()++; + } + return ret; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + auto ans = de_brujin(n, 2, 1 << n); + repv(i, ans) { + cout << i; + } + cout << '\n'; +} diff --git a/CSES Problem Set/Digit Queries.cpp b/CSES Problem Set/Digit Queries.cpp new file mode 100644 index 0000000..4ae2326 --- /dev/null +++ b/CSES Problem Set/Digit Queries.cpp @@ -0,0 +1,99 @@ +/* Problem URL: https://cses.fi/problemset/task/2431 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int q; + cin >> q; + while (q--) { + ll k; + cin >> k; + k--; + + ll now = 1; + int digs = 1; + while (k >= digs * now * 9) { + k -= digs * now * 9; + now *= 10; + digs++; + } + + ll act = now; + act += (k / digs); + k %= digs; + + string fds = to_string(act); + cout << fds[k] << '\n'; + } +} diff --git a/CSES Problem Set/Distinct Colors.cpp b/CSES Problem Set/Distinct Colors.cpp new file mode 100644 index 0000000..586866e --- /dev/null +++ b/CSES Problem Set/Distinct Colors.cpp @@ -0,0 +1,127 @@ +/* Problem URL: https://cses.fi/problemset/task/1139 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vi colors(n); + cin >> colors; + + V> c(n); + vi ans(n); + + vvi graph(n); + rep(i, n - 1) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + graph[b].push_back(a); + } + + function dfs = [&](int i, int p) { + c[i].insert(colors[i]); + + for (auto j : graph[i]) { + if (j == p) { + continue; + } + + dfs(j, i); + + if (c[j].size() > c[i].size()) { + swap(c[j], c[i]); + } + + for (auto k : c[j]) { + c[i].insert(k); + } + } + + ans[i] = c[i].size(); + }; + + dfs(0, 0); + + cout << ans; +} diff --git a/CSES Problem Set/Distinct Numbers.cpp b/CSES Problem Set/Distinct Numbers.cpp new file mode 100644 index 0000000..f410c77 --- /dev/null +++ b/CSES Problem Set/Distinct Numbers.cpp @@ -0,0 +1,88 @@ +/* Problem URL: https://cses.fi/problemset/task/1621 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + set fds; + while (n--) { + int f; + cin >> f; + fds.insert(f); + } + + cout << fds.size() << '\n'; +} diff --git a/CSES Problem Set/Distinct Routes.cpp b/CSES Problem Set/Distinct Routes.cpp new file mode 100644 index 0000000..f42b545 --- /dev/null +++ b/CSES Problem Set/Distinct Routes.cpp @@ -0,0 +1,195 @@ +/* Problem URL: https://cses.fi/problemset/task/1711 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vvi graph(n); + V> edges; + + while (m--) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(edges.size()); + edges.emplace_back(b, 1, 0); + graph[b].push_back(edges.size()); + edges.emplace_back(a, 0, 0); + } + + int inf = INT32_MAX >> 1; + + vi level(n); + auto bfs = [&](){ + level[0] = 0; + queue q; + q.push(0); + + while (!q.empty()) { + auto a = q.front(); + q.pop(); + + for (auto id : graph[a]) { + auto [b, c, u] = edges[id]; + + if (level[b] > level[a] + 1 && c != u) { + level[b] = level[a] + 1; + q.push(b); + } + } + } + + return level[n - 1] != inf; + }; + + V vis(n); + + function dfs = [&](int i, ll flux){ + if (flux == 0) { + return 0LL; + } + + if (i == n - 1) { + return flux; + } + + for (auto id : graph[i]) { + auto &[j, c, u] = edges[id]; + + if (level[j] <= level[i] || vis[j]) { + continue; + } + + ll ans = dfs(j, min(flux, c - u)); + + if (ans == 0) { + continue; + } + + auto &[_, __, ud] = edges[id ^ 1]; + + u += ans; + ud -= ans; + + return ans; + } + + vis[i] = true; + return 0LL; + }; + + ll ans = 0; + while (true) { + fill(all(level), inf); + + if (!bfs()) { + break; + } + + fill(all(vis), false); + + ll count = 0; + while ((count = dfs(0, inf))) { + ans += count; + } + } + + cout << ans << '\n'; + + while (ans--) { + vi tmp; + + function dfs = [&](int i){ + tmp.push_back(i + 1); + + for (auto id : graph[i]) { + auto &[j, c, u] = edges[id]; + + if (u == 1) { + u--; + dfs(j); + return; + } + } + }; + + dfs(0); + cout << tmp.size() << '\n'; + cout << tmp; + tmp.clear(); + } +} diff --git a/CSES Problem Set/Distinct Values Queries.cpp b/CSES Problem Set/Distinct Values Queries.cpp new file mode 100644 index 0000000..a9883e1 --- /dev/null +++ b/CSES Problem Set/Distinct Values Queries.cpp @@ -0,0 +1,176 @@ +/* Problem URL: https://cses.fi/problemset/task/1734 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, q; + cin >> n >> q; + + vl tmp(n); + cin >> tmp; + vl fds(n); + set nums; + rep(i, n) { + nums.insert(tmp[i]); + } + + int v = 0; + map var; + repv(i, nums) { + var[i] = v; + v++; + } + + rep(i, n) { + fds[i] = var[tmp[i]]; + } + + int len = (int)sqrt(n) + 1; + + vi prev(n, -1); + vi next(n, n); + vl blocks(len, 0); + V uniq(n, false); + + rep(i, n) { + if (prev[fds[i]] == -1) { + uniq[i] = true; + prev[fds[i]] = i; + blocks[i / len]++; + continue; + } + + next[prev[fds[i]]] = i; + prev[fds[i]] = i; + } + + V> query(q); + size_t act = 0; + for (auto &[l, r, i] : query) { + cin >> l >> r; + l--, r--; + i = act; + act++; + } + + sortv(query); + + int ql = 0; + vl ans(q); + for (auto [l, r, i] : query) { + while (ql < l) { + blocks[ql / len]--; + uniq[next[ql]] = true; + blocks[next[ql] / len]++; + ql++; + } + + if (l / len == r / len) { + ll sum = 0; + for (size_t i = l; i <= r; i++) { + if (uniq[i]) { + sum++; + } + } + ans[i] = sum; + continue; + } + + ll sum = 0; + int bl = l / len; + int br = r / len; + + for (int i = l, end = (bl + 1) * len - 1; i <= end; i++) { + if (uniq[i]) { + sum++; + } + } + + for (int i = bl + 1; i <= br - 1; i++) { + sum += blocks[i]; + } + + for (int i = br * len; i <= r; i++) { + if (uniq[i]) { + sum++; + } + } + + ans[i] = sum; + } + + rep(i, q) { + cout << ans[i] << '\n'; + } +} diff --git a/CSES Problem Set/Distinct Values Subarrays.cpp b/CSES Problem Set/Distinct Values Subarrays.cpp new file mode 100644 index 0000000..dbd28a5 --- /dev/null +++ b/CSES Problem Set/Distinct Values Subarrays.cpp @@ -0,0 +1,102 @@ +/* Problem URL: https://cses.fi/problemset/task/3420 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + set seen; + vl fds(n); + cin >> fds; + + int i = 0; + int j = 0; + + ll ans = 0; + + while (i < n) { + while (seen.count(fds[i])) { + seen.erase(fds[j]); + j++; + } + + seen.insert(fds[i]); + i++; + + ans += i - j; + } + + cout << ans << '\n'; +} diff --git a/CSES Problem Set/Distinct Values Subsequences.cpp b/CSES Problem Set/Distinct Values Subsequences.cpp new file mode 100644 index 0000000..d9d63f6 --- /dev/null +++ b/CSES Problem Set/Distinct Values Subsequences.cpp @@ -0,0 +1,101 @@ +/* Problem URL: https://cses.fi/problemset/task/3421 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + map var; + while (n--) { + ll num; + cin >> num; + var[num]++; + } + + ll mod = 1e9 + 7; + ll ans = 0; + + ll now = 0; + + repv(i, var) { + ans += i.second; + ans += i.second * now; + ans %= mod; + + now = (now * i.second + now + i.second) % mod; + } + + cout << ans << '\n'; +} diff --git a/CSES Problem Set/Distributing Apples.cpp b/CSES Problem Set/Distributing Apples.cpp new file mode 100644 index 0000000..ea52ef7 --- /dev/null +++ b/CSES Problem Set/Distributing Apples.cpp @@ -0,0 +1,117 @@ +/* Problem URL: https://cses.fi/problemset/task/1716 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + const ll mod = 1e9 + 7; + + vl fact(2e6 + 1); + fact[0] = 1; + + nrep(i, 1, fact.size()) { + fact[i] = (fact[i - 1] * i) % mod; + } + + auto fpow = [&](ll a, ll b) { + ll ans = 1; + ll now = a; + + rep(i, 30) { + if ((b >> i) & 1) { + ans *= now; + ans %= mod; + } + + now *= now; + now %= mod; + } + + return ans; + }; + + auto comb = [&](ll n, ll k) { + return (fact[n] * fpow((fact[k] * fact[n - k]) % mod, mod - 2)) % mod; + }; + + cout << comb(n + m - 1, m) << '\n'; +} diff --git a/CSES Problem Set/Download Speed.cpp b/CSES Problem Set/Download Speed.cpp new file mode 100644 index 0000000..c68f294 --- /dev/null +++ b/CSES Problem Set/Download Speed.cpp @@ -0,0 +1,173 @@ +/* Problem URL: https://cses.fi/problemset/task/1694 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vvi graph(n); + V> edges; + + while (m--) { + ll a, b, c; + cin >> a >> b >> c; + a--, b--; + + graph[a].push_back(edges.size()); + edges.emplace_back(b, c, 0); + graph[b].push_back(edges.size()); + edges.emplace_back(a, 0, 0); + } + + int inf = INT32_MAX >> 1; + auto flux = [&](){ + vi level(n); + + auto bfs = [&](){ + queue q; + level[0] = 0; + q.push(0); + + + while (!q.empty()) { + int i = q.front(); + q.pop(); + + for (auto id : graph[i]) { + auto [j, c, u] = edges[id]; + if (level[i] + 1 < level[j] && c != u) { + level[j] = level[i] + 1; + q.push(j); + } + } + } + + return level[n - 1] != inf; + }; + + V vis(n); + + function dfs = [&](int i, ll flux){ + if (flux == 0) { + return 0LL; + } + + if (i == n - 1) { + return flux; + } + + for (auto id : graph[i]) { + auto &[j, c, u] = edges[id]; + + if (level[i] >= level[j] || vis[j]) { + continue; + } + + ll ans = dfs(j, min(flux, c - u)); + + if (ans == 0) { + continue; + } + + auto &[_, __, ud] = edges[id ^ 1]; + + u += ans; + ud -= ans; + return ans; + } + + vis[i] = true; + return 0LL; + }; + + ll ans = 0; + while (true) { + fill(all(level), inf); + if (!bfs()) { + break; + } + + fill(all(vis), false); + ll pushed = 0; + while ((pushed = dfs(0, inf))) { + ans += pushed; + } + } + + return ans; + }; + + cout << flux() << '\n'; +} diff --git a/CSES Problem Set/Exponentiation II.cpp b/CSES Problem Set/Exponentiation II.cpp new file mode 100644 index 0000000..260c358 --- /dev/null +++ b/CSES Problem Set/Exponentiation II.cpp @@ -0,0 +1,103 @@ +/* Problem URL: https://cses.fi/problemset/task/1712 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +ll fastpow(ll n, ll p, ll mod) +{ + ll total = n; + ll ans = 1; + + for (size_t i = 0; i < 30; i++) { + if (p & (1 << i)) { + ans *= total; + ans %= mod; + } + total *= total; + total %= mod; + } + + return ans; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int q; + cin >> q; + while (q--) { + ll a, b, c; + cin >> a >> b >> c; + + ll p = fastpow(b, c, 1e9 + 6); + cout << fastpow(a, p, 1e9 + 7) << '\n'; + } +} diff --git a/CSES Problem Set/Exponentiation.cpp b/CSES Problem Set/Exponentiation.cpp new file mode 100644 index 0000000..31ec329 --- /dev/null +++ b/CSES Problem Set/Exponentiation.cpp @@ -0,0 +1,108 @@ +/* Problem URL: https://cses.fi/problemset/task/1095 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +ll m = 1e9 + 7; + +ll fastpow(ll a, ll p) +{ + if (p == 0) { + return 1; + } + + ll ans = 1; + ll total = a; + for (size_t i = 0; i < 30; i++) { + if (p & (1 << i)) { + ans *= total; + ans %= m; + } + total *= total; + total %= m; + } + + return ans; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + while (n--) { + ll a, b; + cin >> a >> b; + + cout << fastpow(a, b) << '\n'; + } +} diff --git a/CSES Problem Set/Factory Machines.cpp b/CSES Problem Set/Factory Machines.cpp new file mode 100644 index 0000000..b8f729f --- /dev/null +++ b/CSES Problem Set/Factory Machines.cpp @@ -0,0 +1,108 @@ +/* Problem URL: https://cses.fi/problemset/task/1620 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll n, t; + cin >> n >> t; + + vl machines(n); + cin >> machines; + + ll ans = 0; + ll low = 0; + ll high = INT64_MAX; + + while (low <= high) { + ll mid = (high - low) / 2 + low; + + ll sum = 0; + repv(i, machines) { + sum += mid / i; + if (sum >= t) { + break; + } + } + + if (sum >= t) { + ans = mid; + high = mid - 1; + continue; + } + + low = mid + 1; + } + + cout << ans << '\n'; +} diff --git a/CSES Problem Set/Ferris Wheel.cpp b/CSES Problem Set/Ferris Wheel.cpp new file mode 100644 index 0000000..ed7f17b --- /dev/null +++ b/CSES Problem Set/Ferris Wheel.cpp @@ -0,0 +1,110 @@ +/* Problem URL: https://cses.fi/problemset/task/1090 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll n, x; + cin >> n >> x; + + vl fds(n); + cin >> fds; + + int ans = 0; + ll now = 0; + + sortv(fds); + + int i = 0; + int j = n - 1; + + int get = 0; + + while (i <= j) { + if (i == j) { + ans++; + break; + } + + if (fds[i] + fds[j] <= x) { + i++; + j--; + } else { + j--; + } + + ans++; + } + + cout << ans << '\n'; +} diff --git a/CSES Problem Set/Finding a Centroid.cpp b/CSES Problem Set/Finding a Centroid.cpp new file mode 100644 index 0000000..966c1ca --- /dev/null +++ b/CSES Problem Set/Finding a Centroid.cpp @@ -0,0 +1,128 @@ +/* Problem URL: https://cses.fi/problemset/task/2079 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vvi graph(n); + rep(i, n - 1) { + int a, b; + cin >> a >> b; + + a--, b--; + graph[a].push_back(b); + graph[b].push_back(a); + } + + vi sizes(n); + function calc = [&](int i, int p) { + sizes[i] = 1; + + for (auto j : graph[i]) { + if (j == p) { + continue; + } + + sizes[i] += calc(j, i); + } + + return sizes[i]; + }; + + calc(0, 0); + + function dfs = [&](int i) { + sizes[i] = 1; + int choice = -1; + + for (auto j : graph[i]) { + if (sizes[j] > n / 2) { + choice = j; + continue; + } + + sizes[i] += sizes[j]; + } + + if (choice == -1) { + return i; + } + + return dfs(choice); + }; + + cout << dfs(0) + 1 << '\n'; +} diff --git a/CSES Problem Set/Flight Routes Check.cpp b/CSES Problem Set/Flight Routes Check.cpp new file mode 100644 index 0000000..c423041 --- /dev/null +++ b/CSES Problem Set/Flight Routes Check.cpp @@ -0,0 +1,149 @@ +/* Problem URL: https://cses.fi/problemset/task/1682 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vvi graph(n); + vvi inv(n); + + while (m--) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + inv[b].push_back(a); + } + + V vis(n); + stack s; + + function postorder = [&](int i) { + vis[i] = true; + + for (auto j : graph[i]) { + if (vis[j]) { + continue; + } + + postorder(j); + } + + s.push(i); + }; + + rep(i, n) { + if (vis[i]) { + continue; + } + + postorder(i); + } + + int choice = s.top(); + fill(all(vis), false); + + function dfs = [&](int i) { + vis[i] = true; + + for (auto j : inv[i]) { + if (vis[j]) { + continue; + } + + dfs(j); + } + }; + + dfs(choice); + + rep(i, n) { + if (!vis[i]) { + cout << "NO\n"; + cout << i + 1 << ' ' << choice + 1 << '\n'; + return 0; + } + } + + cout << "YES\n"; +} diff --git a/CSES Problem Set/Flight Routes.cpp b/CSES Problem Set/Flight Routes.cpp new file mode 100644 index 0000000..c222cc8 --- /dev/null +++ b/CSES Problem Set/Flight Routes.cpp @@ -0,0 +1,124 @@ +/* Problem URL: https://cses.fi/problemset/task/1196 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m, k; + cin >> n >> m >> k; + + V>> graph(n); + + while (m--) { + int a, b, c; + cin >> a >> b >> c; + a--, b--; + + graph[a].emplace_back(b, c); + } + + ll inf = INT64_MAX >> 1; + V> dis(n, multiset({inf})); + + auto dijkstra = [&]() { + dis[0].insert(0); + priority_queue, V>, greater<>> pq; + pq.emplace(0, 0); + + while (!pq.empty()) { + auto [c, i] = pq.top(); + pq.pop(); + + if (c > *prev(dis[i].end())) { + continue; + } + + for (auto [j, cc] : graph[i]) { + if (dis[j].size() < k || c + cc < *prev(dis[j].end())) { + dis[j].insert(c + cc); + pq.emplace(c + cc, j); + if (dis[j].size() > k) { + dis[j].erase(prev(dis[j].end())); + } + } + } + } + }; + + dijkstra(); + + for (auto i : dis.back()) { + cout << i << ' '; + } + cout << '\n'; +} diff --git a/CSES Problem Set/Forest Queries.cpp b/CSES Problem Set/Forest Queries.cpp new file mode 100644 index 0000000..1fb89b4 --- /dev/null +++ b/CSES Problem Set/Forest Queries.cpp @@ -0,0 +1,136 @@ +/* Problem URL: https://cses.fi/problemset/task/1652 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, q; + cin >> n >> q; + + vvi count(n, vi(n)); + + rep(i, n) { + rep(j, n) { + char c; + cin >> c; + + if (c == '*') { + count[i][j] = 1; + } + } + } + + rep(i, n) { + rep(j, n) { + if (i > 0) { + count[i][j] += count[i - 1][j]; + } + + if (j > 0) { + count[i][j] += count[i][j - 1]; + } + + if (i > 0 && j > 0) { + count[i][j] -= count[i - 1][j - 1]; + } + } + } + + while (q--) { + int x1, y1, x2, y2; + cin >> x1 >> y1 >> x2 >> y2; + x1--, y1--, x2--, y2--; + + int ans = count[x2][y2]; + + if (x1 > 0) { + ans -= count[x1 - 1][y2]; + } + + if (y1 > 0) { + ans -= count[x2][y1 - 1]; + } + + if (x1 > 0 && y1 > 0) { + ans += count[x1 - 1][y1 - 1]; + } + + cout << ans << '\n'; + } +} diff --git a/CSES Problem Set/Game Routes.cpp b/CSES Problem Set/Game Routes.cpp new file mode 100644 index 0000000..b53c662 --- /dev/null +++ b/CSES Problem Set/Game Routes.cpp @@ -0,0 +1,114 @@ +/* Problem URL: https://cses.fi/problemset/task/1681 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vvi graph(n); + + while (m--) { + int a, b; + cin >> a >> b; + + graph[a - 1].push_back(b - 1); + } + + const ll mod = 1e9 + 7; + + vl dp(n, -1); + + function dfs = [&](int i) { + if (i == n - 1) { + dp[i] = 1; + return dp[i]; + } + + if (dp[i] != -1) { + return dp[i]; + } + + dp[i] = 0; + + for (auto j : graph[i]) { + dp[i] += dfs(j); + dp[i] %= mod; + } + + return dp[i]; + }; + + cout << dfs(0) << '\n'; +} diff --git a/CSES Problem Set/Giant Pizza.cpp b/CSES Problem Set/Giant Pizza.cpp new file mode 100644 index 0000000..52b6e46 --- /dev/null +++ b/CSES Problem Set/Giant Pizza.cpp @@ -0,0 +1,207 @@ +/* Problem URL: https://cses.fi/problemset/task/1684 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> m >> n; + + n <<= 1; + + vvi graph(n); + vvi inv(n); + + while (m--) { + char op1, op2; + int a, b; + cin >> op1 >> a >> op2 >> b; + + a--, b--; + + if (op1 == '-') { + a += n >> 1; + } + + if (op2 == '-') { + b += n >> 1; + } + + graph[a].push_back((b + (n >> 1)) % n); + graph[b].push_back((a + (n >> 1)) % n); + inv[(b + (n >> 1)) % n].push_back(a); + inv[(a + (n >> 1)) % n].push_back(b); + } + + V vis(n); + vi top; + + function preorder = [&](int i) { + vis[i] = true; + + for (auto j : graph[i]) { + if (vis[j]) { + continue; + } + + preorder(j); + } + + top.push_back(i); + }; + + rep(i, n) { + if (vis[i]) { + continue; + } + + preorder(i); + } + + reverse(all(top)); + fill(all(vis), false); + + vi group(n); + vi representant(n); + int g = 0; + + function scc = [&](int i) { + vis[i] = true; + group[i] = g; + + for (auto j : inv[i]) { + if (vis[j]) { + continue; + } + + scc(j); + } + }; + + repv(i, top) { + if (vis[i]) { + continue; + } + + scc(i); + representant.push_back(i); + g++; + } + + rep(i, n >> 1) { + if (group[i] == group[i + (n >> 1)]) { + cout << "IMPOSSIBLE\n"; + return 0; + } + } + + fill(all(vis), false); + reverse(all(representant)); + + string ans(n >> 1, ' '); + + function getans = [&](int i) { + vis[i] = true; + + if (ans[i % (n >> 1)] == ' ') { + ans[i % (n >> 1)] = i < (n >> 1) ? '-' : '+'; + } + + repv(j, graph[i]) { + if (vis[j] || group[j] != group[i]) { + continue; + } + + getans(j); + } + }; + + repv(i, representant) { + if (vis[i]) { + continue; + } + + getans(i); + } + + repv(i, ans) { + cout << i << ' '; + } + cout << '\n'; +} diff --git a/CSES Problem Set/Gray Code.cpp b/CSES Problem Set/Gray Code.cpp new file mode 100644 index 0000000..6fd3099 --- /dev/null +++ b/CSES Problem Set/Gray Code.cpp @@ -0,0 +1,89 @@ +/* Problem URL: https://cses.fi/problemset/task/2205 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + rep(i, (1 << n)) { + int act = i ^ (i >> 1); + + for (int j = n - 1; j >= 0; j--) { + int bit = (act >> j) & 1; + cout << (char)(bit + '0'); + } + cout << '\n'; + } +} diff --git a/CSES Problem Set/Grid Coloring I.cpp b/CSES Problem Set/Grid Coloring I.cpp new file mode 100644 index 0000000..4bc74bc --- /dev/null +++ b/CSES Problem Set/Grid Coloring I.cpp @@ -0,0 +1,100 @@ +/* Problem URL: https://cses.fi/problemset/task/3311 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + V fds(n); + cin >> fds; + + V ans(n, string(m, ' ')); + + rep(i, n) { + rep(j, m) { + int now = 0; + char act; + while (act = now + 'A', act == fds[i][j] || (i > 0 && act == ans[i - 1][j]) || (j > 0 && act == ans[i][j - 1])) { + now++; + } + + ans[i][j] = act; + } + } + + repv(i, ans) { + cout << i << '\n'; + } +} diff --git a/CSES Problem Set/Grid Path Description.cpp b/CSES Problem Set/Grid Path Description.cpp new file mode 100644 index 0000000..4234d20 --- /dev/null +++ b/CSES Problem Set/Grid Path Description.cpp @@ -0,0 +1,145 @@ +/* Problem URL: https://cses.fi/problemset/task/1625 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +char a[49]; +bool frame[7][7]; + +int func(int i, int j, int pos) +{ + if (pos >= 48) { + return (i == 6 && j == 0); + } + + if (i == 6 && j == 0) { + return 0; + } + + if ((i == 0 || frame[i - 1][j]) && (i == 6 || frame[i + 1][j]) && (j != 0 && !frame[i][j - 1]) && (j != 6 && !frame[i][j + 1])) { + return 0; + } + + if ((j == 0 || frame[i][j - 1]) && (j == 6 || frame[i][j + 1]) && (i != 0 && !frame[i - 1][j]) && (i != 6 && !frame[i + 1][j])) { + return 0; + } + + frame[i][j] = true; + + if (a[pos] == '?') { + int add[][4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; + int ans = 0; + + repv(k, add) { + int id = i + k[0]; + int jd = j + k[1]; + + if (id >= 0 && id < 7 && jd >= 0 && jd < 7 && !frame[id][jd]) { + ans += func(id, jd, pos + 1); + } + } + + frame[i][j] = false; + return ans; + } + + int id = i; + int jd = j; + switch (a[pos]) { + case 'U': + id--; + break; + case 'D': + id++; + break; + case 'L': + jd--; + break; + case 'R': + jd++; + } + + int ans = 0; + if (id >= 0 && id < 7 && jd >= 0 && jd < 7 && !frame[id][jd]) { + ans = func(id, jd, pos + 1); + } + + frame[i][j] = false; + return ans; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + scanf("%s", a); + + printf("%d\n", func(0, 0, 0)); +} diff --git a/CSES Problem Set/Hamiltonian Flights.cpp b/CSES Problem Set/Hamiltonian Flights.cpp new file mode 100644 index 0000000..eda6273 --- /dev/null +++ b/CSES Problem Set/Hamiltonian Flights.cpp @@ -0,0 +1,121 @@ +/* Problem URL: https://cses.fi/problemset/task/1690 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vvi graph(n); + while (m--) { + int a, b; + cin >> a >> b; + + graph[a - 1].push_back(b - 1); + } + + const ll mod = 1e9 + 7; + + vvl memo(n, vl(1 << n, -1)); + + function dp = [&](int i, ll mask) -> ll { + if (i == n - 1) { + return mask == 0; + } + + ll &ans = memo[i][mask]; + if (ans != -1) { + return ans; + } + + ans = 0; + + repv(j, graph[i]) { + if (mask & (1 << j)) { + ans += dp(j, mask ^ (1 << j)); + ans %= mod; + } + } + + return ans; + }; + + cout << dp(0, ((1 << n) - 1) ^ 1) << '\n'; +} diff --git a/CSES Problem Set/High Score.cpp b/CSES Problem Set/High Score.cpp new file mode 100644 index 0000000..84d44be --- /dev/null +++ b/CSES Problem Set/High Score.cpp @@ -0,0 +1,147 @@ +/* Problem URL: https://cses.fi/problemset/task/1673 */ + +#include + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + V> edges; + V>> graph(n); + + while (m--) { + int a, b, c; + cin >> a >> b >> c; + a--, b--; + + edges.emplace_back(a, b, -c); + graph[a].emplace_back(b, c); + } + + V reachable(n, false); + V vis(n, false); + + reachable.back() = true; + + function dfs = [&](int i) -> bool { + vis[i] = true; + + for (auto [j, c] : graph[i]) { + if (reachable[j]) { + reachable[i] = true; + } + + if (vis[j]) { + continue; + } + + if (dfs(j)) { + reachable[i] = true; + } + } + + return reachable[i]; + }; + + dfs(0); + + auto bellman = [&]() -> ll { + ll inf = INT64_MAX >> 1; + vl dis(n, inf); + dis[0] = 0; + + rep(i, n - 1) { + for (auto [a, b, c] : edges) { + if (dis[a] < inf) { + rmin(dis[b], dis[a] + c); + } + } + } + + for (auto [a, b, c] : edges) { + if (dis[a] < inf && dis[b] > dis[a] + c) { + if (reachable[b]) { + return -1; + } + dis[b] = dis[a] + c; + } + } + + return -dis.back(); + }; + + cout << bellman() << '\n'; +} diff --git a/CSES Problem Set/Increasing Subsequence II.cpp b/CSES Problem Set/Increasing Subsequence II.cpp new file mode 100644 index 0000000..1ff4f92 --- /dev/null +++ b/CSES Problem Set/Increasing Subsequence II.cpp @@ -0,0 +1,131 @@ +/* Problem URL: https://cses.fi/problemset/task/1748 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + map var; + vi fds(n); + repv(i, fds) { + cin >> i; + var[i] = 0; + } + + int v = 0; + repv(i, var) { + i.second = v; + v++; + } + + repv(i, fds) { + i = var[i]; + } + + while (__builtin_popcount(fds.size()) != 1) { + fds.push_back(0); + } + + vl seg(fds.size() * 2); + ll mod = 1e9 + 7; + + auto update = [&](int i, ll v) { + seg[i + fds.size()] += v; + seg[i + fds.size()] %= mod; + for (i = (i + fds.size()) / 2; i > 0; i /= 2) { + seg[i] = (seg[i * 2] + seg[i * 2 + 1]) % mod; + } + }; + + function search = [&](int i, int l, int r, int tl, int tr) -> ll { + if (l > tr || r < tl) { + return 0; + } + + if (l >= tl && r <= tr) { + return seg[i]; + } + + int mid = (l + r) / 2; + return (search(i * 2, l, mid, tl, tr) + search(i * 2 + 1, mid + 1, r, tl, tr)) % mod; + }; + + rep(i, n) { + ll tmp = search(1, 0, fds.size() - 1, 0, fds[i] - 1) + 1; + update(fds[i], tmp); + } + + cout << search(1, 0, n - 1, 0, n - 1) << '\n'; +} diff --git a/CSES Problem Set/Investigation.cpp b/CSES Problem Set/Investigation.cpp new file mode 100644 index 0000000..2d26e15 --- /dev/null +++ b/CSES Problem Set/Investigation.cpp @@ -0,0 +1,195 @@ +/* Problem URL: https://cses.fi/problemset/task/1202 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + V>> graph(n); + V>> rev(n); + + while (m--) { + int a, b, c; + cin >> a >> b >> c; + a--, b--; + + graph[a].emplace_back(b, c); + rev[b].emplace_back(a, c); + } + + ll inf = INT64_MAX >> 1; + vl dis(n, inf); + + auto dijkstra = [&]() { + priority_queue, V>, greater<>> pq; + dis[0] = 0; + pq.emplace(0, 0); + + while (!pq.empty()) { + auto [c, i] = pq.top(); + pq.pop(); + + if (c > dis[i]) { + continue; + } + + for (auto [j, cc] : graph[i]) { + if (dis[j] > c + cc) { + dis[j] = c + cc; + pq.emplace(dis[j], j); + } + } + } + + return dis.back(); + }; + + ll ans = dijkstra(); + + vl dp1(n, -1); + + ll mod = 1e9 + 7; + + function dfs1 = [&](int i, ll c) { + if (i == 0) { + dp1[i] = 1; + return dp1[i]; + } + + if (dp1[i] != -1) { + return dp1[i]; + } + + dp1[i] = 0; + + for (auto [j, cc] : rev[i]) { + if (dis[j] == c - cc) { + dp1[i] += dfs1(j, c - cc); + dp1[i] %= mod; + } + } + + return dp1[i]; + }; + + vi dp2(n, -1); + function dfsmax = [&](int i, ll c) { + if (i == 0) { + dp2[i] = 1; + return dp2[i]; + } + + if (dp2[i] != -1) { + return dp2[i]; + } + + dp2[i] = 0; + + for (auto [j, cc] : rev[i]) { + if (dis[j] == c - cc) { + int res = dfsmax(j, c - cc); + if (res > 0) { + rmax(dp2[i], res + 1); + } + } + } + + return dp2[i]; + }; + + vi dp3(n, -1); + function dfsmin = [&](int i, ll c) { + if (i == 0) { + dp3[i] = 1; + return dp3[i]; + } + + if (dp3[i] != -1) { + return dp3[i]; + } + + dp3[i] = INT32_MAX >> 1; + + for (auto [j, cc] : rev[i]) { + if (dis[j] == c - cc) { + rmin(dp3[i], dfsmin(j, c - cc) + 1); + } + } + + return dp3[i]; + }; + + cout << ans << ' ' << dfs1(n - 1, ans) << ' ' << dfsmin(n - 1, ans) - 1 << ' ' << dfsmax(n - 1, ans) - 1 << '\n'; +} diff --git a/CSES Problem Set/Josephus Problem I.cpp b/CSES Problem Set/Josephus Problem I.cpp new file mode 100644 index 0000000..c4cb6c2 --- /dev/null +++ b/CSES Problem Set/Josephus Problem I.cpp @@ -0,0 +1,100 @@ +/* Problem URL: https://cses.fi/problemset/task/2162 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + list children; + rep(i, n) { + children.push_back(i + 1); + } + + auto itr = children.begin(); + while (!children.empty()) { + itr++; + if (itr == children.end()) { + itr = children.begin(); + } + + cout << *itr << ' '; + itr = children.erase(itr); + if (itr == children.end()) { + itr = children.begin(); + } + } + + cout << '\n'; +} diff --git a/CSES Problem Set/Josephus Problem II.cpp b/CSES Problem Set/Josephus Problem II.cpp new file mode 100644 index 0000000..842e50a --- /dev/null +++ b/CSES Problem Set/Josephus Problem II.cpp @@ -0,0 +1,100 @@ +/* Problem URL: https://cses.fi/problemset/task/2163 */ + +#include + +#include +#include + +#include + +using namespace __gnu_pbds; +using namespace std; +typedef tree, rb_tree_tag, tree_order_statistics_node_update> ordered_set; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll n, k; + cin >> n >> k; + + ll now = 0; + ordered_set set; + rep(i, n) { + set.insert(i + 1); + } + + while (!set.empty()) { + now = (now + k) % set.size(); + auto itr = set.find_by_order(now); + cout << *itr << ' '; + set.erase(itr); + } + cout << '\n'; +} diff --git a/CSES Problem Set/Josephus Queries.cpp b/CSES Problem Set/Josephus Queries.cpp new file mode 100644 index 0000000..d24b607 --- /dev/null +++ b/CSES Problem Set/Josephus Queries.cpp @@ -0,0 +1,92 @@ +/* Problem URL: https://cses.fi/problemset/task/2164 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int q; + cin >> q; + while (q--) { + ll n, k; + cin >> n >> k; + + ll count = 1; + while (k > n / 2 + n % 2) { + k -= n / 2 + n % 2; + n /= 2; + count <<= 1; + } + + cout << k * count << '\n'; + } +} diff --git a/CSES Problem Set/Knight Moves Grid.cpp b/CSES Problem Set/Knight Moves Grid.cpp new file mode 100644 index 0000000..0eb16d1 --- /dev/null +++ b/CSES Problem Set/Knight Moves Grid.cpp @@ -0,0 +1,105 @@ +/* Problem URL: https://cses.fi/problemset/task/3217 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + int inf = INT32_MAX >> 1; + vvi dis(n, vi(n, inf)); + + dis[0][0] = 0; + queue> q; + q.emplace(0, 0); + + while (!q.empty()) { + auto [i, j] = q.front(); + q.pop(); + + int add[][2] = {{2, 1}, {-2, 1}, {1, 2}, {-1, 2}, {2, -1}, {1, -2}, {-2, -1}, {-1, -2}}; + + repv(k, add) { + int id = i + k[0]; + int jd = j + k[1]; + + if (id >= 0 && id < n && jd >= 0 && jd < n && dis[id][jd] > dis[i][j] + 1) { + dis[id][jd] = dis[i][j] + 1; + q.emplace(id, jd); + } + } + } + + cout << dis; +} diff --git a/CSES Problem Set/Knight's Tour.cpp b/CSES Problem Set/Knight's Tour.cpp new file mode 100644 index 0000000..aac240b --- /dev/null +++ b/CSES Problem Set/Knight's Tour.cpp @@ -0,0 +1,173 @@ +/* Problem URL: https://cses.fi/problemset/task/1689 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int x, y; + cin >> x >> y; + x--, y--; + + int n = 8; + + vvi ans(n, vi(n)); + + vvi count(n, vi(n)); + + int add[][2] = {{1, 2}, {1, -2}, {-1, 2}, {-1, -2}, {2, 1}, {2, -1}, {-2, 1}, {-2, -1}}; + + auto check = [&](int i, int j) { + return i >= 0 && i < n && j >= 0 && j < n; + }; + + rep(i, n) { + rep(j, n) { + repv(k, add) { + int id = i + k[0]; + int jd = j + k[1]; + + if (check(id, jd)) { + count[id][jd]++; + } + } + } + } + + swap(x, y); + + int oo = INT32_MAX >> 1; + + int curx = x; + int cury = y; + ans[x][y] = 1; + + function dfs = [&](int i, int j) { + if (ans[i][j] == 64) { + return true; + } + + int prev = count[i][j]; + + count[i][j] = oo; + + priority_queue, V>, greater<>> pq; + + repv(k, add) { + int id = i + k[0]; + int jd = j + k[1]; + + if (check(id, jd)) { + count[id][jd]--; + pq.emplace(count[id][jd], id, jd); + } + } + + while (!pq.empty()) { + auto [ty, id, jd] = pq.top(); + pq.pop(); + + if (ty > 8) { + break; + } + + ans[id][jd] = ans[i][j] + 1; + + if (dfs(id, jd)) { + return true; + } + } + + repv(k, add) { + int id = i + k[0]; + int jd = j + k[1]; + + if (check(id, jd)) { + count[id][jd]++; + } + } + + count[i][j] = prev; + + return false; + }; + + dfs(x, y); + + cout << ans; +} diff --git a/CSES Problem Set/Labyrinth.cpp b/CSES Problem Set/Labyrinth.cpp new file mode 100644 index 0000000..09ac246 --- /dev/null +++ b/CSES Problem Set/Labyrinth.cpp @@ -0,0 +1,163 @@ +/* Problem URL: https://cses.fi/problemset/task/1193 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + V fds(n); + cin >> fds; + + int is; + int js; + + rep(i, n) { + rep(j, m) { + if (fds[i][j] == 'A') { + is = i; + js = j; + goto leave; + } + } + } + +leave: + + string ans; + auto bfs = [&](int i, int j) { + queue> q; + q.emplace(i, j); + + while (!q.empty()) { + auto [i, j] = q.front(); + q.pop(); + + int add[][3] = {{0, 1, 0}, {1, 0, 1}, {0, -1, 2}, {-1, 0, 3}}; + + for (auto &k : add) { + int id = i + k[0]; + int jd = j + k[1]; + + if (id >= 0 && id < n && jd >= 0 && jd < m) { + if (fds[id][jd] == 'B') { + char var[] = {'R', 'D', 'L', 'U'}; + ans.push_back(var[k[2]]); + return make_pair(i, j); + } + + if (fds[id][jd] == '.') { + fds[id][jd] = k[2]; + q.emplace(id, jd); + } + } + } + } + + return make_pair(-1, -1); + }; + + auto [i, j] = bfs(is, js); + + if (i == -1) { + cout << "NO\n"; + return 0; + } + + cout << "YES\n"; + + while (fds[i][j] != 'A') { + switch(fds[i][j]) { + case 0: + ans.push_back('R'); + j--; + break; + case 1: + ans.push_back('D'); + i--; + break; + case 2: + ans.push_back('L'); + j++; + break; + case 3: + ans.push_back('U'); + i++; + break; + } + } + + reverse(all(ans)); + cout << ans.size() << '\n' << ans << '\n'; +} diff --git a/CSES Problem Set/List Removals.cpp b/CSES Problem Set/List Removals.cpp new file mode 100644 index 0000000..c535562 --- /dev/null +++ b/CSES Problem Set/List Removals.cpp @@ -0,0 +1,104 @@ +/* Problem URL: https://cses.fi/problemset/task/1749 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + ordered_set> ord; + + rep(i, n) { + int x; + cin >> x; + + ord.insert({i, x}); + } + + while (n--) { + int i; + cin >> i; + i--; + + auto itr = ord.find_by_order(i); + cout << itr->second << " \n"[n == 0]; + ord.erase(itr); + } +} diff --git a/CSES Problem Set/Longest Common Subsequence.cpp b/CSES Problem Set/Longest Common Subsequence.cpp new file mode 100644 index 0000000..f0e0e77 --- /dev/null +++ b/CSES Problem Set/Longest Common Subsequence.cpp @@ -0,0 +1,126 @@ +/* Problem URL: https://cses.fi/problemset/task/3403 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vi a(n); + vi b(m); + cin >> a >> b; + + vvi dp(n + 1, vi(m + 1)); + + nrep(i, 1, n + 1) { + nrep(j, 1, m + 1) { + if (a[i - 1] == b[j - 1]) { + dp[i][j] = dp[i - 1][j - 1] + 1; + continue; + } + + dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); + } + } + + cout << dp[n][m] << '\n'; + // if (dp[n][m] == 0) { + // return 0; + // } + + vi ans(dp[n][m]); + int act = dp[n][m] - 1; + + int i = n; + int j = m; + + while (i > 0 && j > 0) { + if (a[i - 1] == b[j - 1]) { + ans[act] = a[i - 1]; + i--; + j--; + act--; + continue; + } + + if (dp[i - 1][j] > dp[i][j - 1]) { + i--; + continue; + } + + j--; + } + + cout << ans; +} diff --git a/CSES Problem Set/Longest Flight Route.cpp b/CSES Problem Set/Longest Flight Route.cpp new file mode 100644 index 0000000..fc059a1 --- /dev/null +++ b/CSES Problem Set/Longest Flight Route.cpp @@ -0,0 +1,132 @@ +/* Problem URL: https://cses.fi/problemset/task/1680 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vvi graph(n); + + while (m--) { + int a, b; + cin >> a >> b; + + graph[a - 1].push_back(b - 1); + } + + vi dp(n, -1); + + function dfs = [&](int i) { + if (i == n - 1) { + dp[i] = 1; + return dp[i]; + } + + if (dp[i] != -1) { + return dp[i]; + } + + dp[i] = 0; + + for (auto j : graph[i]) { + int res = dfs(j); + if (res > 0) { + rmax(dp[i], res + 1); + } + } + + return dp[i]; + }; + + if (dfs(0) == 0) { + cout << "IMPOSSIBLE\n"; + return 0; + } + + vi ans = {1}; + + int now = 0; + while (now != n - 1) { + for (auto j : graph[now]) { + if (dp[j] == dp[now] - 1) { + ans.push_back(j + 1); + now = j; + break; + } + } + } + + cout << ans.size() << '\n' << ans; +} diff --git a/CSES Problem Set/Mail Delivery.cpp b/CSES Problem Set/Mail Delivery.cpp new file mode 100644 index 0000000..878024e --- /dev/null +++ b/CSES Problem Set/Mail Delivery.cpp @@ -0,0 +1,130 @@ +/* Problem URL: https://cses.fi/problemset/task/1691 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + V>> graph(n); + V used(m); + rep(i, m) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].emplace_back(b, i); + graph[b].emplace_back(a, i); + } + + rep(i, n) { + if (graph[i].size() & 1) { + cout << "IMPOSSIBLE\n"; + return 0; + } + } + + vi ans; + stack euler; + euler.push(0); + while (!euler.empty()) { + int i = euler.top(); + while (!graph[i].empty() && used[graph[i].back().second]) { + graph[i].pop_back(); + } + + if (graph[i].empty()) { + ans.push_back(i + 1); + euler.pop(); + continue; + } + + used[graph[i].back().second] = true; + euler.push(graph[i].back().first); + graph[i].pop_back(); + } + + if (ans.size() != m + 1) { + cout << "IMPOSSIBLE\n"; + return 0; + } + + cout << ans; +} diff --git a/CSES Problem Set/Maximum Subarray Sum II.cpp b/CSES Problem Set/Maximum Subarray Sum II.cpp new file mode 100644 index 0000000..72ee22a --- /dev/null +++ b/CSES Problem Set/Maximum Subarray Sum II.cpp @@ -0,0 +1,104 @@ +/* Problem URL: https://cses.fi/problemset/task/1644 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, a, b; + cin >> n >> a >> b; + + vl fds(n); + cin >> fds; + + vl pref(n + 1); + rep(i, n) { + pref[i + 1] = pref[i] + fds[i]; + } + + ll ans = INT64_MIN >> 1; + + int i = a; + + multiset var; + + while (i <= n) { + if (i - b - 1 >= 0) { + var.erase(var.find(pref[i - b - 1])); + } + var.insert(pref[i - a]); + rmax(ans, pref[i] - *var.begin()); + i++; + } + + cout << ans << '\n'; +} diff --git a/CSES Problem Set/Maximum Subarray Sum.cpp b/CSES Problem Set/Maximum Subarray Sum.cpp new file mode 100644 index 0000000..e67099a --- /dev/null +++ b/CSES Problem Set/Maximum Subarray Sum.cpp @@ -0,0 +1,102 @@ +/* Problem URL: https://cses.fi/problemset/task/1643 */ + +#include + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vl fds(n); + cin >> fds; + + ll now = 0; + ll ans = 0; + ll maximus = INT64_MIN; + repv(i, fds) { + now += i; + rmax(now, 0LL); + + rmax(ans, now); + rmax(maximus, i); + } + + if (maximus < 0) { + cout << maximus << '\n'; + return 0; + } + + cout << ans << '\n'; +} diff --git a/CSES Problem Set/Meet in the Middle.cpp b/CSES Problem Set/Meet in the Middle.cpp new file mode 100644 index 0000000..e16a7b9 --- /dev/null +++ b/CSES Problem Set/Meet in the Middle.cpp @@ -0,0 +1,150 @@ +/* Problem URL: https://cses.fi/problemset/task/1628/ */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + ll s; + cin >> n >> s; + + vl fds(n); + cin >> fds; + + int mid = n / 2; + + vl count; + + function func1 = [&](int i, ll sum) { + if (sum > s) { + return; + } + + if (i >= mid) { + count.push_back(sum); + return; + } + + func1(i + 1, sum); + func1(i + 1, sum + fds[i]); + }; + + ll ans = 0; + vl sum; + vl c; + + function func2 = [&](int i, ll ss) { + if (ss > s) { + return; + } + + if (i >= n - mid) { + // ans += count[s - sum]; + auto itr = lower_bound(all(sum), s - ss); + if (itr == sum.end() || *itr != s - ss) { + return; + } + ans += c[itr - sum.begin()]; + return; + } + + func2(i + 1, ss); + func2(i + 1, ss + fds[i + mid]); + }; + + func1(0, 0); + + sortv(count); + ll prev = count[0]; + int co = 1; + + nrep(i, 1, count.size()) { + if (prev == count[i]) { + co++; + continue; + } + + sum.push_back(prev); + c.push_back(co); + + prev = count[i]; + co = 1; + } + + sum.push_back(prev); + c.push_back(co); + + func2(0, 0); + + cout << ans << '\n'; +} diff --git a/CSES Problem Set/Message Route.cpp b/CSES Problem Set/Message Route.cpp new file mode 100644 index 0000000..ea33df9 --- /dev/null +++ b/CSES Problem Set/Message Route.cpp @@ -0,0 +1,131 @@ +/* Problem URL: https://cses.fi/problemset/task/1667 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vvi graph(n); + while (m--) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + graph[b].push_back(a); + } + + vi p(n, -1); + + auto bfs = [&]() { + p[0] = 0; + queue q; + q.push(0); + + while (!q.empty()) { + int i = q.front(); + q.pop(); + + for (auto j : graph[i]) { + if (p[j] != -1) { + continue; + } + + p[j] = i; + q.push(j); + } + } + }; + + bfs(); + + if (p[n - 1] == -1) { + cout << "IMPOSSIBLE\n"; + return 0; + } + + vi ans; + ans.push_back(n); + int i = p[n - 1]; + while (i != 0) { + ans.push_back(i + 1); + i = p[i]; + } + + ans.push_back(1); + reverse(all(ans)); + + cout << ans.size() << '\n' << ans; +} diff --git a/CSES Problem Set/Mex Grid Construction.cpp b/CSES Problem Set/Mex Grid Construction.cpp new file mode 100644 index 0000000..b7407d9 --- /dev/null +++ b/CSES Problem Set/Mex Grid Construction.cpp @@ -0,0 +1,134 @@ +/* Problem URL: https://cses.fi/problemset/task/3419 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vvi ans(n, vi(n)); + ans[0][0] = 0; + + V>> mexleft(n, V>(n)); + V>> mexup(n, V>(n)); + mexleft[0][0].insert(0); + mexup[0][0].insert(0); + + auto find_mex = [](set &a) { + int now = 0; + auto itr = a.begin(); + + while (itr != a.end() && now == *itr) { + itr++; + now++; + } + + return now; + }; + + rep(i, n) { + rep(j, n) { + if (i == 0 && j == 0) { + continue; + } + + if (i > 0) { + swap(mexup[i][j], mexup[i - 1][j]); + } + + if (j > 0) { + swap(mexleft[i][j], mexleft[i][j - 1]); + } + + set tmp; + repv(k, mexup[i][j]) { + tmp.insert(k); + } + repv(k, mexleft[i][j]) { + tmp.insert(k); + } + + int mex = find_mex(tmp); + ans[i][j] = mex; + mexup[i][j].insert(mex); + mexleft[i][j].insert(mex); + } + } + + rep(i, n) { + rep(j, n) { + cout << ans[i][j] << " \n"[j == n - 1]; + } + } +} diff --git a/CSES Problem Set/Minimal Grid Path.cpp b/CSES Problem Set/Minimal Grid Path.cpp new file mode 100644 index 0000000..23921e3 --- /dev/null +++ b/CSES Problem Set/Minimal Grid Path.cpp @@ -0,0 +1,125 @@ +/* Problem URL: https://cses.fi/problemset/task/3359 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + V fds(n); + cin >> fds; + + string ans(2 * n - 1, 'Z' + 1); + + queue> q; + V> vis(n, V(n, false)); + vis[0][0] = true; + q.emplace(0, 0); + queue> tmp; + + rep(_, n * 2 - 1) { + while (!q.empty()) { + auto [i, j] = q.front(); + q.pop(); + + int pos = i + j; + + rmin(ans[pos], fds[i][j]); + tmp.emplace(i, j); + } + + while (!tmp.empty()) { + auto [i, j] = tmp.front(); + tmp.pop(); + + if (ans[i + j] < fds[i][j]) { + continue; + } + + int add[][2] = {{0, 1}, {1, 0}}; + + for (auto &k : add) { + int id = i + k[0]; + int jd = j + k[1]; + + if (id < n && jd < n && !vis[id][jd]) { + vis[id][jd] = true; + q.emplace(id, jd); + } + } + } + } + + cout << ans << '\n'; +} diff --git a/CSES Problem Set/Missing Coin Sum.cpp b/CSES Problem Set/Missing Coin Sum.cpp new file mode 100644 index 0000000..86ebc4e --- /dev/null +++ b/CSES Problem Set/Missing Coin Sum.cpp @@ -0,0 +1,95 @@ +/* Problem URL: https://cses.fi/problemset/task/2183 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + vl fds(n); + cin >> fds; + sortv(fds); + + if (fds[0] > 1) { + cout << "1\n"; + return 0; + } + + ll sum = 1; + + for (size_t i = 1; i < n && fds[i] <= sum + 1; i++) { + sum += fds[i]; + } + + cout << sum + 1 << '\n'; +} diff --git a/CSES Problem Set/Monsters.cpp b/CSES Problem Set/Monsters.cpp new file mode 100644 index 0000000..0ec7bf9 --- /dev/null +++ b/CSES Problem Set/Monsters.cpp @@ -0,0 +1,196 @@ +/* Problem URL: https://cses.fi/problemset/task/1194 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + V fds(n); + cin >> fds; + + int si, sj; + + int inf = INT32_MAX >> 1; + + queue> mq; + vvi mdis(n, vi(m, inf)); + + rep(i, n){ + rep(j, m) { + if (fds[i][j] == 'A') { + si = i; + sj = j; + continue; + } + + if (fds[i][j] == 'M') { + mq.emplace(i, j); + mdis[i][j] = 0; + } + } + } + + auto mbfs = [&](){ + while (!mq.empty()) { + auto [i, j] = mq.front(); + mq.pop(); + + int add[][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}}; + + repv(k, add) { + int id = i + k[0]; + int jd = j + k[1]; + + if (id >= 0 && id < n && jd >= 0 && jd < m && fds[id][jd] != '#' && mdis[id][jd] > mdis[i][j] + 1) { + mdis[id][jd] = mdis[i][j] + 1; + mq.emplace(id, jd); + } + } + } + }; + + mbfs(); + + vvi pdis(n, vi(m, inf)); + + auto bfs = [&](){ + pdis[si][sj] = 0; + queue> q; + q.emplace(si, sj); + + while (!q.empty()) { + auto [i, j] = q.front(); + q.pop(); + + + int add[][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}}; + + repv(k, add) { + int id = i + k[0]; + int jd = j + k[1]; + + if (id >= 0 && id < n && jd >= 0 && jd < m && fds[id][jd] == '.' && pdis[id][jd] > pdis[i][j] + 1) { + pdis[id][jd] = pdis[i][j] + 1; + q.emplace(id, jd); + } + } + } + }; + + bfs(); + + string ans; + + function dfs = [&](int i, int j) { + if (i < 0 || i >= n || j < 0 || j >= m) { + return true; + } + + if (pdis[i][j] >= mdis[i][j] || fds[i][j] == '#' || fds[i][j] == 'M') { + return false; + } + + int add[][3] = {{0, 1, 'R'}, {0, -1, 'L'}, {-1, 0, 'U'}, {1, 0, 'D'}}; + + repv(k, add) { + int id = i + k[0]; + int jd = j + k[1]; + + if (id >= 0 && id < n && jd >= 0 && jd < m && pdis[id][jd] <= pdis[i][j]) { + continue; + } + + ans.push_back(k[2]); + + if (dfs(id, jd)) { + return true; + } + + ans.pop_back(); + } + + return false; + }; + + if (!dfs(si, sj)) { + cout << "NO\n"; + return 0; + } + + ans.pop_back(); + + cout << "YES\n" << ans.size() << '\n'; + cout << ans << '\n'; +} diff --git a/CSES Problem Set/Mountain Range.cpp b/CSES Problem Set/Mountain Range.cpp new file mode 100644 index 0000000..4a96d9b --- /dev/null +++ b/CSES Problem Set/Mountain Range.cpp @@ -0,0 +1,190 @@ +/* Problem URL: https://cses.fi/problemset/task/3314 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vl fds(n); + cin >> fds; + + while (__builtin_popcount(fds.size()) != 1) { + fds.push_back(0); + } + + vi seg(fds.size() * 2, 1); + + auto update = [&](int i, int v) { + seg[i + fds.size()] = v; + + for (int j = (i + fds.size()) / 2; j > 0; j /= 2) { + seg[j] = max(seg[j * 2], seg[j * 2 + 1]); + } + }; + + function search = [&](int i, int l, int r, int tl, int tr) { + if (l > tr || r < tl) { + return 0; + } + + if (l >= tl && r <= tr) { + return seg[i]; + } + + int mid = (l + r) / 2; + return max(search(i * 2, l, mid, tl, tr), search(i * 2 + 1, mid + 1, r, tl, tr)); + }; + + priority_queue, V>, greater<>> order; + + vvl sparse(n, vl(20)); + rep(i, n) { + sparse[i][0] = fds[i]; + } + + nrep(j, 1, 20) { + for (int i = 0; i + (1 << (j - 1)) < n; i++) { + sparse[i][j] = max(sparse[i][j - 1], sparse[i + (1 << (j - 1))][j - 1]); + } + } + + auto sparsequery = [&](int l, int r) { + if (l == r) { + return sparse[l][0]; + } + + int log = 31 - __builtin_clz(r - l + 1); + return max(sparse[l][log], sparse[r - (1 << log) + 1][log]); + }; + + rep(i, n) { + order.emplace(fds[i], i); + } + + while (!order.empty()) { + int i = order.top().second; + order.pop(); + int ans1 = 0; + int ans2 = 0; + + if (i > 0 && fds[i - 1] < fds[i]) { + int ans = 0; + int low = 0; + int high = i - 1; + + while (low <= high) { + int mid = (low + high) / 2; + + if (sparsequery(mid, i - 1) >= fds[i]) { + ans = mid + 1; + low = mid + 1; + continue; + } + + high = mid - 1; + } + + if (ans != i) { + ans1 = search(1, 0, fds.size() - 1, ans, i - 1); + } + } + + if (i < n - 1 && fds[i + 1] < fds[i]) { + int ans = n - 1; + int low = i + 1; + int high = n - 1; + + while (low <= high) { + int mid = (low + high) / 2; + + if (sparsequery(i + 1, mid) >= fds[i]) { + ans = mid - 1; + high = mid - 1; + continue; + } + + low = mid + 1; + } + + if (ans != i) { + ans2 = search(1, 0, fds.size() - 1, i + 1, ans); + } + } + + update(i, max(ans1, ans2) + 1); + } + + cout << seg[1] << '\n'; +} diff --git a/CSES Problem Set/Movie Festival II.cpp b/CSES Problem Set/Movie Festival II.cpp new file mode 100644 index 0000000..8e9946c --- /dev/null +++ b/CSES Problem Set/Movie Festival II.cpp @@ -0,0 +1,106 @@ +/* Problem URL: https://cses.fi/problemset/task/1632 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, k; + cin >> n >> k; + + V> fds(n); + + repv(i, fds) { + cin >> i.second >> i.first; + } + + sortv(fds); + + multiset ends; + rep(i, k) { + ends.insert(0); + } + + int ans = 0; + repv(i, fds) { + auto itr = ends.upper_bound(i.second); + if (itr == ends.begin()) { + continue; + } + + ans++; + ends.erase(prev(itr)); + ends.insert(i.first); + } + + cout << ans << '\n'; +} diff --git a/CSES Problem Set/Movie Festival.cpp b/CSES Problem Set/Movie Festival.cpp new file mode 100644 index 0000000..5afaec1 --- /dev/null +++ b/CSES Problem Set/Movie Festival.cpp @@ -0,0 +1,101 @@ +/* Problem URL: https://cses.fi/problemset/task/1629 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + V> dura; + + while (n--) { + ll a, b; + cin >> a >> b; + dura.emplace_back(b, a); + } + + sortv(dura); + + int ans = 1; + int nowe = dura[0].first; + + nrep(i, 1, dura.size()) { + if (dura[i].second >= nowe) { + ans++; + nowe = dura[i].first; + } + } + + cout << ans << '\n'; +} diff --git a/CSES Problem Set/Nearest Smaller Values.cpp b/CSES Problem Set/Nearest Smaller Values.cpp new file mode 100644 index 0000000..a6d3ce7 --- /dev/null +++ b/CSES Problem Set/Nearest Smaller Values.cpp @@ -0,0 +1,100 @@ +/* Problem URL: https://cses.fi/problemset/task/1645 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vl fds(n); + cin >> fds; + + stack> s; + + rep(i, fds.size()) { + while (!s.empty() && s.top().first >= fds[i]) { + s.pop(); + } + + if (s.empty()) { + cout << "0 "; + s.emplace(fds[i], i); + continue; + } + + cout << s.top().second + 1 << ' '; + s.emplace(fds[i], i); + } + cout << '\n'; +} diff --git a/CSES Problem Set/Nested Ranges Check.cpp b/CSES Problem Set/Nested Ranges Check.cpp new file mode 100644 index 0000000..2fe3b10 --- /dev/null +++ b/CSES Problem Set/Nested Ranges Check.cpp @@ -0,0 +1,112 @@ +/* Problem URL: https://cses.fi/problemset/task/2168 */ + +#include +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + int prev = n; + + V>> ranges(n); + rep(i, n) { + cin >> ranges[i].second.first >> ranges[i].second.second; + ranges[i].first = i; + } + + sort(all(ranges), [&](pair> &a, pair> &b){ + if (a.second.first == b.second.first) { + return a.second.second > b.second.second; + } + return a.second.first < b.second.first; + }); + + V ans1(prev); + V ans2(prev); + + ll minimus = INT64_MAX; + for (int i = n - 1; i >= 0; i--) { + ans1[ranges[i].first] = ranges[i].second.second >= minimus; + rmin(minimus, ranges[i].second.second); + } + + ll maximus = 0; + rep(i, n) { + ans2[ranges[i].first] = ranges[i].second.second <= maximus; + rmax(maximus, ranges[i].second.second); + } + + cout << ans1 << ans2; +} diff --git a/CSES Problem Set/Nested Ranges Count.cpp b/CSES Problem Set/Nested Ranges Count.cpp new file mode 100644 index 0000000..030bf1b --- /dev/null +++ b/CSES Problem Set/Nested Ranges Count.cpp @@ -0,0 +1,141 @@ +/* Problem URL: https://cses.fi/problemset/task/2169 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + V>> ranges(n); + + map var; + rep(i, n) { + cin >> ranges[i].second.first >> ranges[i].second.second; + ranges[i].first = i; + var[ranges[i].second.first] = 0; + var[ranges[i].second.second] = 0; + } + + int v = 0; + repv(i, var) { + i.second = v; + v++; + } + + repv(i, ranges) { + i.second.first = var[i.second.first]; + i.second.second = var[i.second.second]; + } + + sort(all(ranges), [&](pair> &a, pair> &b){ + if (a.second.first == b.second.first) { + return a.second.second > b.second.second; + } + return a.second.first < b.second.first; + }); + + vi bit(var.size()); + + auto add = [&](int x){ + for (; x < var.size(); x |= (x + 1)) { + bit[x]++; + } + }; + + auto get = [&](int x){ + int ans = 0; + for (; x >= 0; x = (x & (x + 1)) - 1) { + ans += bit[x]; + } + + return ans; + }; + + vi ans1(n); + vi ans2(n); + + for (int i = n - 1; i >= 0; i--) { + ans1[ranges[i].first] = get(ranges[i].second.second); + add(ranges[i].second.second); + } + + fill(all(bit), 0); + + rep(i, n) { + ans2[ranges[i].first] = get(var.size() - 1) - get(ranges[i].second.second - 1); + add(ranges[i].second.second); + } + + cout << ans1 << ans2; +} diff --git a/CSES Problem Set/New Roads Queries.cpp b/CSES Problem Set/New Roads Queries.cpp new file mode 100644 index 0000000..8ad7bca --- /dev/null +++ b/CSES Problem Set/New Roads Queries.cpp @@ -0,0 +1,199 @@ +/* Problem URL: https://cses.fi/problemset/task/2101/ */ + +#include + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m, q; + cin >> n >> m >> q; + + V> edges; + + rep(i, m) { + int a, b; + cin >> a >> b; + a--, b--; + edges.emplace_back(i, a, b); + } + + vi dsu(n); + rep(i, n) { + dsu[i] = i; + } + + function find_p = [&](int i){ + if (dsu[i] == i) { + return i; + } + return dsu[i] = find_p(dsu[i]); + }; + + auto make_pair = [&](int a, int b) { + a = find_p(a); + b = find_p(b); + + if (a == b) { + return false; + } + + dsu[b] = a; + + return true; + }; + + V>> graph(n); + + for (auto [i, a, b] : edges) { + if (make_pair(a, b)) { + graph[a].emplace_back(b, i + 1); + graph[b].emplace_back(a, i + 1); + } + } + + vvi par(n, vi(20, 0)); + vvi maximus(n, vi(20, 0)); + vi depth(n, 0); + + function dfs = [&](int i, int p, int big){ + par[i][0] = p; + maximus[i][0] = big; + depth[i] = depth[p] + 1; + + nrep(j, 1, 20) { + par[i][j] = par[par[i][j - 1]][j - 1]; + maximus[i][j] = max(maximus[i][j - 1], maximus[par[i][j - 1]][j - 1]); + } + + for (auto [u, v] : graph[i]) { + if (u == p) { + continue; + } + + dfs(u, i, v); + } + }; + + auto lca = [&](int a, int b){ + if (depth[a] > depth[b]) { + swap(a, b); + } + + int ans = 0; + + int diff = depth[b] - depth[a]; + + for (int i = 19; i >= 0; i--) { + if (diff & (1 << i)) { + rmax(ans, maximus[b][i]); + b = par[b][i]; + } + } + + if (a == b) { + return ans; + } + + for (int i = 19; i >= 0; i--) { + if (par[a][i] != par[b][i]) { + rmax(ans, maximus[a][i]); + rmax(ans, maximus[b][i]); + a = par[a][i]; + b = par[b][i]; + } + } + + return max({ans, maximus[a][0], maximus[b][0]}); + }; + + rep(i, n) { + if (dsu[i] == i) { + fill(all(par[i]), i); + dfs(i, i, 0); + } + } + + while (q--) { + int a, b; + cin >> a >> b; + a--, b--; + + if (find_p(a) != find_p(b)) { + cout << "-1\n"; + continue; + } + + cout << lca(a, b) << '\n'; + } +} diff --git a/CSES Problem Set/Palindrome Reorder.cpp b/CSES Problem Set/Palindrome Reorder.cpp new file mode 100644 index 0000000..14321a0 --- /dev/null +++ b/CSES Problem Set/Palindrome Reorder.cpp @@ -0,0 +1,121 @@ +/* Problem URL: https://cses.fi/problemset/task/1755 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + string a; + cin >> a; + + vi count(26); + + repv(i, a) { + count[i - 'A']++; + } + + int odds = 0; + int odd = -1; + rep(i, 26) { + if (count[i] & 1) { + odds++; + odd = i; + } + } + + if (odds > 1) { + cout << "NO SOLUTION\n"; + return 0; + } + + if (odd != -1) { + count[odd]--; + } + + rep(i, 26) { + rep(j, count[i] / 2) { + cout << (char)(i + 'A'); + } + } + + if (odd != -1) { + cout << (char)(odd + 'A'); + } + + for (int i = 25; i >= 0; i--) { + nrep(j, count[i] / 2, count[i]) { + cout << (char)(i + 'A'); + } + } + + cout << '\n'; +} diff --git a/CSES Problem Set/Planets Cycles.cpp b/CSES Problem Set/Planets Cycles.cpp new file mode 100644 index 0000000..8bb937d --- /dev/null +++ b/CSES Problem Set/Planets Cycles.cpp @@ -0,0 +1,166 @@ +/* Problem URL: https://cses.fi/problemset/task/1751 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vi next(n); + repv(i, next) { + cin >> i; + i--; + } + + V vis(n); + V frame(n); + V cycle(n); + vi cyclesize(n); + + auto getcycle = [&](int i) { + set rem; + frame[i] = true; + vis[i] = true; + rem.insert(i); + int j = next[i]; + + while (!vis[j] && !frame[j]) { + vis[j] = true; + frame[j] = true; + rem.insert(j); + j = next[j]; + } + + if (!frame[j]) { + repv(i, rem) { + frame[i] = false; + } + return; + } + + int now = 1; + i = j; + j = next[j]; + cycle[i] = true; + + while (j != i) { + cycle[j] = true; + now++; + j = next[j]; + } + + cyclesize[i] = now; + j = next[j]; + while (j != i) { + cyclesize[j] = now; + j = next[j]; + } + + repv(i, rem) { + frame[i] = false; + } + }; + + rep(i, n) { + getcycle(i); + } + + vi memo(n, -1); + rep(i, n) { + if (cycle[i]) { + memo[i] = cyclesize[i]; + } + } + + function dfs = [&](int i) { + int &ans = memo[i]; + if (ans != -1) { + return ans; + } + + ans = dfs(next[i]) + 1; + return ans; + }; + + rep(i, n) { + cout << dfs(i) << ' '; + } + cout << '\n'; +} diff --git a/CSES Problem Set/Planets Queries I.cpp b/CSES Problem Set/Planets Queries I.cpp new file mode 100644 index 0000000..291656e --- /dev/null +++ b/CSES Problem Set/Planets Queries I.cpp @@ -0,0 +1,116 @@ +/* Problem URL: https://cses.fi/problemset/task/1750 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, q; + cin >> n >> q; + + vvi parent(30, vi(n)); + + rep(i, n) { + int a; + cin >> a; + + parent[0][i] = a - 1; + } + + nrep(i, 1, 30) { + rep(j, n) { + parent[i][j] = parent[i - 1][parent[i - 1][j]]; + } + } + + while (q--) { + int i; + ll k; + cin >> i >> k; + + i--; + + rep(j, 30) { + if ((k >> j) & 1) { + i = parent[j][i]; + } + } + + cout << i + 1 << '\n'; + } +} diff --git a/CSES Problem Set/Planets Queries II.cpp b/CSES Problem Set/Planets Queries II.cpp new file mode 100644 index 0000000..883fefa --- /dev/null +++ b/CSES Problem Set/Planets Queries II.cpp @@ -0,0 +1,240 @@ +/* Problem URL: https://cses.fi/problemset/task/1160 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, q; + cin >> n >> q; + + vi parent(n); + rep(i, n) { + int a; + cin >> a; + + parent[i] = a - 1; + } + + V loop(n); + vi group(n); + vi cyclepos(n); + + V vis(n); + V frame(n); + vi groupsize; + vi cyclechoice(n); + int g = 0; + + auto func = [&](int i) { + set used; + while (!vis[i] && !frame[i]) { + vis[i] = true; + frame[i] = true; + used.insert(i); + i = parent[i]; + } + + if (frame[i]) { + loop[i] = true; + group[i] = g; + cyclechoice[i] = i; + + int now = 1; + + int j = parent[i]; + + while (j != i) { + cyclepos[j] = now; + now++; + loop[j] = true; + group[j] = g; + cyclechoice[j] = j; + j = parent[j]; + } + + g++; + groupsize.push_back(now); + } + + for (auto j : used) { + frame[j] = false; + } + }; + + rep(i, n) { + if (vis[i]) { + continue; + } + + func(i); + } + + fill(all(vis), false); + vi pos(n); + + function dfs = [&](int i) { + vis[i] = true; + + if (!vis[parent[i]] && !loop[parent[i]]) { + dfs(parent[i]); + } + + pos[i] = pos[parent[i]] + 1; + cyclechoice[i] = cyclechoice[parent[i]]; + }; + + rep(i, n) { + if (!loop[i] && !vis[i]) { + dfs(i); + } + } + + vvi lift(17, vi(n)); + + vi valids; + rep(i, n) { + if (!loop[i]) { + valids.push_back(i); + lift[0][i] = parent[i]; + } + } + + nrep(i, 1, 17) { + repv(j, valids) { + lift[i][j] = lift[i - 1][lift[i - 1][j]]; + } + } + + while (q--) { + int a, b; + cin >> a >> b; + a--, b--; + + if (loop[a] && loop[b]) { + if (group[a] != group[b]) { + cout << "-1\n"; + continue; + } + + int g = group[a]; + + cout << ((cyclepos[b] - cyclepos[a]) % groupsize[g] + groupsize[g]) % groupsize[g] << '\n'; + continue; + } + + if (loop[a] && !loop[b]) { + cout << "-1\n"; + continue; + } + + if (!loop[a] && loop[b]) { + int choice = cyclechoice[a]; + + if (group[choice] != group[b]) { + cout << "-1\n"; + continue; + } + + int g = group[choice]; + + cout << pos[a] + ((cyclepos[b] - cyclepos[choice]) % groupsize[g] + groupsize[g]) % groupsize[g] << '\n'; + continue; + } + + int diff = pos[a] - pos[b]; + + if (diff < 0) { + cout << "-1\n"; + continue; + } + + rep(i, 17) { + if ((diff >> i) & 1) { + a = lift[i][a]; + } + } + + if (a != b) { + cout << "-1\n"; + continue; + } + + cout << diff << '\n'; + } +} diff --git a/CSES Problem Set/Planets and Kingdoms.cpp b/CSES Problem Set/Planets and Kingdoms.cpp new file mode 100644 index 0000000..d12e125 --- /dev/null +++ b/CSES Problem Set/Planets and Kingdoms.cpp @@ -0,0 +1,154 @@ +/* Problem URL: https://cses.fi/problemset/task/1683 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vvi graph(n); + vvi inv(n); + + while (m--) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + inv[b].push_back(a); + } + + V vis(n); + stack s; + + function postorder = [&](int i) { + vis[i] = true; + + for (auto j : graph[i]) { + if (vis[j]) { + continue; + } + + postorder(j); + } + + s.push(i); + }; + + rep(i, n) { + if (vis[i]) { + continue; + } + + postorder(i); + } + + vi group(n); + int g = 1; + fill(all(vis), false); + + function dfs = [&](int i) { + vis[i] = true; + group[i] = g; + + for (auto j : inv[i]) { + if (vis[j]) { + continue; + } + + dfs(j); + } + }; + + while (!s.empty()) { + int now = s.top(); + s.pop(); + + if (vis[now]) { + continue; + } + + dfs(now); + g++; + } + + cout << g - 1 << '\n'; + cout << group; +} diff --git a/CSES Problem Set/Playlist.cpp b/CSES Problem Set/Playlist.cpp new file mode 100644 index 0000000..20a4825 --- /dev/null +++ b/CSES Problem Set/Playlist.cpp @@ -0,0 +1,101 @@ +/* Problem URL: https://cses.fi/problemset/task/1141 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + map appear; + + int n; + cin >> n; + + int ans = 0; + int now = 0; + int time = 0; + rep(i, n) { + ll nm; + cin >> nm; + + now++; + + if (appear.count(nm) && appear[nm] >= time) { + now -= appear[nm] - time + 1; + time = appear[nm] + 1; + } + + rmax(ans, now); + appear[nm] = i; + } + + cout << ans << '\n'; +} diff --git a/CSES Problem Set/Police Chase.cpp b/CSES Problem Set/Police Chase.cpp new file mode 100644 index 0000000..99076b5 --- /dev/null +++ b/CSES Problem Set/Police Chase.cpp @@ -0,0 +1,174 @@ +/* Problem URL: https://cses.fi/problemset/task/1695 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int INF = INT32_MAX >> 1; + +struct dinitz { + const bool scaling = false; // com scaling -> O(nm log(MAXCAP)), + int lim; // com constante alta + struct edge { + int to, cap, rev, flow; + bool res; + edge(int to_, int cap_, int rev_, bool res_) + : to(to_), cap(cap_), rev(rev_), flow(0), res(res_) {} + }; + + vector> g; + vector lev, beg; + ll F; + dinitz(int n) : g(n), F(0) {} + + void add(int a, int b, int c) { + g[a].emplace_back(b, c, g[b].size(), false); + g[b].emplace_back(a, 0, g[a].size()-1, true); + } + bool bfs(int s, int t) { + lev = vector(g.size(), -1); lev[s] = 0; + beg = vector(g.size(), 0); + queue q; q.push(s); + while (q.size()) { + int u = q.front(); q.pop(); + for (auto& i : g[u]) { + if (lev[i.to] != -1 or (i.flow == i.cap)) continue; + if (scaling and i.cap - i.flow < lim) continue; + lev[i.to] = lev[u] + 1; + q.push(i.to); + } + } + return lev[t] != -1; + } + int dfs(int v, int s, int f = INF) { + if (!f or v == s) return f; + for (int& i = beg[v]; i < g[v].size(); i++) { + auto& e = g[v][i]; + if (lev[e.to] != lev[v] + 1) continue; + int foi = dfs(e.to, s, min(f, e.cap - e.flow)); + if (!foi) continue; + e.flow += foi, g[e.to][e.rev].flow -= foi; + return foi; + } + return 0; + } + ll max_flow(int s, int t) { + for (lim = scaling ? (1<<30) : 1; lim; lim /= 2) + while (bfs(s, t)) while (int ff = dfs(s, t)) F += ff; + return F; + } +}; + +// Recupera as arestas do corte s-t +vector> get_cut(dinitz& g, int s, int t) { + g.max_flow(s, t); + vector> cut; + vector vis(g.g.size(), 0), st = {s}; + vis[s] = 1; + while (st.size()) { + int u = st.back(); st.pop_back(); + for (auto e : g.g[u]) if (!vis[e.to] and e.flow < e.cap) + vis[e.to] = 1, st.push_back(e.to); + } + for (int i = 0; i < g.g.size(); i++) for (auto e : g.g[i]) + if (vis[i] and !vis[e.to] and !e.res) cut.emplace_back(i, e.to); + return cut; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + dinitz din(n); + + while (m--) { + int a, b; + cin >> a >> b; + a--, b--; + + din.add(a, b, 1); + din.add(b, a, 1); + } + + V> ans = get_cut(din, 0, n - 1); + + cout << ans.size() << '\n'; + for (auto [a, b] : ans) { + cout << a + 1 << ' ' << b + 1 << '\n'; + } +} diff --git a/CSES Problem Set/Projects.cpp b/CSES Problem Set/Projects.cpp index b0ed7ff..148c3a4 100644 --- a/CSES Problem Set/Projects.cpp +++ b/CSES Problem Set/Projects.cpp @@ -1,9 +1,7 @@ /* Problem URL: https://cses.fi/problemset/task/1140 */ #include - using namespace std; - #define V vector #define rmin(a, b) a = min(a, b) @@ -69,52 +67,6 @@ auto operator>>(istream &is, vector> &vec)->istream& { return is; } -class segtree { - vl seg; - - ll _query(int i, int l, int r, int ql, int qr) { - if (l > qr || r < ql) { - return -1; - } - - if (l >= ql && r <= qr) { - return seg[i]; - } - - int mid = (l + r) / 2; - return max(_query(i * 2, l, mid, ql, qr), _query(i * 2 + 1, mid + 1, r, ql, qr)); - } - - public: - segtree() = delete; - segtree(vl &items): seg(items.size() * 2) { - size_t n = items.size(); - for (size_t i = n; i < n * 2; i++) { - seg[i] = items[i - n]; - } - - for (size_t i = n - 1; i > 0; i--) { - seg[i] = max(seg[i * 2], seg[i * 2 + 1]); - } - } - - void update(ll v, int i) { - i += seg.size() / 2; - if (seg[i] >= v) { - return; - } - seg[i] = v; - i /= 2; - for (; i > 0; i /= 2) { - seg[i] = max(seg[i * 2], seg[i * 2 + 1]); - } - } - - ll query(int i) { - return _query(1, 0, seg.size() / 2 - 1, 0, i); - } -}; - int main() { ios::sync_with_stdio(false); @@ -122,39 +74,33 @@ int main() int n; cin >> n; - V> fds(n); - set lol; - for (auto &[i, e, v] : fds) { - cin >> i >> e >> v; - lol.insert(e); + V> q(n); + + map var; + for (auto &[x, y, z] : q) { + cin >> x >> y >> z; + var[x] = 0; + var[y] = 0; } - pair ends(vl(lol.size(), 0), vl(lol.size())); - auto itr = lol.begin(); - for (size_t i = 0; itr != lol.end(); i++, itr++) { - ends.second[i] = *itr; + int v = 0; + for (auto &i : var) { + i.second = v; + v++; } - sort(fds.begin(), fds.end()); - - ll neutral = 1e9 + 1; - while (__builtin_popcount(ends.second.size()) != 1) { - ends.first.push_back(-1); - ends.second.push_back(neutral++); + V>> proj(var.size()); + for (auto [s, e, v] : q) { + proj[var[e]].emplace_back(var[s], v); } - segtree seg(ends.first); - - for (auto [i, f, v] : fds) { - auto itr = lower_bound(ends.second.begin(), ends.second.end(), i); - if (itr == ends.second.begin()) { - seg.update(v, lower_bound(ends.second.begin(), ends.second.end(), f) - ends.second.begin()); - continue; + vl dp(var.size() + 1); + nrep(i, 1, dp.size()) { + dp[i] += dp[i - 1]; + for (auto [s, v] : proj[i - 1]) { + rmax(dp[i], dp[s] + v); } - - ll up = seg.query(itr - ends.second.begin() - 1) + v; - seg.update(up, lower_bound(ends.second.begin(), ends.second.end(), f) - ends.second.begin()); } - cout << seg.query(ends.first.size() - 1) << '\n'; + cout << dp.back() << '\n'; } diff --git a/CSES Problem Set/Raab Game I.cpp b/CSES Problem Set/Raab Game I.cpp new file mode 100644 index 0000000..f14dc44 --- /dev/null +++ b/CSES Problem Set/Raab Game I.cpp @@ -0,0 +1,116 @@ +/* Problem URL: https://cses.fi/problemset/task/3399 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, a, b; + cin >> n >> a >> b; + + vi ans1(n); + rep(i, n) { + ans1[i] = i + 1; + } + + if (a == 0 && b == 0) { + cout << "YES\n" << ans1 << ans1; + continue; + } + + if (a + b == 1 || a == 0 || b == 0 || a + b > n) { + cout << "NO\n"; + continue; + } + + vi ans2(n); + + int i = 0; + for (; i < b; i++) { + ans2[i] = i + a + 1; + } + + for (; i < b + a; i++) { + ans2[i] = i - b + 1; + } + + for (; i < n; i++) { + ans2[i] = ans1[i]; + } + + cout << "YES\n"; + cout << ans1 << ans2; + } +} diff --git a/CSES Problem Set/Reading Books.cpp b/CSES Problem Set/Reading Books.cpp new file mode 100644 index 0000000..56aea7b --- /dev/null +++ b/CSES Problem Set/Reading Books.cpp @@ -0,0 +1,93 @@ +/* Problem URL: https://cses.fi/problemset/task/1631 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vl fds(n); + cin >> fds; + + sortv(fds); + + ll sum = 0; + + vl pref(n + 1); + rep(i, n) { + pref[i + 1] = pref[i] + fds[i]; + } + + cout << (fds.back() >= pref.back() - fds.back() ? fds.back() * 2 : pref.back()) << '\n'; +} diff --git a/CSES Problem Set/Restaurant Customers.cpp b/CSES Problem Set/Restaurant Customers.cpp new file mode 100644 index 0000000..2e95da4 --- /dev/null +++ b/CSES Problem Set/Restaurant Customers.cpp @@ -0,0 +1,109 @@ +/* Problem URL: https://cses.fi/problemset/task/1619 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + V> query(n); + map var; + for (auto &[a, b] : query) { + cin >> a >> b; + var[a] = 0; + var[b] = 0; + } + + int v = 0; + for (auto &i : var) { + i.second = v; + v++; + } + + vi ans(var.size() + 1, 0); + + for (auto [a, b] : query) { + ans[var[a]]++; + ans[var[b] + 1]--; + } + + int maximus = 0; + int count = 0; + repv(i, ans) { + count += i; + rmax(maximus, count); + } + + cout << maximus << '\n'; +} diff --git a/CSES Problem Set/Room Allocation.cpp b/CSES Problem Set/Room Allocation.cpp new file mode 100644 index 0000000..a3780e4 --- /dev/null +++ b/CSES Problem Set/Room Allocation.cpp @@ -0,0 +1,126 @@ +/* Problem URL: https://cses.fi/problemset/task/1164 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + V> cus(n); + map var; + size_t ind = 0; + for (auto &[i, a, b] : cus) { + cin >> a >> b; + var[a] = 0; + var[b] = 0; + i = ind; + ind++; + } + + int v = 0; + for (auto &i : var) { + i.second = v; + v++; + } + + V> act; + + for (auto &[i, a, b] : cus) { + a = var[a]; + b = var[b]; + act.emplace_back(a, 0, i); + act.emplace_back(b, 1, i); + } + + int k = 1; + priority_queue> pos; + for (size_t i = 1; i <= n; i++) { + pos.push(i); + } + vi ans(n); + + sortv(act); + + for (auto [a, yes, i] : act) { + if (!yes) { + ans[i] = pos.top(); + pos.pop(); + rmax(k, ans[i]); + } else { + pos.push(ans[i]); + } + } + + cout << k << '\n'; + cout << ans; +} diff --git a/CSES Problem Set/Round Trip II.cpp b/CSES Problem Set/Round Trip II.cpp new file mode 100644 index 0000000..1fc2ce6 --- /dev/null +++ b/CSES Problem Set/Round Trip II.cpp @@ -0,0 +1,151 @@ +/* Problem URL: https://cses.fi/problemset/task/1678 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vvi graph(n); + + while (m--) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + } + + V vis(n); + V frame(n); + vi parent(n, -1); + + function(int)> dfs = [&](int i) -> pair { + vis[i] = true; + frame[i] = true; + + for (auto j : graph[i]) { + if (frame[j]) { + return {i, j}; + } + + if (vis[j]) { + continue; + } + + parent[j] = i; + auto res = dfs(j); + if (res != make_pair(-1, -1)) { + return res; + } + } + + frame[i] = false; + return {-1, -1}; + }; + + pair res = {-1, -1}; + + rep(i, n) { + if (!vis[i]) { + res = dfs(i); + if (res != make_pair(-1, -1)) { + break; + } + + res = {-1, -1}; + } + } + + if (res == make_pair(-1, -1)) { + cout << "IMPOSSIBLE\n"; + return 0; + } + + vi ans; + + ans.push_back(res.first + 1); + + int tmp = parent[res.first]; + while (tmp != res.second) { + ans.push_back(tmp + 1); + tmp = parent[tmp]; + } + + ans.push_back(res.second + 1); + ans.push_back(res.first + 1); + reverse(all(ans)); + + cout << ans.size() << '\n' << ans; +} diff --git a/CSES Problem Set/Round Trip.cpp b/CSES Problem Set/Round Trip.cpp new file mode 100644 index 0000000..835d881 --- /dev/null +++ b/CSES Problem Set/Round Trip.cpp @@ -0,0 +1,142 @@ +/* Problem URL: https://cses.fi/problemset/task/1669 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vvi graph(n); + + while (m--) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + graph[b].push_back(a); + } + + vi parent(n, -1); + V frame(n, false); + vi ans; + + function dfs = [&](int i, int p) { + frame[i] = true; + + repv(j, graph[i]) { + if (frame[j] && j != p) { + ans.push_back(i + 1); + int p = parent[i]; + while (p != j) { + ans.push_back(p + 1); + p = parent[p]; + } + ans.push_back(j + 1); + ans.push_back(i + 1); + return true; + } + + if (parent[j] != -1) { + continue; + } + + parent[j] = i; + + if (dfs(j, i)) { + return true; + } + } + + frame[i] = false; + return false; + }; + + rep(i, n) { + if (parent[i] != -1) { + continue; + } + + if (dfs(i, i)) { + break; + } + } + + if (ans.empty()) { + cout << "IMPOSSIBLE\n"; + return 0; + } + + cout << ans.size() << '\n' << ans; +} diff --git a/CSES Problem Set/Salary Queries.cpp b/CSES Problem Set/Salary Queries.cpp new file mode 100644 index 0000000..34955c8 --- /dev/null +++ b/CSES Problem Set/Salary Queries.cpp @@ -0,0 +1,118 @@ +/* Problem URL: https://cses.fi/problemset/task/1144 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, q; + cin >> n >> q; + + ordered_set> ord; + + vi sal(n); + + rep(i, n) { + cin >> sal[i]; + + ord.insert({sal[i], i}); + } + + while (q--) { + char op; + cin >> op; + + if (op == '?') { + int a, b; + cin >> a >> b; + + int lower = ord.order_of_key({a, -1}); + cout << ord.order_of_key({b + 1, -1}) - lower << '\n'; + continue; + } + + int i, x; + cin >> i >> x; + i--; + ord.erase({sal[i], i}); + + sal[i] = x; + + ord.insert({sal[i], i}); + } +} diff --git a/CSES Problem Set/School Dance.cpp b/CSES Problem Set/School Dance.cpp new file mode 100644 index 0000000..d26288f --- /dev/null +++ b/CSES Problem Set/School Dance.cpp @@ -0,0 +1,198 @@ +/* Problem URL: https://cses.fi/problemset/task/1696 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int INF = INT32_MAX >> 1; + +struct dinitz { + const bool scaling = false; // com scaling -> O(nm log(MAXCAP)), + int lim; // com constante alta + struct edge { + int to, cap, rev, flow; + bool res; + edge(int to_, int cap_, int rev_, bool res_) + : to(to_), cap(cap_), rev(rev_), flow(0), res(res_) {} + }; + + vector> g; + vector lev, beg; + ll F; + dinitz(int n) : g(n), F(0) {} + + void add(int a, int b, int c) { + g[a].emplace_back(b, c, g[b].size(), false); + g[b].emplace_back(a, 0, g[a].size()-1, true); + } + bool bfs(int s, int t) { + lev = vector(g.size(), -1); lev[s] = 0; + beg = vector(g.size(), 0); + queue q; q.push(s); + while (q.size()) { + int u = q.front(); q.pop(); + for (auto& i : g[u]) { + if (lev[i.to] != -1 or (i.flow == i.cap)) continue; + if (scaling and i.cap - i.flow < lim) continue; + lev[i.to] = lev[u] + 1; + q.push(i.to); + } + } + return lev[t] != -1; + } + int dfs(int v, int s, int f = INF) { + if (!f or v == s) return f; + for (int& i = beg[v]; i < g[v].size(); i++) { + auto& e = g[v][i]; + if (lev[e.to] != lev[v] + 1) continue; + int foi = dfs(e.to, s, min(f, e.cap - e.flow)); + if (!foi) continue; + e.flow += foi, g[e.to][e.rev].flow -= foi; + return foi; + } + return 0; + } + ll max_flow(int s, int t) { + for (lim = scaling ? (1<<30) : 1; lim; lim /= 2) + while (bfs(s, t)) while (int ff = dfs(s, t)) F += ff; + return F; + } +}; + +// Recupera as arestas do corte s-t +vector> get_cut(dinitz& g, int s, int t) { + g.max_flow(s, t); + vector> cut; + vector vis(g.g.size(), 0), st = {s}; + vis[s] = 1; + while (st.size()) { + int u = st.back(); st.pop_back(); + for (auto e : g.g[u]) if (!vis[e.to] and e.flow < e.cap) + vis[e.to] = 1, st.push_back(e.to); + } + for (int i = 0; i < g.g.size(); i++) for (auto e : g.g[i]) + if (vis[i] and !vis[e.to] and !e.res) cut.emplace_back(i, e.to); + return cut; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m, k; + cin >> n >> m >> k; + + dinitz din(n + m + 2); + + int s = 0; + int t = n + m + 1; + + rep(i, n) { + din.add(s, i + 1, 1); + } + + rep(i, m) { + din.add(i + n + 1, t, 1); + } + + while (k--) { + int a, b; + cin >> a >> b; + b += n; + + din.add(a, b, 1); + } + + int ans = din.max_flow(s, t); + + cout << ans << '\n'; + + rep(i, n) { + ans = -1; + for (auto edge : din.g[i + 1]) { + if (edge.res || edge.flow == 0) { + continue; + } + + ans = edge.to - n; + } + + if (ans == -1) { + continue; + } + + cout << i + 1 << ' ' << ans << '\n'; + } +} diff --git a/CSES Problem Set/Shortest Routes I.cpp b/CSES Problem Set/Shortest Routes I.cpp new file mode 100644 index 0000000..4923f1c --- /dev/null +++ b/CSES Problem Set/Shortest Routes I.cpp @@ -0,0 +1,123 @@ +/* Problem URL: https://cses.fi/problemset/task/1671 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + V>> graph(n); + + while (m--) { + int a, b, c; + cin >> a >> b >> c; + a--, b--; + + graph[a].emplace_back(b, c); + } + + ll inf = INT64_MAX >> 1; + + vl dis(n, inf); + + auto dijkstra = [&]() { + dis[0] = 0; + priority_queue, V>, greater<>> pq; + + pq.emplace(0, 0); + + while (!pq.empty()) { + auto [c, i] = pq.top(); + pq.pop(); + + if (c > dis[i]) { + continue; + } + + for (auto [j, cc] : graph[i]) { + if (dis[j] > c + cc) { + dis[j] = c + cc; + pq.emplace(dis[j], j); + } + } + } + }; + + dijkstra(); + + rep(i, n) { + cout << dis[i] << ' '; + } + cout << '\n'; +} diff --git a/CSES Problem Set/Shortest Routes II.cpp b/CSES Problem Set/Shortest Routes II.cpp index 415f48e..1ba5b43 100644 --- a/CSES Problem Set/Shortest Routes II.cpp +++ b/CSES Problem Set/Shortest Routes II.cpp @@ -9,8 +9,8 @@ using namespace std; #define rmin(a, b) a = min(a, b) #define rmax(a, b) a = max(a, b) -#define rep(i, lim) for (size_t i = 0; i < (lim); i++) -#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) #define repv(i, v) for (auto &i : (v)) #define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } @@ -77,40 +77,40 @@ int main() int n, m, q; cin >> n >> m >> q; - vvl weight(n, vl(n, INT64_MAX >> 1)); - vvl floyd(n, vl(n)); + ll inf = INT64_MAX >> 1; + vvl graph(n, vl(n, inf)); - for (size_t i = 0; i < n; i++) { - weight[i][i] = 0; + rep(i, n) { + graph[i][i] = 0; } while (m--) { ll a, b, c; cin >> a >> b >> c; a--, b--; - rmin(weight[a][b], c); - rmin(weight[b][a], c); + + rmin(graph[a][b], c); + rmin(graph[b][a], c); } - for (size_t i = 0; i < n; i++) { - for (size_t j = 0; j < n; j++) { - floyd[i][j] = weight[i][j]; - } - } - - for (size_t i = 0; i < n; i++) { - for (size_t j = 0; j < n; j++) { - for (size_t k = 0; k < n; k++) { - rmin(floyd[j][k], floyd[j][i] + floyd[i][k]); + rep(k, n) { + rep(i, n) { + rep(j, n) { + rmin(graph[i][j], graph[i][k] + graph[k][j]); } } } - + while (q--) { - ll a, b; + int a, b; cin >> a >> b; a--, b--; - cout << (floyd[a][b] != INT64_MAX >> 1 ? floyd[a][b] : -1) << '\n'; + if (graph[a][b] == inf) { + cout << "-1\n"; + continue; + } + + cout << graph[a][b] << '\n'; } } diff --git a/CSES Problem Set/Sliding Window Sum.cpp b/CSES Problem Set/Sliding Window Sum.cpp new file mode 100644 index 0000000..6ed4d16 --- /dev/null +++ b/CSES Problem Set/Sliding Window Sum.cpp @@ -0,0 +1,104 @@ +/* Problem URL: https://cses.fi/problemset/task/3220 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, k; + cin >> n >> k; + + ll x, a, b, c; + cin >> x >> a >> b >> c; + + list fds; + fds.push_back(x); + ll total = x; + + nrep(i, 1, k) { + fds.push_back((fds.back() * a + b) % c); + total += fds.back(); + } + + ll ans = total; + n -= k; + rep(i, n) { + fds.push_back((fds.back() * a + b) % c); + total += fds.back(); + total -= fds.front(); + fds.pop_front(); + + ans ^= total; + } + + cout << ans << '\n'; +} diff --git a/CSES Problem Set/Stick Lengths.cpp b/CSES Problem Set/Stick Lengths.cpp new file mode 100644 index 0000000..22cfb93 --- /dev/null +++ b/CSES Problem Set/Stick Lengths.cpp @@ -0,0 +1,100 @@ +/* Problem URL: https://cses.fi/problemset/task/1074 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vl fds(n); + cin >> fds; + + sortv(fds); + + vl pref(n); + vl suf(n); + + nrep(i, 1, n) { + pref[i] = pref[i - 1] + abs(fds[i] - fds[i - 1]) * i; + suf[n - i - 1] = suf[n - i] + abs(fds[n - i - 1] - fds[n - i]) * i; + } + + ll ans = INT64_MAX >> 1; + + rep(i, n) { + rmin(ans, pref[i] + suf[i]); + } + + cout << ans << '\n'; +} diff --git a/CSES Problem Set/String Matching.cpp b/CSES Problem Set/String Matching.cpp new file mode 100644 index 0000000..ac175a4 --- /dev/null +++ b/CSES Problem Set/String Matching.cpp @@ -0,0 +1,125 @@ +/* Problem URL: https://cses.fi/problemset/task/1753 */ + +#include +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +struct hash_string { + vl pow; + vl hash; + static ll base; + static ll mod; + + hash_string() = delete; + hash_string(string &a) { + pow.resize(a.size() + 1); + hash.resize(a.size() + 1); + + pow[0] = 1; + hash[0] = 0; + + + rep(i, a.size()) { + pow[i + 1] = ((__int128)pow[i] * base) % mod; + hash[i + 1] = ((__int128)hash[i] * base + a[i]) % mod; + } + } + + ll gethash(int i, int f) + { + return ((hash[f + 1] - ((__int128)hash[i] * pow[f - i + 1])) % mod + mod) % mod; + } +}; + +ll hash_string::mod = (1LL << 61) - 1; +mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll hash_string::base = uniform_int_distribution(0, (1LL << 61) - 1)(rng); + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + string a, b; + cin >> a >> b; + + hash_string ahash(a); + hash_string bhash(b); + + ll hash = bhash.gethash(0, b.size() - 1); + + ll count = 0; + for (size_t i = 0; i + b.size() - 1 < a.size(); i++) { + if (hash == ahash.gethash(i, i + b.size() - 1)) { + count++; + } + } + + cout << count << '\n'; +} diff --git a/CSES Problem Set/String Reorder.cpp b/CSES Problem Set/String Reorder.cpp new file mode 100644 index 0000000..74fad69 --- /dev/null +++ b/CSES Problem Set/String Reorder.cpp @@ -0,0 +1,120 @@ +/* Problem URL: https://cses.fi/problemset/task/1743 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + string fds; + cin >> fds; + + vi count(26); + repv(i, fds) { + count[i - 'A']++; + } + + string ans(fds.size(), ' '); + rep(i, ans.size()) { + int now = -1; + auto valid = [&](int act){ + int maximal = 0; + int size = 0; + + rep(i, 26) { + if (i == act) { + rmax(maximal, count[i] - 1); + size += count[i] - 1; + } else { + rmax(maximal, count[i]); + size += count[i]; + } + } + + return maximal <= size - maximal + 1; + }; + rep(j, 26) { + if (count[j] != 0 && (i == 0 || j + 'A' != ans[i - 1]) && valid(j)) { + now = j; + count[j]--; + break; + } + } + + if (now == -1) { + cout << "-1\n"; + return 0; + } + + ans[i] = now + 'A'; + } + cout << ans << '\n'; +} diff --git a/CSES Problem Set/Subarray Distinct Values.cpp b/CSES Problem Set/Subarray Distinct Values.cpp new file mode 100644 index 0000000..113e054 --- /dev/null +++ b/CSES Problem Set/Subarray Distinct Values.cpp @@ -0,0 +1,126 @@ +/* Problem URL: https://cses.fi/problemset/task/2428 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, k; + cin >> n >> k; + + map var; + vl fds(n); + repv(i, fds){ + cin >> i; + var[i] = 0; + } + + int v = 0; + repv(i, var) { + i.second = v; + v++; + } + + repv(i, fds) { + i = var[i]; + } + + vi count(var.size(), 0); + + int l = 0; + int r = 0; + ll ans = 0; + + int diffs = 0; + + while (r < n) { + if (count[fds[r]] == 0) { + diffs++; + } + + count[fds[r]]++; + + while (diffs > k) { + count[fds[l]]--; + if (count[fds[l]] == 0) { + diffs--; + } + l++; + } + + ans += r - l + 1; + + r++; + } + + cout << ans << '\n'; +} diff --git a/CSES Problem Set/Subarray Divisibility.cpp b/CSES Problem Set/Subarray Divisibility.cpp new file mode 100644 index 0000000..d8a7137 --- /dev/null +++ b/CSES Problem Set/Subarray Divisibility.cpp @@ -0,0 +1,95 @@ +/* Problem URL: https://cses.fi/problemset/task/1662 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + ll sum = 0; + ll ans = 0; + map count; + count[0] = 1; + + rep(i, n) { + ll now; + cin >> now; + + sum = ((sum + now) % n + n) % n; + ans += count[sum]; + count[sum]++; + } + + cout << ans << '\n'; +} diff --git a/CSES Problem Set/Subarray Sums I.cpp b/CSES Problem Set/Subarray Sums I.cpp new file mode 100644 index 0000000..9577c31 --- /dev/null +++ b/CSES Problem Set/Subarray Sums I.cpp @@ -0,0 +1,96 @@ +/* Problem URL: https://cses.fi/problemset/task/1660 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + ll x; + cin >> n >> x; + + map count; + + ll sum = 0; + int ans = 0; + count[0] = 1; + rep(i, n) { + ll now; + cin >> now; + + sum += now; + ans += count[sum - x]; + count[sum]++; + } + + cout << ans << '\n'; +} diff --git a/CSES Problem Set/Subarray Sums II.cpp b/CSES Problem Set/Subarray Sums II.cpp new file mode 100644 index 0000000..4de779a --- /dev/null +++ b/CSES Problem Set/Subarray Sums II.cpp @@ -0,0 +1,96 @@ +/* Problem URL: https://cses.fi/problemset/task/1660 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + ll x; + cin >> n >> x; + + map count; + + ll sum = 0; + ll ans = 0; + count[0] = 1; + rep(i, n) { + ll now; + cin >> now; + + sum += now; + ans += count[sum - x]; + count[sum]++; + } + + cout << ans << '\n'; +} diff --git a/CSES Problem Set/Subordinates.cpp b/CSES Problem Set/Subordinates.cpp new file mode 100644 index 0000000..7474b37 --- /dev/null +++ b/CSES Problem Set/Subordinates.cpp @@ -0,0 +1,106 @@ +/* Problem URL: https://cses.fi/problemset/task/1674 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vvi graph(n); + + nrep(i, 1, n) { + int a; + cin >> a; + a--; + + graph[a].push_back(i); + } + + vi ans(n); + + function dfs = [&](int i){ + int tmp = 0; + + for (auto j : graph[i]) { + tmp += dfs(j); + } + + ans[i] = tmp; + return tmp + 1; + }; + + dfs(0); + + cout << ans; +} diff --git a/CSES Problem Set/Sum of Four Values.cpp b/CSES Problem Set/Sum of Four Values.cpp new file mode 100644 index 0000000..2f70180 --- /dev/null +++ b/CSES Problem Set/Sum of Four Values.cpp @@ -0,0 +1,108 @@ +/* Problem URL: https://cses.fi/problemset/task/1642 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + ll x; + cin >> n >> x; + + if (n < 4) { + cout << "IMPOSSIBLE\n"; + return 0; + } + + vl fds(n); + cin >> fds; + + map> pos; + + rep(i, n - 1) { + nrep(j, i + 1, n) { + pos[fds[i] + fds[j]] = {i, j}; + } + } + + rep(i, n - 1) { + nrep(j, i + 1, n) { + auto itr = pos.find(x - fds[i] - fds[j]); + if (itr != pos.end() && itr->second.first != i && itr->second.first != j && itr->second.second != i && itr->second.second != j) { + cout << i + 1 << ' ' << j + 1 << ' ' << itr->second.first + 1 << ' ' << itr->second.second + 1 << '\n'; + return 0; + } + } + } + + cout << "IMPOSSIBLE\n"; +} diff --git a/CSES Problem Set/Sum of Three Values.cpp b/CSES Problem Set/Sum of Three Values.cpp new file mode 100644 index 0000000..acada1f --- /dev/null +++ b/CSES Problem Set/Sum of Three Values.cpp @@ -0,0 +1,114 @@ +/* Problem URL: https://cses.fi/problemset/task/1641 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + ll x; + cin >> n >> x; + + if (n < 3) { + cout << "IMPOSSIBLE\n"; + return 0; + } + + V> fds(n); + rep(i, n) { + fds[i].second = i; + cin >> fds[i].first; + } + + sortv(fds); + + nrep(i, 1, n) { + int l = 0; + int r = n - 1; + + while (l != r) { + if (fds[l].first + fds[r].first == x - fds[i].first && l != i && r != i) { + cout << fds[l].second + 1 << ' ' << fds[r].second + 1 << ' ' << fds[i].second + 1 << '\n'; + return 0; + } + + if (fds[l].first + fds[r].first < x - fds[i].first) { + l++; + continue; + } + r--; + } + } + + + cout << "IMPOSSIBLE\n"; +} diff --git a/CSES Problem Set/Sum of Two Values.cpp b/CSES Problem Set/Sum of Two Values.cpp new file mode 100644 index 0000000..dafc298 --- /dev/null +++ b/CSES Problem Set/Sum of Two Values.cpp @@ -0,0 +1,95 @@ +/* Problem URL: https://cses.fi/problemset/task/1640 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll n, x; + cin >> n >> x; + + map pos; + + rep(i, n) { + ll now; + cin >> now; + + if (pos.count(x - now)) { + cout << pos[x - now] + 1 << ' ' << i + 1 << '\n'; + return 0; + } + + pos[now] = i; + } + + cout << "IMPOSSIBLE\n"; +} diff --git a/CSES Problem Set/Tasks and Deadlines.cpp b/CSES Problem Set/Tasks and Deadlines.cpp new file mode 100644 index 0000000..d440a72 --- /dev/null +++ b/CSES Problem Set/Tasks and Deadlines.cpp @@ -0,0 +1,102 @@ +/* Problem URL: https://cses.fi/problemset/task/1630 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + priority_queue, V>, greater<>> pq; + + while (n--) { + ll a, b; + cin >> a >> b; + pq.emplace(a, b); + } + + ll now = 0; + ll ans = 0; + + priority_queue, V>, greater<>> aux; + + while (!pq.empty()) { + auto [a, b] = pq.top(); + pq.pop(); + + now += a; + ans += b - now; + } + + cout << ans << '\n'; +} diff --git a/CSES Problem Set/Teleporters Path.cpp b/CSES Problem Set/Teleporters Path.cpp new file mode 100644 index 0000000..6432b27 --- /dev/null +++ b/CSES Problem Set/Teleporters Path.cpp @@ -0,0 +1,149 @@ +/* Problem URL: https://cses.fi/problemset/task/1693 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vi out(n); + vi in(n); + + vvi graph(n); + + while (m--) { + int a, b; + cin >> a >> b; + a--, b--; + + out[a]++; + in[b]++; + + graph[a].push_back(b); + } + + auto check = [&]() { + if (out[0] != in[0] + 1 || out[n - 1] + 1 != in[n - 1]) { + return false; + }; + + nrep(i, 1, n - 1) { + if (out[i] != in[i]) { + return false; + } + } + + return true; + }; + + if (!check()) { + cout << "IMPOSSIBLE\n"; + return 0; + } + + stack s; + s.push(0); + + vi path; + + while (!s.empty()) { + int i = s.top(); + + if (graph[i].empty()) { + path.push_back(i + 1); + s.pop(); + continue; + } + + s.push(graph[i].back()); + graph[i].pop_back(); + } + + rep(i, n) { + if (!graph[i].empty()) { + cout << "IMPOSSIBLE\n"; + return 0; + } + } + + reverse(all(path)); + + cout << path; +} diff --git a/CSES Problem Set/Tower of Hanoi.cpp b/CSES Problem Set/Tower of Hanoi.cpp new file mode 100644 index 0000000..1cef619 --- /dev/null +++ b/CSES Problem Set/Tower of Hanoi.cpp @@ -0,0 +1,98 @@ +/* Problem URL: https://cses.fi/problemset/task/2165 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + V> ans; + + function tower = [&](int main, int aux, int target, int n) { + if (n <= 0) { + return; + } + + tower(main, target, aux, n - 1); + ans.emplace_back(main, target); + tower(aux, main, target, n - 1); + }; + + tower(1, 2, 3, n); + + cout << ans.size() << '\n'; + repv(i, ans) { + cout << i.first << ' ' << i.second << '\n'; + } +} diff --git a/CSES Problem Set/Towers.cpp b/CSES Problem Set/Towers.cpp new file mode 100644 index 0000000..a5faf68 --- /dev/null +++ b/CSES Problem Set/Towers.cpp @@ -0,0 +1,95 @@ +/* Problem URL: https://cses.fi/problemset/task/1073 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + multiset cubes; + + while (n--) { + ll now; + cin >> now; + + auto itr = cubes.upper_bound(now); + if (itr != cubes.end()) { + cubes.erase(itr); + } + + cubes.insert(now); + } + + cout << cubes.size() << '\n'; +} diff --git a/CSES Problem Set/Traffic Lights.cpp b/CSES Problem Set/Traffic Lights.cpp new file mode 100644 index 0000000..fe91f04 --- /dev/null +++ b/CSES Problem Set/Traffic Lights.cpp @@ -0,0 +1,109 @@ +/* Problem URL: https://cses.fi/problemset/task/1163 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + multiset sizes; + set> ranges; + + ll x; + int n; + cin >> x >> n; + + sizes.insert(x + 1); + ranges.emplace(0, x); + + while (n--) { + ll now; + cin >> now; + + auto itr = prev(ranges.upper_bound({now, x + 1})); + auto [s, e] = *itr; + ranges.erase(itr); + sizes.erase(sizes.find(e - s + 1)); + + if (now >= s) { + ranges.emplace(s, now); + sizes.insert(now - s + 1); + } + if (now <= e) { + ranges.emplace(now, e); + sizes.insert(e - now + 1); + } + + cout << *prev(sizes.end()) - 1 << ' '; + } + + cout << '\n'; +} diff --git a/CSES Problem Set/Trailing Zeros.cpp b/CSES Problem Set/Trailing Zeros.cpp new file mode 100644 index 0000000..6c97601 --- /dev/null +++ b/CSES Problem Set/Trailing Zeros.cpp @@ -0,0 +1,86 @@ +/* Problem URL: https://cses.fi/problemset/task/1618 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll n; + cin >> n; + + ll ans = 0; + for (ll i = 5; i <= n; i *= 5) { + ans += n / i; + } + + cout << ans << '\n'; +} diff --git a/CSES Problem Set/Tree Diameter.cpp b/CSES Problem Set/Tree Diameter.cpp new file mode 100644 index 0000000..d591075 --- /dev/null +++ b/CSES Problem Set/Tree Diameter.cpp @@ -0,0 +1,119 @@ +/* Problem URL: https://cses.fi/problemset/task/1131 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vvi graph(n); + rep(i, n - 1) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + graph[b].push_back(a); + } + + int maximus = 0; + int node = 0; + vi dis(n, 0); + + function dfs = [&](int i, int p) { + dis[i] = dis[p] + 1; + + if (dis[i] > maximus) { + node = i; + maximus = dis[i]; + } + + for (auto j : graph[i]) { + if (j == p) { + continue; + } + + dfs(j, i); + } + }; + + dfs(0, 0); + + maximus = 0; + + fill(all(dis), 0); + dfs(node, node); + + cout << dis[node] - 1 << '\n'; +} diff --git a/CSES Problem Set/Tree Distances I.cpp b/CSES Problem Set/Tree Distances I.cpp new file mode 100644 index 0000000..a958f97 --- /dev/null +++ b/CSES Problem Set/Tree Distances I.cpp @@ -0,0 +1,141 @@ +/* Problem URL: https://cses.fi/problemset/task/1132 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vvi graph(n); + + rep(i, n - 1) { + int a, b; + cin >> a >> b; + a--, b--; + graph[a].push_back(b); + graph[b].push_back(a); + } + + vi dis(n); + vi ans(n, 0); + function dfs = [&](int i, int p){ + int ret = 0; + for (auto j : graph[i]) { + if (j == p) { + continue; + } + + rmax(ret, dfs(j, i)); + } + + dis[i] = ret + 1; + return ret + 1; + }; + + dfs(0, 0); + + function getans = [&](int i, int p){ + int a = 0; + priority_queue pq; + + for (auto j : graph[i]) { + rmax(a, dis[j]); + pq.push(dis[j]); + } + + ans[i] = a; + dis[i] = a + 1; + + for (auto j : graph[i]) { + if (j == p) { + continue; + } + + if (dis[j] == a) { + int prev = dis[i]; + pq.pop(); + dis[i] = pq.empty() ? 1 : pq.top() + 1; + getans(j, i); + dis[i] = prev; + pq.push(prev); + } else { + getans(j, i); + } + } + }; + + getans(0, 0); + + cout << ans; +} diff --git a/CSES Problem Set/Tree Distances II.cpp b/CSES Problem Set/Tree Distances II.cpp new file mode 100644 index 0000000..0dfc318 --- /dev/null +++ b/CSES Problem Set/Tree Distances II.cpp @@ -0,0 +1,78 @@ +/* Problem URL: https://cses.fi/problemset/task/1133 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + +} diff --git a/CSES Problem Set/Tree Matching.cpp b/CSES Problem Set/Tree Matching.cpp new file mode 100644 index 0000000..51dbc4d --- /dev/null +++ b/CSES Problem Set/Tree Matching.cpp @@ -0,0 +1,113 @@ +/* Problem URL: https://cses.fi/problemset/task/1130 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vvi graph(n); + rep(i, n - 1) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + graph[b].push_back(a); + } + + vvi dp(n, vi(2, 0)); + + function dfs = [&](int i, int p) { + for (auto j : graph[i]) { + if (j == p) { + continue; + } + + dfs(j, i); + dp[i][0] += dp[j][1]; + } + + for (auto j : graph[i]) { + if (j == p) { + continue; + } + + rmax(dp[i][1], dp[i][0] - dp[j][1] + dp[j][0] + 1); + } + }; + + dfs(0, 0); + cout << max(dp[0][0], dp[0][1]) << '\n'; +} diff --git a/CSES Problem Set/Two Knights.cpp b/CSES Problem Set/Two Knights.cpp new file mode 100644 index 0000000..0c46aea --- /dev/null +++ b/CSES Problem Set/Two Knights.cpp @@ -0,0 +1,84 @@ +/* Problem URL: https://cses.fi/problemset/task/1072 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll n; + cin >> n; + + cout << "0\n"; + for (ll i = 2; i <= n; i++) { + cout << i * i * (i * i - 1) / 2 - 4 * ((i - 1) * (i - 2)) << '\n'; + } +}