Upload the memories

This commit is contained in:
LittleSheep 2025-06-22 22:17:57 +08:00
parent 241c4863c9
commit 9e8efb435c
6 changed files with 219 additions and 1 deletions

View File

@ -31,4 +31,8 @@ add_executable(Playground
csp-awarding/main.cc
holiday-plan/main.cc
bears-fruit-basket/main.cc
pre-sum/main.cc)
pre-sum/main.cc
trip-bus/main.cc
processing-parts/main.cc
logical-expressions/main.cc
logical-expressions/main.cc)

View File

@ -0,0 +1 @@
0&(1|0)|(1|1|1&0)

View File

@ -0,0 +1,91 @@
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
string expr;
vector<char> expr_builder;
stack<char> op_builder;
void in_order2post_order() {
for (char ele: expr) {
if (ele == '0' || ele == '1')expr_builder.push_back(ele);
else if (ele == '(')op_builder.push(ele);
else if (ele == ')') {
while (!op_builder.empty() && op_builder.top() != '(') {
expr_builder.push_back(op_builder.top());
op_builder.pop();
}
op_builder.pop();
} else if (ele == '&') {
while (!op_builder.empty() && op_builder.top() == '&') {
expr_builder.push_back(op_builder.top());
op_builder.pop();
}
op_builder.push('&');
} else if (ele == '|') {
while (!op_builder.empty() && op_builder.top() != '(') {
expr_builder.push_back(op_builder.top());
op_builder.pop();
}
op_builder.push('|');
}
}
while (!op_builder.empty()) {
expr_builder.push_back(op_builder.top());
op_builder.pop();
}
}
const int N = 1e6 + 5;
struct node {
int v, left, right;
} ast[N];
int plant_tree() {
int idx = 0;
stack<int> st;
for (auto item: expr_builder) {
if (isdigit(item)) {
ast[++idx] = {item - '0', -1, -1};
st.push(idx);
} else {
int right = st.top();
st.pop();
int left = st.top();
st.pop();
ast[++idx] = {(item == '&' ? 2 : 3), left, right};
st.push(idx);
}
}
return idx;
}
int short_circuit_and = 0, short_circuit_or = 0;
int calc(int start) {
// Digit
if (ast[start].v == 0 || ast[start].v == 1) return ast[start].v;
// Operator
int left = calc(ast[start].left);
if (left == 0 && ast[start].v == 2) { // AND operator
short_circuit_and++;
return 0;
} else if (left == 1 && ast[start].v == 3) { // OR operator
short_circuit_or++;
return 1;
}
int right = calc(ast[start].right);
return right;
}
int main() {
cin >> expr;
in_order2post_order();
int start = plant_tree();
cout << calc(start) << endl;
printf("%d %d\n", short_circuit_and, short_circuit_or);
}

9
processing-parts/data.in Normal file
View File

@ -0,0 +1,9 @@
3 2 6
1 2
2 3
1 1
2 1
3 1
1 2
2 2
3 2

60
processing-parts/main.cc Normal file
View File

@ -0,0 +1,60 @@
#include <iostream>
#include <cstring>
using namespace std;
const int MAXN = 10005;
vector<int> graph[MAXN];
bool visited[MAXN];
int dis_odd[MAXN], dis_even[MAXN];
void find_shortest_path() {
memset(dis_even, 0x3f, sizeof dis_even);
memset(dis_odd, 0x3f, sizeof dis_odd);
queue<pair<int, int>> q;
for (int &i: graph[1]) {
dis_odd[i] = 1;
q.emplace(i, 1);
}
while (!q.empty()) {
int x = q.front().first, y = q.front().second;
for (int &i: graph[x]) {
if (y % 2 == 0) {
if (y + 1 < dis_odd[i]) {
dis_odd[i] = y + 1;
q.emplace(i, y + 1);
}
} else {
if (y + 1 < dis_even[i]) {
dis_even[i] = y + 1;
q.emplace(i, y + 1);
}
}
}
q.pop();
}
}
int main() {
int n, m, q;
cin >> n >> m >> q;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
find_shortest_path();
while (q--) {
int u, v;
cin >> u >> v;
if (v % 2 == 0) {
if (dis_even[u] > v) cout << "No" << endl;
else cout << "Yes" << endl;
} else {
if (dis_odd[u] > v) cout << "No" << endl;
else cout << "Yes" << endl;
}
}
}

53
trip-bus/main.cc Normal file
View File

@ -0,0 +1,53 @@
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
int dp[10005][105];
bool visited[10005][105];
struct node {
node(int i, int i1, int i2) {
this->u = i;
this->idx = i1;
this->d = i2;
}
int u, idx, d;
bool operator<(const node &other) const {
return d > other.d;
}
};
vector<pair<int, int>> graph[10005];
int main() {
int n, m, k;
cin >> n >> m >> k;
for (int i = 0; i < m; i++) {
int u, v, a;
cin >> u >> v >> a;
graph[u].emplace_back(v, a);
}
memset(dp, 0x3f, sizeof dp);
priority_queue<node> tasks;
tasks.emplace(1, 0, dp[1][0] = 0);
while (!tasks.empty()) {
int u = tasks.top().u;
int idx = tasks.top().idx;
tasks.pop();
if (visited[u][idx]) continue;
else visited[u][idx] = true;
for (auto [v, w]: graph[u]) {
int time = dp[u][idx];
int j = (idx + 1) % k;
if (time < w) time += (w - time + k - 1) / k * k;
if (dp[v][j] > time + 1) tasks.emplace(v, j, dp[v][j] = time + 1);
}
}
if (dp[n][0] == 0x3f3f3f3f) dp[n][0] = 1;
cout << dp[n][0] << endl;
}