-
Notifications
You must be signed in to change notification settings - Fork 814
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎉 feat: initial commit for advanced 1156 to 1159 using C++
- Loading branch information
Showing
4 changed files
with
160 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,25 @@ | ||
#include <cmath> | ||
#include <iostream> | ||
#include <cmath> | ||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,32 @@ | ||
#include <iostream> | ||
#include <unordered_set> | ||
#include <set> | ||
using namespace std; | ||
|
||
int n, m, cnt; | ||
string temp, smallx = "99999999", smalla = "99999999", ansx, ansa; | ||
set<string> record; | ||
int main() { | ||
string id, oldest = "999999999999999999"; | ||
unordered_set<string> 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,52 @@ | ||
#include <iostream> | ||
#include <algorithm> | ||
#include <vector> | ||
using namespace std; | ||
|
||
struct UnionFind { | ||
vector<int> id; | ||
vector<int> 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<vector<int>> durations(n + 1, vector<int>(n + 1)); | ||
while (m--) { | ||
int c, r, d; | ||
cin >> c >> r >> d; | ||
durations[c][r] += d; | ||
} | ||
vector<int> 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<int> temp[n + 1]; | ||
for (auto x : suspects) | ||
temp[uf.Find(x)].push_back(x); | ||
vector<vector<int>> ans; | ||
for (auto gang : temp) | ||
if (!gang.empty()) | ||
ans.push_back(gang); | ||
sort(ans.begin(), ans.end(), [](vector<int> v1, vector<int> 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 <iostream> | ||
#include <vector> | ||
using namespace std; | ||
int k, n, m, c, r, d, p[1001], sc[1001], rec[1001], mark[1001], record[1001][1001]; | ||
vector<int> 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; | ||
} |
148 changes: 69 additions & 79 deletions
148
AdvancedLevel_C++/1159. Structure of a Binary Tree (30).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,70 @@ | ||
#include <iostream> | ||
#include <string> | ||
#include <unordered_map> | ||
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<int, TreeNode*> 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<iostream> | ||
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; | ||
} |