Compare commits

...

14 Commits

Author SHA1 Message Date
9e8efb435c Upload the memories 2025-06-22 22:18:08 +08:00
241c4863c9
P5686 100pts
2024-10-22 22:03:21 +08:00
b2505a5361
P7912 100pts 队列 (used to be using vector, TLE)
2024-10-21 21:41:32 +08:00
8fc5fa15dd
P7072 100pts 桶排序
2024-10-15 21:13:27 +08:00
1bb46a2ec5
P5662 100pts
2024-10-14 21:06:35 +08:00
2ec168d4be
P7915 100pts
2024-10-11 22:00:06 +08:00
f284d184fd P8814 100pts 2024-10-10 21:34:48 +08:00
64e55d4c4b P7910 76pts (TLE) 2024-10-10 21:15:37 +08:00
783cd3980a (wip) P7078 15pts (WA+TLE) 2024-10-09 22:26:11 +08:00
318c907321 (wip) P7911 莫名其妙不一样 https://www.luogu.com.cn/problem/P7911 2024-10-08 23:11:04 +08:00
64a7875d1d 状态压缩动态规划 2024-06-09 14:40:50 +08:00
3f7ee8e8c0 💩 Dancing Links 数独(未完成) 2024-05-26 16:04:00 +08:00
8e3a6a794d 中缀转后缀+表达式计算 2024-05-26 15:44:38 +08:00
edbf5a63d9 主要成分 map 实现 2024-05-26 14:58:02 +08:00
43 changed files with 3542 additions and 9 deletions

3
.gitignore vendored
View File

@ -1 +1,4 @@
*.bin
/cmake-build-debug
/.idea

38
CMakeLists.txt Normal file
View File

@ -0,0 +1,38 @@
cmake_minimum_required(VERSION 3.28)
project(Playground)
set(CMAKE_CXX_STANDARD 14)
include_directories(.)
add_executable(Playground
complete-backup/main.cc
counting-people/main.cc
factorization/main.cc
fastest-way2unified-data/main.cc
flip-card/main.cc
jumping-wooden-trunk/main.cc
longest-decreasing-subsequence/main.cc
palindrome-number/main.cc
pick-class/main.cc
plug-dp/main.cc
reporting-number/main.cc
simple-calculator/main.cc
state-compress-dp/main.cpp
sudoku/main.cc
take-most-space-element/main.cc
walking-in-maze/main.cc
network-connection/main.cpp
eating-snake/main.cc
insert-sort/main.cc
csp-decode/main.cc
palin/main.cc
csp-backpack/main.cc
csp-awarding/main.cc
holiday-plan/main.cc
bears-fruit-basket/main.cc
pre-sum/main.cc
trip-bus/main.cc
processing-parts/main.cc
logical-expressions/main.cc
logical-expressions/main.cc)

18
add-oil/main.cc Normal file
View File

@ -0,0 +1,18 @@
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main() {
int num = 0;
string str;
cin >> str;
if (atoi(str[0]) > 2) {
num++;
}
bool is_feb = atoi(str[1]) == 2;
int days = stoi(str[3].to_string()+str[4].to_string());
if (days > 30 || (is_feb && days > 28)) {
num++;
}
cout << num << endl;
}

View File

@ -0,0 +1,2 @@
12
1 1 0 0 1 1 1 0 1 1 0 0

View File

@ -0,0 +1,68 @@
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct fruit {
int type;
int start;
int end;
fruit(int t, int start, int end) {
this->type = t;
this->start = start;
this->end = end;
}
};
bool took[200000 + 5], fruits[200000 + 5];
int main() {
int n;
queue<fruit> blocks, pending_blocks;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> fruits[i];
}
fruits[n + 1] = !fruits[n]; // Make the last block can be handled
for (int ptr = 2, lead = 1; ptr <= n + 1; ptr++) {
if (fruits[ptr] != fruits[ptr - 1]) {
blocks.emplace(fruits[ptr - 1], lead, ptr - 1);
lead = ptr;
}
}
// 1st -> 1 1 0 0 1 1 1 0 1 1 0 0
// x x x x x x
// 2nd -> 1 0 1 1 0 0
int remain = n;
while (remain) {
while (!blocks.empty()) {
auto front = blocks.front();
blocks.pop();
while (took[front.start] && front.start <= front.end) front.start++;
if (front.start > front.end) continue;
printf("%d ", front.start);
remain--;
took[front.start] = true;
if (front.end == front.start) continue;
front.start++;
pending_blocks.push(front);
}
printf("\n");
while (!pending_blocks.empty()) {
auto front = pending_blocks.front();
pending_blocks.pop();
while (!pending_blocks.empty()) {
auto back = pending_blocks.front();
if (front.type == back.type) {
front.end = back.end;
pending_blocks.pop();
} else {
break;
}
}
blocks.push(front);
}
}
}

