complete 7604.cpp 1267.rs 8760.rs

This commit is contained in:
2026-01-26 22:17:44 -08:00
parent 1a55a65ed7
commit de4d2c6afd
3 changed files with 156 additions and 0 deletions

View File

@@ -0,0 +1,95 @@
#include <iostream>
#include <vector>
using namespace std;
#define let auto
void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
}
auto transform_to_postorder(vector<char> preorder, vector<char> inorder, vector<char> &postorder) -> bool {
if (preorder.size() == 0) {
return true;
}
let root = preorder[0];
if (preorder.size() == 1) {
if (preorder[0] == inorder[0]) {
postorder[0] = root;
return true;
} else {
return false;
}
}
let find = inorder.begin();
for (find = inorder.begin(); find != inorder.end(); find++) {
if (*find == root) {
break;
}
}
if (find == inorder.end()) {
return false;
}
let left_subtree_inorder = vector<char>(inorder.begin(), find);
let right_subtree_inorder = vector<char>(find + 1, inorder.end());
let left_size = left_subtree_inorder.size();
let right_size = right_subtree_inorder.size();
let left_subtree_preorder = vector<char>(preorder.begin() + 1, preorder.begin() + 1 + left_size);
let right_subtree_preorder = vector<char>(preorder.begin() + 1 + left_size, preorder.end());
// propagate
let left_sub_post = vector<char>(left_subtree_inorder.size());
let right_sub_post = vector<char>(right_subtree_inorder.size());
let sig_left = transform_to_postorder(
left_subtree_preorder, left_subtree_inorder, left_sub_post);
let sig_right = transform_to_postorder(
right_subtree_preorder, right_subtree_inorder, right_sub_post);
if (sig_left == false || sig_right == false) {
return false;
}
std::copy(
left_sub_post.begin(), left_sub_post.end(), postorder.begin());
std::copy(
right_sub_post.begin(), right_sub_post.end(), postorder.begin() + left_sub_post.size());
postorder[postorder.size() - 1] = root;
return true;
}
auto main() -> int {
fastio();
string pre_str, in_str;
while (cin >> pre_str && pre_str != "#") {
cin >> in_str;
vector<char> preorders(pre_str.begin(), pre_str.end());
vector<char> inorders(in_str.begin(), in_str.end());
vector<char> postorders(preorders.size());
if (transform_to_postorder(preorders, inorders, postorders)) {
for (let c: postorders) {
cout << c;
}
cout << "\n";
} else {
cout << "Invalid tree\n";
}
}
return 0;
}

View File

@@ -0,0 +1,34 @@
use std::io::{read_to_string, stdin};
fn main() {
let temp = read_to_string(stdin()).unwrap();
let mut iter = temp
.split_ascii_whitespace()
.map(|x| x.parse::<usize>().unwrap());
let n = iter.next().unwrap();
let times_call = (0..n).map(|_| iter.next().unwrap()).collect::<Vec<usize>>();
let y: usize = times_call
.iter()
.map(|x| {
let d = x / 30;
d * 10 + 10
})
.sum();
let m: usize = times_call
.iter()
.map(|x| {
let d = x / 60;
d * 15 + 15
})
.sum();
if y < m {
println!("Y {}", y);
} else if y > m {
println!("M {}", m);
} else {
println!("Y M {}", y);
}
}

View File

@@ -0,0 +1,27 @@
use std::io::{read_to_string, stdin};
fn main() {
let temp = read_to_string(stdin()).unwrap();
let mut iter = temp
.split_ascii_whitespace()
.map(|x| x.parse::<usize>().unwrap());
let z = iter.next().unwrap();
(0..z).for_each(|_| {
let (w, k) = (iter.next().unwrap(), iter.next().unwrap());
let w_r = w % 2;
let k_r = k % 2;
let res = if w_r == 0 {
w * k / 2
} else if k_r == 0 {
w * k / 2
} else {
w * (k - 1) / 2 + (w - 1) / 2
};
println!("{}", res);
});
}