diff --git a/AdvancedLevel_C++/1156. Sexy Primes (20).cpp b/AdvancedLevel_C++/1156. Sexy Primes (20).cpp index afc0a57..bf94195 100644 --- a/AdvancedLevel_C++/1156. Sexy Primes (20).cpp +++ b/AdvancedLevel_C++/1156. Sexy Primes (20).cpp @@ -1,34 +1,25 @@ -#include #include +#include using namespace std; - -bool IsPrime(int x) { - if (x < 3) return x == 2; - if (x % 2 == 0) return false; - - int limit = sqrt(x); - for (int i = 3; i <= limit; i += 2) - if (x % i == 0) return false; - return true; +int p, ans; +int is_prime(int x) { + if (x < 2) return 0; + for (int i = 2; i <= sqrt(x); i++) + if (x % i == 0) return 0; + return 1; } - int main() { - int n; - cin >> n; - if (IsPrime(n)) { - if (IsPrime(n - 6)) { - cout << "Yes" << endl << n - 6 << endl; - return 0; - } else if (IsPrime(n + 6)) { - cout << "Yes" << endl << n + 6 << endl; - return 0; - } - } - while (true) { - if (IsPrime(n) && (IsPrime(n - 6) || IsPrime(n + 6))) { - cout << "No" << endl << n << endl; - return 0; + cin >> p; + if (is_prime(p) && is_prime(p - 6)) { + cout << "Yes\n" << p - 6; + } else if (is_prime(p) && is_prime(p + 6)) { + cout << "Yes\n" << p + 6; + } else { + for (ans = p + 1; ; ans++) { + if (is_prime(ans) && is_prime(ans - 6)) break; + if (is_prime(ans) && is_prime(ans + 6)) break; } - n++; + cout << "No\n" << ans; } + return 0; } \ No newline at end of file diff --git a/AdvancedLevel_C++/1157. Anniversary (25).cpp b/AdvancedLevel_C++/1157. Anniversary (25).cpp index adb904e..0df9e56 100644 --- a/AdvancedLevel_C++/1157. Anniversary (25).cpp +++ b/AdvancedLevel_C++/1157. Anniversary (25).cpp @@ -1,28 +1,32 @@ #include -#include +#include using namespace std; - +int n, m, cnt; +string temp, smallx = "99999999", smalla = "99999999", ansx, ansa; +set record; int main() { - string id, oldest = "999999999999999999"; - unordered_set alumnus; - int n, m, count = 0; cin >> n; - while (n--) { - cin >> id; - alumnus.insert(id); + for (int i = 0; i < n; i++) { + cin >> temp; + record.insert(temp); } cin >> m; - while (m--) { - cin >> id; - if (alumnus.count(id)) { - count++; - if (count == 0 || stoi(id.substr(6, 8)) < stoi(oldest.substr(6, 8))) - oldest = id; - } else { - if (count == 0 && stoi(id.substr(6, 8)) < stoi(oldest.substr(6, 8))) - oldest = id; + for (int i = 0; i < m; i++) { + cin >> temp; + if (record.count(temp)) { + cnt++; + if (smallx > temp.substr(6, 8)) { + smallx = temp.substr(6, 8); + ansx = temp; + } + } + if (smalla > temp.substr(6, 8)) { + smalla = temp.substr(6, 8); + ansa = temp; } } - cout << count << endl << oldest << endl; + cout << cnt << endl; + if (cnt) cout << ansx; + else cout << ansa; return 0; } \ No newline at end of file diff --git a/AdvancedLevel_C++/1158. Telefraud Detection (25).cpp b/AdvancedLevel_C++/1158. Telefraud Detection (25).cpp index 8c2d674..3ee04bf 100644 --- a/AdvancedLevel_C++/1158. Telefraud Detection (25).cpp +++ b/AdvancedLevel_C++/1158. Telefraud Detection (25).cpp @@ -1,73 +1,52 @@ -#include -#include -#include -using namespace std; - -struct UnionFind { - vector id; - vector sz; - - UnionFind(int n) : id(n), sz(n, 1) { - for (int i = 0; i < n; i++) - id[i] = i; - } - - int Find(int x) { - if (id[x] == x) return x; - return id[x] = Find(id[x]); - } - - void Union(int x, int y) { - int i = Find(x); - int j = Find(y); - if (i == j) return; - if (sz[i] < sz[j]) { id[i] = j; sz[j] += sz[i]; } - else { id[j] = i; sz[i] += sz[j]; } - } -}; - -int main() { - int k, n, m; - cin >> k >> n >> m; - vector> durations(n + 1, vector(n + 1)); - while (m--) { - int c, r, d; - cin >> c >> r >> d; - durations[c][r] += d; - } - vector suspects; - for (int i = 1; i <= n; i++) { - int count = 0, call_back = 0; - for (int j = 1; j <= n; j++) { - if (durations[i][j] > 0 && durations[i][j] <= 5) { - count++; - call_back += (durations[j][i] != 0); - } - } - if (count > k && call_back <= 0.2 * count) - suspects.push_back(i); - } - if (suspects.empty()) { - cout << "None" << endl; - return 0; - } - UnionFind uf(n + 1); - for (int i = 0; i < suspects.size(); i++) - for (int j = i + 1; j < suspects.size(); j++) - if (durations[suspects[i]][suspects[j]] != 0 && durations[suspects[j]][suspects[i]] != 0) - uf.Union(suspects[i], suspects[j]); - vector temp[n + 1]; - for (auto x : suspects) - temp[uf.Find(x)].push_back(x); - vector> ans; - for (auto gang : temp) - if (!gang.empty()) - ans.push_back(gang); - sort(ans.begin(), ans.end(), [](vector v1, vector v2) { - return v1.front() < v2.front(); - }); - for (auto gang : ans) - for (int i = 0; i < gang.size(); i++) - cout << gang[i] << (i < gang.size() - 1 ? ' ' : '\n'); - return 0; +#include +#include +using namespace std; +int k, n, m, c, r, d, p[1001], sc[1001], rec[1001], mark[1001], record[1001][1001]; +vector su; +int Find(int a) { + if (p[a] != a) return p[a] = Find(p[a]); + return a; +} +void add(int a, int b) { + int f1 = Find(a), f2 = Find(b); + if (f1 < f2) p[f2] = f1; + else p[f1] = f2; +} +int main() { + for (int i = 1; i <= 1000; i++) p[i] = i; + cin >> k >> n >> m; + for (int i = 0; i < m; i++) { + cin >> c >> r >> d; + record[c][r] += d; + } + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= n; j++) { + if (record[i][j] && record[i][j] <= 5) { + sc[i]++; + if (record[j][i]) rec[i]++; + } + } + if (sc[i] > k && rec[i] * 5 <= sc[i]) su.push_back(i); + } + if (su.empty()) { + cout << "None"; + return 0; + } + for (int i = 0; i < su.size(); i++) { + for (int j = i + 1; j < su.size(); j++) { + if (record[su[i]][su[j]] && record[su[j]][su[i]]) add(su[i], su[j]); + } + } + for (int i = 0; i < su.size(); i++) { + if (mark[su[i]]) continue; + cout << su[i]; + for (int j = i + 1; j < su.size(); j++) { + if (Find(su[i]) == Find(su[j])) { + cout << ' ' << su[j]; + mark[su[j]] = 1; + } + } + cout << '\n'; + } + return 0; } \ No newline at end of file diff --git a/AdvancedLevel_C++/1159. Structure of a Binary Tree (30).cpp b/AdvancedLevel_C++/1159. Structure of a Binary Tree (30).cpp index 61bea59..3b3b013 100644 --- a/AdvancedLevel_C++/1159. Structure of a Binary Tree (30).cpp +++ b/AdvancedLevel_C++/1159. Structure of a Binary Tree (30).cpp @@ -1,80 +1,70 @@ -#include -#include -#include -using namespace std; - -struct TreeNode { - int key, level; - TreeNode* parent; - TreeNode* left; - TreeNode* right; - - TreeNode(int key) : key(key) {} -}; - -bool is_full = true; -unordered_map nodes; - -TreeNode* Build(int* postorder, int* inorder, int n) { - if (n <= 0) return nullptr; - TreeNode* root = new TreeNode(postorder[n - 1]); - nodes[root->key] = root; - int i; - for (i = 0; i < n && inorder[i] != root->key; i++); - root->left = Build(postorder, inorder, i); - root->right = Build(postorder + i, inorder + i + 1, n - i - 1); - if (root->left) root->left->parent = root; - if (root->right) root->right->parent = root; - return root; -} - -void Traversal(TreeNode* root) { - if (!root) return; - if (root->parent) root->level = root->parent->level + 1; - if (root->left && !root->right) is_full = false; - if (!root->left && root->right) is_full = false; - Traversal(root->left); - Traversal(root->right); -} - -int main() { - int n, m; - cin >> n; - int* postorder = new int[n]; - int* inorder = new int[n]; - for (int i = 0; i < n; i++) cin >> postorder[i]; - for (int i = 0; i < n; i++) cin >> inorder[i]; - TreeNode* root = Build(postorder, inorder, n); - root->level = 1; - Traversal(root); - cin >> m; getchar(); - while (m--) { - bool flag; - int a, b; - string statement; - getline(cin, statement); - if (statement.find("root") != string::npos) { - sscanf(statement.c_str(), "%d is the root", &a); - flag = (a == root->key); - } else if (statement.find("siblings") != string::npos) { - sscanf(statement.c_str(), "%d and %d are siblings", &a, &b); - flag = (nodes[a]->parent == nodes[b]->parent); - } else if (statement.find("parent") != string::npos) { - sscanf(statement.c_str(), "%d is the parent of %d", &a, &b); - flag = (nodes[b]->parent == nodes[a]); - } else if (statement.find("left child") != string::npos) { - sscanf(statement.c_str(), "%d is the left child of %d", &a, &b); - flag = (nodes[b]->left == nodes[a]); - } else if (statement.find("right child") != string::npos) { - sscanf(statement.c_str(), "%d is the right child of %d", &a, &b); - flag = (nodes[b]->right == nodes[a]); - } else if (statement.find("same level") != string::npos) { - sscanf(statement.c_str(), "%d and %d are on the same level", &a, &b); - flag = (nodes[a]->level == nodes[b]->level); - } else { - flag = is_full; - } - cout << (flag ? "Yes" : "No") << endl; - } - return 0; +#include +using namespace std; +struct node { + int lchild, rchild, parent, level; + node() { + lchild = rchild = parent = -1; + } +}Tree[1002]; +int n, m, a, b, root, f, Full; +int In[32], Post[32]; +string t; +int deal(int R, int start, int end, int Pa) { + if(start > end) return -1; + int i = start; + while(i < end && In[i] != Post[R]) i++; + Tree[Post[R]].parent = Pa; + Tree[Post[R]].level = Tree[Pa].level + 1; + Tree[Post[R]].lchild = deal(R - 1 - end + i, start, i - 1, Post[R]); + Tree[Post[R]].rchild = deal(R - 1, i + 1, end, Post[R]); + if (Tree[Post[R]].lchild * Tree[Post[R]].rchild < 0) Full = 1; + return Post[R]; +} +int main() { + cin >> n; + for (int i = 0; i < n; i++) { + cin >> Post[i]; + } + for (int i = 0; i < n; i++) + cin >> In[i]; + root = Post[n - 1]; + deal(n - 1, 0, n - 1, 1001); + cin >> m; + while (m--) { + f = 0; + cin >> t; + if (t == "It") { + cin >> t >> t >> t >> t; + if (Full) cout << "No\n"; + else cout << "Yes\n"; + continue; + } + a = stoi(t); + cin >> t; + if (t == "is") { + cin >> t >> t; + if (t == "root") { + if (a == root) f = 1; + } else if (t == "parent") { + cin >> t >> b; + if (Tree[b].parent == a) f = 1; + } else if (t == "left") { + cin >> t >> t >> b; + if (Tree[b].lchild == a) f = 1; + } else { + cin >> t >> t >> b; + if (Tree[b].rchild == a) f = 1; + } + } else { + cin >> b >> t >> t; + if (t == "siblings") { + if (Tree[a].parent == Tree[b].parent) f = 1; + } else { + cin >> t >> t >> t; + if (Tree[a].level == Tree[b].level) f = 1; + } + } + cout << (f ? "Yes\n" : "No\n"); + } + return 0; } \ No newline at end of file