View File

@ -4,16 +4,16 @@ using namespace std;
#define MAXN 10005
#define M 105
int dp[MAXN];
int w[M], v[M];
int w[M], state[M];
int x[M];
int main() {
int n, W;
cin >> W >> n;
for (int i = 1; i <= n; i++)
cin >> w[i] >> v[i];
cin >> w[i] >> state[i];
for (int i = 1; i <= n; i++)
for (int j = w[i]; j <= W; j++)
dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
dp[j] = max(dp[j], dp[j - w[i]] + state[i]);
cout << dp[W] << endl;
return 0;
}

25
csp-awarding/main.cc Normal file
View File

@ -0,0 +1,25 @@
#include <iostream>
#include <algorithm>
using namespace std;
int bkt[1005];
int main() {
int n, w;
cin >> n >> w;
for (int i = 0; i < n; i++) {
int ele;
cin >> ele;
bkt[ele]++;
int tot = 0;
for (int j = 600; j >= 0; j--) {
tot += bkt[j];
if (tot >= max(1, (i + 1) * w / 100)) {
printf("%d ", j);
break;
}
}
}
}

7
csp-backpack/data.in Normal file
View File

@ -0,0 +1,7 @@
6 1 100
50
20
25
20
25
50

25
csp-backpack/main.cc Normal file
View File

@ -0,0 +1,25 @@
#include <cstring>
#include <iostream>
using namespace std;
#define maxn 10005
#define M 105
int dp[maxn];
int w[M][M];
int main() {
int T, n, W;
cin >> T >> n >> W;
for (int i = 1; i <= T; i++)
for (int j = 1; j <= n; j++) // <- used to be i++
cin >> w[i][j];
for (int i = 1; i < T; i++) {
memset(dp, 0, sizeof dp);
for (int j = 1; j <= n; j++)
for (int k = w[i][j]; k <= W; k++)
dp[k] = max(dp[k], dp[k - w[i][j]] + w[i + 1][j] - w[i][j]);
W = max(dp[W] + W, W);
}
cout << W << endl;
return 0;
}

11
csp-decode/data.in Normal file
View File

@ -0,0 +1,11 @@
10
770 77 5
633 1 211
545 1 499
683 3 227
858 3 257
723 37 13
572 26 11
867 17 17
829 3 263
528 4 109

25
csp-decode/main.cc Normal file
View File

@ -0,0 +1,25 @@
#include <iostream>
#include <cmath>
using namespace std;
#define ll long long
int main() {
ll k;
cin >> k;
while (k--) {
ll n, d, e;
cin >> n >> d >> e;
ll base1 = n - e * d + 2;
ll var1 = sqrt(base1 * base1 - 4 * n);
ll var2 = n - e * d + 2;
ll p = (var2 + var1) / 2;
ll q = var2 - p;
if (p * q == n && e * d == (p - 1) * (q - 1) + 1 && p && q) {
cout << min(p, q) << " " << max(p, q) << endl;
} else {
cout << "NO" << endl;
}
}
}

5
eating-snake/data.in Normal file
View File

@ -0,0 +1,5 @@
2
3
11 14 14
3
1 5 2 6 3 25

5
eating-snake/data2.in Normal file
View File

@ -0,0 +1,5 @@
2
5
13 31 33 39 42
5
1 7 2 10 3 24 4 48 5 50

21
eating-snake/data3.in Normal file
View File

@ -0,0 +1,21 @@
10
3
29 46 67
3
1 15 2 34 3 114
3
1 120 2 168 3 224
3
1 65 2 132 3 293
3
1 79 2 192 3 474
3
1 24 2 64 3 562
3
1 396 2 458 3 595
3
1 44 2 317 3 592
3
1 138 2 145 3 572
3
1 602 2 726 3 831

144
eating-snake/data3.out Normal file
View File

