diff --git a/Nowcoder/81602H.cpp b/Nowcoder/81602H.cpp new file mode 100644 index 0000000..d66ce7e --- /dev/null +++ b/Nowcoder/81602H.cpp @@ -0,0 +1,142 @@ +/** + * @file 81602H.cpp + * @author Macesuted (i@macesuted.moe) + * @date 2024-08-06 + * + * @copyright Copyright (c) 2024 + * + */ + +#include +using namespace std; + +#define endl '\n' + +typedef pair pll; + +const int64_t base1 = 1331, base2 = 13331, mod1 = 1e9 + 7, mod2 = 1e9 + 9; + +list> S; +map key; +map dict; +int n; + +pll getHash(string s) { + pll v = {0, 0}; + for (auto c : s) v.first = (v.first * base1 + c) % mod1, v.second = (v.second * base2 + c) % mod2; + dict[v] = s; + return v; +} + +int haveOut, deleteCnt; +vector solve(string s) { + int m = s.size(); + vector pos; + for (int i = 0; i < m; i++) + if (s[i] == '(' || s[i] == ')') pos.push_back(i); + vector args; + int bl = pos.front(), br = pos.back(); + { + string t; + int cnt = 0; + for (int i = bl + 1; i < br; i++) { + char c = s[i]; + if (c == ',' && (pos.size() == 2 || i < pos[1])) + args.push_back(t), t.clear(), cnt++; + else + t.push_back(c); + } + args.push_back(t); + } + string cmd = s.substr(0, bl); + + if (cmd == "begin") { + return {}; + } + if (cmd == "commit") { + for (auto line = S.begin(); line != S.end(); line++) { + if ((*line)[n].first == 1) (*line)[n].first = 0; + if ((*line)[n].second == 1) line = S.erase(line), line--; + } + return {}; + } + if (cmd == "abort") { + for (auto line = S.begin(); line != S.end(); line++) { + if ((*line)[n].second == 1) (*line)[n].second = 0; + if ((*line)[n].first == 1) line = S.erase(line), line--; + } + return {}; + } + + set vals; + + if (cmd == "select_in") { + cmd = "select"; + auto ret = solve(args[2]); + for (auto i : ret) vals.insert(i); + } + if (cmd == "delete_in") { + cmd = "delete"; + auto ret = solve(args[1]); + for (auto i : ret) vals.insert(i); + } + if (cmd == "select") vals.insert(getHash(args[2])); + if (cmd == "delete") vals.insert(getHash(args[1])); + + if (cmd == "insert") { + vector line; + line.reserve(n + 1); + for (int i = 0; i < n; i++) line.push_back(getHash(args[i])); + line.push_back({1, 0}); + S.push_back(line); + return {}; + } + if (cmd == "select") { + haveOut = 1; + vector ret; + int out = key[args[0]], cond = key[args[1]]; + for (auto &line : S) + if (line[n].second == 0 && vals.count(line[cond])) ret.push_back(line[out]); + return ret; + } + if (cmd == "delete") { + haveOut = 2, deleteCnt = 0; + int cond = key[args[0]]; + for (auto &line : S) + if (line[n].second == 0 && vals.count(line[cond])) line[n].second = 1, deleteCnt++; + return {}; + } + return {}; +} + +void solve(void) { + int q; + cin >> n >> q; + for (int i = 0; i < n; i++) { + string s; + cin >> s; + key[s] = i; + } + while (q--) { + string command; + cin >> command; + haveOut = 0; + vector ret = solve(command); + if (haveOut == 1) { + int x = ret.size(); + cout << x << endl; + if (x) cout << dict[ret[1 - 1]] << endl << dict[ret[(x + 1) / 2 - 1]] << endl << dict[ret[x - 1]] << endl; + } else if (haveOut == 2) + cout << deleteCnt << endl; + } + return; +} + +int main() { + ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); + + int _ = 1; + while (_--) solve(); + + return 0; +} \ No newline at end of file diff --git a/Nowcoder/81602I.cpp b/Nowcoder/81602I.cpp new file mode 100644 index 0000000..e5f13b7 --- /dev/null +++ b/Nowcoder/81602I.cpp @@ -0,0 +1,37 @@ +/** + * @file 81602I.cpp + * @author Macesuted (i@macesuted.moe) + * @date 2024-08-06 + * + * @copyright Copyright (c) 2024 + * + */ + +#include +using namespace std; + +#define endl '\n' + +void solve(void) { + int m, k, h; + cin >> m >> k >> h; + if (k == m) return cout << min(h, m) << endl, void(); + int l = 0, r = h; + while (l + 1 < r) { + int64_t mid = (l + r) >> 1, tot = mid; + if (mid >= m) tot += ((mid - m) / (m - k) + 1) * k; + (tot >= h ? r : l) = mid; + } + cout << r << endl; + return; +} + +int main() { + ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); + + int _; + cin >> _; + while (_--) solve(); + + return 0; +} \ No newline at end of file