@ -0,0 +1,144 @@
eating log:
(3, 67)
eating log:
(2, 46)
eating log:
(1, 29)
3 gonna to eat 1
3 give up, cuz it will be eaten after ate that snake, new strongest snake 2
3
alter log:
1 2 3
eating log:
(3, 114)
eating log:
(2, 34)
eating log:
(1, 15)
3 gonna to eat 1
3 has ate 1
eating log:
(3, 99)
eating log:
(2, 34)
3 gonna to eat 2
3 has ate 2
1
alter log:
3
eating log:
(3, 224)
eating log:
(2, 168)
eating log:
(3, 120)
3 gonna to eat 3
3 give up, cuz it will be eaten after ate that snake, new strongest snake 2
3
alter log:
2 3 3
eating log:
(3, 293)
eating log:
(3, 132)
eating log:
(2, 65)
3 gonna to eat 2
3 has ate 2
eating log:
(3, 228)
eating log:
(3, 132)
3 gonna to eat 3
3 has ate 3
1
alter log:
3
eating log:
(3, 474)
eating log:
(2, 192)
eating log:
(3, 79)
3 gonna to eat 3
3 has ate 3
eating log:
(3, 395)
eating log:
(2, 192)
3 gonna to eat 2
3 has ate 2
1
alter log:
3
eating log:
(3, 562)
eating log:
(2, 64)
eating log:
(3, 24)
3 gonna to eat 3
3 has ate 3
eating log:
(3, 538)
eating log:
(2, 64)
3 gonna to eat 2
3 has ate 2
1
alter log:
3
eating log:
(3, 595)
eating log:
(2, 458)
eating log:
(3, 396)
3 gonna to eat 3
3 give up, cuz it will be eaten after ate that snake, new strongest snake 2
3
alter log:
2 3 3
eating log:
(3, 592)
eating log:
(3, 317)
eating log:
(2, 44)
3 gonna to eat 2
3 has ate 2
eating log:
(3, 548)
eating log:
(3, 317)
3 gonna to eat 3
3 has ate 3
1
alter log:
3
eating log:
(3, 572)
eating log:
(2, 145)
eating log:
(3, 138)
3 gonna to eat 3
3 has ate 3
eating log:
(3, 434)
eating log:
(2, 145)
3 gonna to eat 2
3 has ate 2
1
alter log:
3
eating log:
(3, 831)
eating log:
(2, 726)
eating log:
(3, 602)
3 gonna to eat 3
3 give up, cuz it will be eaten after ate that snake, new strongest snake 2
3

141
eating-snake/main.cc Normal file
View File

@ -0,0 +1,141 @@
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define ENABLE_LOG
struct snake {
int id;
int power;
snake(int id, int power) {
this->id = id;
this->power = power;
}
bool operator<(const snake &obj) const {
if (obj.power == power) return obj.id < id;
return obj.power < power;
}
bool operator>=(const snake &obj) const {
return power >= obj.power;
}
};
bool sort_by_id(snake a, snake b) {
return b.id > a.id;
}
bool eat(vector<snake> &vec, bool skip_sort = false, int initial_id = 0) {
if (vec.size() < 2) return true;
if (!skip_sort) {
sort(vec.begin(), vec.end());
}
#if defined(ENABLE_LOG)
for (auto item: vec) {
printf("eating log: \n");
printf("(%d, %d)\n", item.id, item.power);
}
#endif
if (!(vec[initial_id] >= vec[vec.size() - 1])) {
return true;
}
// If this snake ate the last snake
snake curr = vec[0];
snake last = vec[vec.size() - 1];
#if defined(ENABLE_LOG)
printf("%d gonna to eat %d\n", curr.id, last.id);
#endif
vec.pop_back();
vec[initial_id].power -= last.power;
sort(vec.begin(), vec.end());
if (vec[0].id != curr.id) {
bool will_be_eaten = false;
int strongest_curr_power = vec[0].power;
// Looking up if current snake will be eaten
for (auto it = vec.rbegin(); it < vec.rend(); it++) {
if (strongest_curr_power >= it->power) {
if (it->id == curr.id) {
// So bad, the new strongest snake will eat the current strongest snake
// Give up eating right now!
will_be_eaten = true;
break;
}
// No more thinking, just go for it
strongest_curr_power -= it->power;
}
}
if (will_be_eaten) {
#if defined(ENABLE_LOG)
printf("%d give up, cuz it will be eaten after ate that snake, new strongest snake %d\n", curr.id,
vec[0].id);
#endif
// It means the current snake going to be eaten
// Revert the action
for (auto &item: vec) {
if (item.id == curr.id) {
item.power = curr.power;
break;
}
}
vec.push_back(last);
}
}
#if defined(ENABLE_LOG)
printf("%d has ate %d\n", curr.id, last.id);
#endif
// Else, it means the current snake is still strong enough
// Going to see other snakes can be eaten...
return eat(vec, skip_sort = true);
}
int main() {
int count;
cin >> count;
vector<snake> snakes;
int t;
cin >> t;
for (int i = 0; i < t; i++) {
int a;
cin >> a;
snakes.emplace_back(i + 1, a);
}
eat(snakes);
cout << snakes.size() << endl;
for (int i = 0; i < count - 1; i++) {
int n;
cin >> n;
sort(snakes.begin(), snakes.end(), sort_by_id);
#if defined(ENABLE_LOG)
printf("alter log: \n");
for (const auto &item: snakes) {
printf("%d ", item.id);
}
printf("\n");
#endif
for (int j = 0; j < n; j++) {
int id, alter;
cin >> id >> alter;
if (snakes.size() < id) {
// Respawn
snakes.emplace_back(id, alter);
} else {
// Alter power
snakes[id - 1].power = alter;
}
}
eat(snakes);
cout << snakes.size() << endl;
}
}

9
holiday-plan/main.cc Normal file
View File

@ -0,0 +1,9 @@
#include <iostream>
using namespace std;
int main() {
int n, m, k;
cin >> n >> m >> k;
}

6
insert-sort/data.in Normal file
View File

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

38
insert-sort/main.cc Normal file
View File

@ -0,0 +1,38 @@
#include <iostream>
using namespace std;
const int N = 10005;
pair<int, int> arr[N];
int beta[N];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &arr[i].first);
arr[i].second = i;
}
sort(arr + 1, arr + n + 1);
for (int i = 1; i <= n; i++) beta[arr[i].second] = i;
while (m--) {
int op, x, y;
scanf("%d", &op);
if (op == 1) {
scanf("%d%d", &x, &y);
arr[beta[x]].first = y;
for (int i = 1; i < n; i++) {
if (arr[i] > arr[i + 1]) swap(arr[i], arr[i + 1]);
}
for (int i = n - 1; i >= 1; i--) {
if (arr[i] > arr[i + 1]) swap(arr[i], arr[i + 1]);
}
for (int i = 1; i <= n; i++) beta[arr[i].second] = i;
} else {
scanf("%d", &x);
printf("%d\n", beta[x]);
}
}
return 0;
}

View File

@ -1,17 +1,17 @@
#include <iostream>
using namespace std;
const int MAXN = 105;
int dp[MAXN], arr[MAXN];
int dp[MAXN], obstacle[MAXN];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
cin >> obstacle[i];
dp[i] = n - 1;
}
dp[1] = 0;
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= i + arr[i] && j <= n; j++) {
for (int j = i + 1; j <= i + obstacle[i] && j <= n; j++) {
dp[j] = min(dp[j], dp[i] + 1);
}
}

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);
}

View File

@ -3,13 +3,13 @@ using namespace std;
#define MAXN 10005
#define M 200
int dp[MAXN];
int arr[M];
int obstacle[M];
int longest_decreasing_subsequence(int n) {
int maximun = 0;
for (int i = 0; i < n; i++) {
dp[i]++;
for (int j = 0; j < i; j++) {
if (arr[j] > arr[i]) // 最大上升为 `arr[j] < arr[i]`
if (obstacle[j] > obstacle[i]) // 最大上升为 `obstacle[j] < obstacle[i]`
dp[i] = max(dp[i], dp[j] + 1);
}
if (dp[i] > maximun)
@ -21,7 +21,7 @@ int main() {
int N;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> arr[i];
cin >> obstacle[i];
}
int value = longest_decreasing_subsequence(N);
cout << value << endl;

View File

@ -0,0 +1,6 @@
5
Server 192.168.1.1:8080
Server 192.168.1.1:8080
Client 192.168.1.1:8080
Client 192.168.1.1:80
Client 192.168.1.1:99999

View File

@ -0,0 +1,11 @@
10
Server 192.168.1.1:80
Client 192.168.1.1:80
Client 192.168.1.1:8080
Server 192.168.1.1:80
Server 192.168.1.1:8080
Server 192.168.1.999:0
Client 192.168.1.1.8080
Client 192.168.1.1:8080
Client 192.168.1.1:80
Client 192.168.1.999:0

102
network-connection/data3.in Normal file
View File

@ -0,0 +1,102 @@
100
Server 249.217.185.41:28390
Server 58.139.75.150:32916
Server 164.132.230.103:2805
Server 230.34.60.10:3192
Server 8.158.211.70:45542
Server 102.194.238.18:15088
Server 101.59.241.186:52427
Server 115.143.108.72:16537
Server 161.221.13.212:52047
Server 7.75.149.236:11770
Server 108.220.137.165:26957
Server 121.180.139.110:5392
Server 12.214.2.248:41461
Server 158.68.44.93:53435
Server 97.63.72.59:35064
Server 220.187.86.78:64381
Server 59.29.128.91:2717
Server 140.175.106.200:30566
Server 245.233.205.228:55782
Server 205.24.170.47:55248
Server 103.60.29.80:37007
Server 223.76.148.124:12954
Server 80.112.212.73:31448
Server 244.221.32.22:12816
Server 140.140.178.160:24540
Server 98.185.111.15:42673
Server 219.249.194.5:48754
Server 80.139.152.191:47985
Server 103.98.92.158:15313
Server 112.94.91.98:9353
Server 203.152.232.46:8570
Server 64.7.69.2:53535
Server 70.77.237.205:48715
Server 63.190.182.194:22734
Server 167.198.175.203:52975
Server 231.18.52.239:65074
Server 30.65.214.84:14567
Server 185.76.45.166:49771
Server 173.43.69.196:13369
Server 85.11.251.17:65259
Server 151.72.42.94:5341
Server 169.96.64.212:12779
Server 114.141.130.157:47652
Server 217.0.167.240:30040
Server 187.253.126.242:22788
Server 33.61.94.253:4080
Server 254.44.190.153:24278
Server 146.243.101.66:32070
Server 80.79.84.224:49562
Server 63.81.148.7:28810
Client 81.235.246.73:18305
Client 59.29.128.91:2717
Client 33.61.94.253:4080
Client 220.120.144.227:3377
Client 64.7.69.2:53535
Client 58.139.75.150:32916
Client 187.253.126.242:22788
Client 100.157.115.70:35513
Client 102.194.238.18:15088
Client 103.60.29.80:37007
Client 140.140.178.160:24540
Client 203.152.232.46:8570
Client 63.190.182.194:22734
Client 231.18.52.239:65074
Client 54.38.47.208:49299
Client 161.221.13.212:52047
Client 219.146.191.132:46238
Client 219.249.194.5:48754
Client 63.81.148.7:28810
Client 103.98.92.158:15313
Client 121.180.139.110:5392
Client 173.43.69.196:13369
Client 244.221.32.22:12816
Client 195.217.161.202:296
Client 200.1.184.95:61373
Client 217.0.167.240:30040
Client 239.182.127.128:4536
Client 49.50.201.196:52090
Client 30.65.214.84:14567
Client 61.35.102.136:21585
Client 12.214.2.248:41461
Client 194.36.60.51:11079
Client 37.61.94.253:4080
Client 34.213.35.199:47886
Client 8.158.211.70:45542
Client 63.81.148.0:28810
Client 209.249.194.5:48754
Client 7.75.149.236:11770
Client 228.171.11.185:11107
Client 95.18.63.74:30162
Client 116.241.162.73:63458
Client 249.207.124.200:48831
Client 223.76.148.124:12954
Client 70.77.237.205:48715
Client 35.29.90.180:59047
Client 164.132.230.103:2805
Client 59.29.128.91:27178
Client 86.190.70.70:20998
Client 59.29.128.91:2517
Client 139.200.25.104:52480

View File

@ -0,0 +1,100 @@
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
FAIL
17
46
FAIL
32
2
45
FAIL
6
21
25
31
34
36
FAIL
9
FAIL
27
50
29
12
39
24
FAIL
FAIL
44
FAIL
FAIL
37
FAIL
13
FAIL
FAIL
FAIL
5
FAIL
FAIL
10
FAIL
FAIL
FAIL
FAIL
22
33
FAIL
3
FAIL
FAIL
FAIL
FAIL

1002
network-connection/data4.in Normal file

File diff suppressed because it is too large Load Diff

1000
network-connection/data4.out Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,57 @@
#include <iostream>
#include <string>
#include <sstream>
#include <map>
#include <cstring>
using namespace std;
bool validate_addr(const string &addr) {
if (addr.size() > 22) return false;
int ip_parts[4];
int port;
sscanf(addr.c_str(), "%d.%d.%d.%d:%d", &ip_parts[0], &ip_parts[1], &ip_parts[2], &ip_parts[3], &port);
char out[35];
sprintf(out, "%d.%d.%d.%d:%d", ip_parts[0], ip_parts[1], ip_parts[2], ip_parts[3], port);
if (strlen(out) != addr.size()) return false; // Leading 0
if (port > 65536 || port < 0) return false;
for (int ip_part: ip_parts) {
if (ip_part > 256 || ip_part < 0) return false;
}
return true;
}
int main() {
int n;
map<string, int> servers;
cin >> n;
for (int i = 0; i < n; i++) {
string mode, t;
cin >> mode >> t;
if (!validate_addr(t)) {
cout << "ERR" << endl;
continue;
}
if (mode == "Server") {
if (servers.find(t) == servers.end()) {
// Not found, good to go
servers[t] = i + 1;
cout << "OK" << endl;
} else {
cout << "FAIL" << endl;
}
}
if (mode == "Client") {
if (servers.find(t) == servers.end()) {
cout << "FAIL" << endl;
} else {
cout << servers[t] << endl;
}
}
}
}

5
palin/data.in Normal file
View File

@ -0,0 +1,5 @@
2
5
4 1 2 4 5 3 1 2 3 5
3
3 2 1 2 1 3

79
palin/main.cc Normal file
View File

@ -0,0 +1,79 @@
#include <iostream>
#include <deque>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1000005;
int arr[N], brr[N], crr[N], n;
bool calc(char op) {
deque<int> a, b;
string out, append;
out.push_back(op);
append.push_back('L');
if (op == 'L') {
for (int i = 2; i < crr[1]; i++) a.push_back(i);
for (int i = n; i > crr[1]; i--) b.push_back(i);
} else {
for (int i = 1; i < crr[n]; i++) a.push_back(i);
for (int i = n - 1; i > crr[n]; i--) b.push_back(i);
}
while (!a.empty() || !b.empty()) {
int x1 = !a.empty() ? a.front() : 0;
int x2 = !b.empty() ? b.front() : 0;
int y1 = !a.empty() ? a.back() : 0;
int y2 = !b.empty() ? b.back() : 0;
if (crr[x1] == y1) {
out.push_back('L');
append.push_back('L');
a.pop_front();
a.pop_back();
} else if (crr[x1] == y2) {
out.push_back('L');
append.push_back('R');
a.pop_front();
b.pop_back();
} else if (crr[x2] == y1) {
out.push_back('R');
append.push_back('L');
b.pop_front();
a.pop_back();
} else if (crr[x2] == y2) {
out.push_back('R');
append.push_back('R');
b.pop_front();
b.pop_back();
} else {
return false;
}
}
reverse(append.begin(), append.end());
out += append;
cout << out << endl;
return true;
}
int main() {
int T;
cin >> T;
while (T--) {
cin >> n;
memset(brr, 0, sizeof brr);
n *= 2;
crr[0] = -1;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
if (brr[arr[i]]) {
crr[brr[arr[i]]] = i;
crr[i] = brr[arr[i]];
} else {
brr[arr[i]] = i;
}
}
if (!calc('L') && !calc('R')) cout << -1 << endl;
}
}

3
pre-sum/data.in Normal file
View File

@ -0,0 +1,3 @@
3
2 3 4
3 4 5

30
pre-sum/main.cc Normal file
View File

@ -0,0 +1,30 @@
#include <iostream>
using namespace std;
long long alpha[500005], beta[500005];
long long pre_alpha[500005], pre_beta[500005];
long long sum_alpha, sum_beta;
long long ans;
const int mod = 1000000007;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> alpha[i];
pre_alpha[i] = (pre_alpha[i - 1] + alpha[i]) % mod;
}
for (int i = 1; i <= n; ++i) {
cin >> beta[i];
pre_beta[i] = (pre_beta[i - 1] + beta[i]) % mod;
}
for (int i = 1; i <= n; i++) {
(ans += (((n + 1) * pre_alpha[i]) % mod) * pre_beta[i]) %= mod;
sum_alpha = (sum_alpha + pre_alpha[i]) % mod;
sum_beta = (sum_beta + pre_beta[i]) % mod;
}
long long tot = (sum_alpha * sum_beta) % mod;
ans = (ans - tot + mod) % mod;
cout << ans;
}

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;
}
}
}

View File

@ -0,0 +1,2 @@
100
40 32 2 42 47 7 24 14 11 7 19 3 44 49 44 32 4 28 4 48 4 7 41 32 42 41 27 4 20 40 44 30 13 44 8 10 16 46 50 18 20 15 4 32 22 25 10 2 17 10 49 4 47 10 41 28 3 3 26 22 13 40 29 14 9 1 43 21 19 20 10 12 40 6 14 9 14 46 25 50 17 45 1 30 5 37 8 5 47 43 39 12 25 1 40 20 2 10 14 21

View File

@ -0,0 +1,24 @@
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int k;
cin >> k;
vector<int> arr;
for (int i = 0; i < k; i++) {
int t;
cin >> t;
arr.push_back(t);
}
auto ptr = unique(arr.begin(), arr.end());
arr.erase(ptr, arr.end());
if (arr.size() <= 2) {
cout << -1 << endl;
return 0;
}
sort(arr.begin(), arr.end());
arr.insert(arr.begin(), 1, 0);
auto kp = arr.size()-1;
cout << max(arr[kp - 2], arr[kp] % arr[kp - 1]) << endl;
}

76
simple-calculator/main.cc Normal file
View File

@ -0,0 +1,76 @@
#include <cmath>
#include <iostream>
#include <stack>
#include <unordered_map>
using namespace std;
unordered_map<char, int> priority{{'(', 0}, {')', 0}, {'+', 1}, {'-', 1},
{'*', 2}, {'/', 2}, {'^', 3}};
string in_order2post_order(string src) {
stack<char> in;
string builder;
for (int i = 0; i < src.size(); i++) {
if (src[i] == ' ')
continue;
if (isdigit(src[i]))
builder += src[i];
else {
builder += ' ';
while (!in.empty() && priority[in.top()] >= priority[src[i]]) {
builder += in.top();
in.pop();
}
in.push(src[i]);
}
}
if (builder[builder.size() - 1] != ' ')
builder += ' ';
while (!in.empty()) {
builder += in.top();
in.pop();
}
return builder;
}
int eval(string src) {
stack<int> in;
int num_builder = 0;
for (int i = 0; i < src.size(); i++) {
if (isdigit(src[i])) {
num_builder = num_builder * 10 + src[i] - '0';
} else if (src[i] == ' ') {
in.push(num_builder);
num_builder = 0;
} else {
int y = in.top();
in.pop();
int x = in.top();
in.pop();
switch (src[i]) {
case '+':
in.push(x + y);
break;
case '-':
in.push(x - y);
break;
case '*':
in.push(x * y);
break;
case '/':
in.push(x / y);
break;
case '^':
in.push(pow(x, y));
break;
}
}
}
return in.top();
}
int main() {
string str;
getline(cin, str);
cout << eval(in_order2post_order(str)) << endl;
}

View File

@ -0,0 +1,64 @@
#include <iostream>
#include <vector>
using namespace std;
int m, n;
int obstacle[101];
int dp[101][1025];
const int MOD_FACTOR = 100000007;
vector<int> state;
void init_state() {
state.push_back(0);
for (int i = 0; i < (1 << n); i++) {
if (!(i & i << 1)) {
state.push_back(i);
}
}
}
void calculate() {
for (int j = 1; j < state.size(); j++) {
if (!(state[j] & obstacle[1])) { // Check overlap
dp[1][j] = 1;
}
}
for (int i = 2; i <= m; i++) {
for (int j = 1; j < state.size(); j++) {
if (!(state[j] & obstacle[i])) { // Check obstacle overlap
for (int k = 1; k < state.size(); k++) {
if (!(state[j] & state[k])) { // Check overlap
dp[i][j] = (dp[i][j] + dp[i - 1][k]) % MOD_FACTOR;
}
}
}
}
}
}
int main() {
cin >> m >> n;
for (int i = 1; i <= m; i++) {
obstacle[i] = 0;
int num;
for (int j = 1; j <= n; j++) {
cin >> num;
if (num == 0) {
obstacle[i] += (1 << (n - j));
}
}
}
init_state();
calculate();
int ans = 0;
for (int j = 1; j < state.size(); j++) {
ans = (ans + dp[m][j]) % MOD_FACTOR;
}
cout << ans - 1 << endl;
return 0;
}

134
sudoku/main.cc Normal file
View File

@ -0,0 +1,134 @@
#include <cstdio>
#include <cstring>
using namespace std;
#define mx 100100
int sum = 0;
int arr[10][10];
inline int read() {
char c = getchar();
if (c == '.')
c = '0';
while (c < '0' || c > '9') {
c = getchar();
if (c == '.')
c = '0';
}
return c - '0';
}
struct dancing_link {
int n, m, cnt;
int l[mx], r[mx], u[mx], d[mx], row[mx], col[mx];
int h[mx];
int s[mx];
int ansk[mx];
void init(int _n, int _m) {
n = _n, m = _m;
int i;
for (i = 0; i <= m; i++) {
r[i] = i + 1;
l[i] = i - 1;
u[i] = d[i] = i;
}
r[m] = 0;
l[0] = m;
memset(h, -1, sizeof(h));
memset(s, 0, sizeof(s));
cnt = m + 1;
}
void link(int R, int C) {
s[C]++;
row[cnt] = R;
col[cnt] = C;
u[cnt] = C;
d[cnt] = d[C];
u[d[C]] = cnt;
d[C] = cnt;
if (h[R] < 0)
h[R] = r[cnt] = l[cnt] = cnt;
else {
r[cnt] = h[R];
l[cnt] = l[h[R]];
r[l[h[R]]] = cnt;
l[h[R]] = cnt;
}
cnt++;
}
void remove(int c) {
r[l[c]] = r[c], l[r[c]] = l[c];
for (int i = d[c]; i != c; i = d[i]) {
for (int j = r[i]; j != i; j = r[j]) {
u[d[j]] = u[j];
d[u[j]] = d[j];
s[col[j]]--;
}
}
}
void resume(int c) {
for (int i = u[c]; i != c; i = u[i]) {
for (int j = l[i]; j != i; j = l[j]) {
u[d[j]] = j;
d[u[j]] = j;
s[col[j]]++;
}
}
r[l[c]] = c;
l[r[c]] = c;
}
bool dance(int deep) {
if (r[0] == 0) {
int x, y, v;
for (int i = 0; i < deep; i++) {
x = (ansk[i] - 1) / 9 / 9;
y = (ansk[i] - 1) / 9 % 9;
v = (ansk[i]) % 9;
if (v == 0)
v = 9;
arr[x][y] = v;
}
return 1;
}
int c = r[0];
for (int i = r[0]; i != 0; i = r[i])
if (s[i] < s[c])
c = i;
remove(c);
for (int i = d[c]; i != c; i = d[i]) {
ansk[deep] = row[i];
for (int j = r[i]; j != i; j = r[j])
remove(col[j]);
if (dance(deep + 1) == 1)
return 1;
for (int j = l[i]; j != i; j = l[j])
resume(col[j]);
}
resume(c);
return false;
}
} dlx;
int main() {
dlx.init(729, 324);
int x;
int o;
for (int i = 0; i <= 8; i++) {
for (int j = 0; j <= 8; j++) {
arr[i][j] = x = read();
for (int k = 1; k <= 9; k++) {
if (x != k && x != 0)
continue;
o = i * 9 * 9 + j * 9 + k;
dlx.link(o, i * 9 + j + 1);
dlx.link(o, i * 9 + 81 + k);
dlx.link(o, j * 9 + 81 * 2 + k);
dlx.link(o, 81 * 3 + (i / 3 * 3 + j / 3) * 9 + k);
}
}
}
dlx.dance(0);
for (int i = 0; i <= 8; i++) {
for (int j = 0; j <= 8; j++)
printf("%d", arr[i][j]);
printf("\n");
}
return 0;
}

View File

@ -0,0 +1,33 @@
#include <unordered_map>
using namespace std;
int read() {
int x = 0, w = 1;
char ch = 0;
while (ch < '0' || ch > '9') {
if (ch == '-')
w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + (ch - '0');
ch = getchar();
}
return x * w;
}
int main() {
int n = read();
unordered_map<int, int> u;
for (int i = 0; i < n; i++) {
int val = read();
u[val]++;
}
for (const auto &[key, value] : u) {
if (value >= n / 2) {
printf("%d\n", key);
return 0;
}
}
printf("No\n");
}

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;
}