problemscpp
A collection of my answers to algorithm problems in c++.
acwing408.cpp
浏览该文件的文档.
1//
2// Created by tategotoazarasi on 24-5-7.
3//
4
5#include "acwing408.h"
6#include "templates.h"
7#include <algorithm>
8#include <climits>
9#include <cmath>
10#include <cstdlib>
11#include <cstring>
12#include <iomanip>
13#include <list>
14#include <queue>
15#include <sstream>
16#include <stack>
17#include <unordered_map>
18#include <vector>
19
20using namespace std;
21
22namespace acwing {
23 namespace acwing3378 {
24 int main(istream &cin, ostream &cout) {
25 int n;
26 int rule;
27 cin >> n >> rule;
28 unordered_map<student *, unsigned> index;
29 vector<student *> students(n);
30 for(int i = 0; i < n; i++) {
31 students[i] = new student();
32 cin >> students[i]->name >> students[i]->score;
33 if(index.find(students[i]) == index.end())
34 index[students[i]] = i;
35 }
36 sort(students.begin(), students.end(), [rule, &index](student *a, student *b) {
37 if(a->score == b->score) {
38 return index[a] < index[b];
39 }
40 if(rule == 0) {
41 return a->score > b->score;
42 } else {
43 return a->score < b->score;
44 }
45 });
46 for(const auto &s: students) {
47 cout << s->name << ' ' << s->score << endl;
48 }
49 return 0;
50 }
51 }// namespace acwing3378
52
53 namespace acwing3376 {
54 int main(istream &cin, ostream &cout) {
55 int n;
56 cin >> n;
57 vector<student> students(n);
58 for(int i = 0; i < n; i++) {
59 cin >> students[i].id >> students[i].score;
60 students[i].id_numeric = stoi(students[i].id);
61 }
62 sort(students.begin(), students.end(), [](const student &a, const student &b) {
63 if(a.score == b.score) {
64 return a.id_numeric < b.id_numeric;
65 }
66 return a.score < b.score;
67 });
68 for(const auto &s: students) {
69 cout << s.id << ' ' << s.score << endl;
70 }
71 return 0;
72 }
73 }// namespace acwing3376
74
75 namespace acwing3374 {
76 int main(istream &cin, ostream &cout) {
77 int m, n;
78 string x;
79 cin >> m >> n >> x;
80 if(x == "0") {
81 cout << "0";
82 return 0;
83 }
84 vector<unsigned short> input = vector<unsigned short>();
85 vector<unsigned short> ans = vector<unsigned short>();
86 queue<unsigned short> quotient = queue<unsigned short>();
87 for(auto i = x.begin(); i != x.end(); i++) {
88 if(isdigit(*i)) {
89 input.push_back(*i - '0');
90 } else if(isupper(*i)) {
91 input.push_back(*i - 'A' + 10);
92 }
93 }
94 do {
95 unsigned int current = 0;
96 quotient = queue<unsigned short>();
97 for(auto i = input.begin(); i != input.end(); i++) {
98 current *= m;
99 current += *i;
100 quotient.push(current / n);
101 current %= n;
102 }
103 while(!quotient.empty() && quotient.front() == 0) {
104 quotient.pop();
105 }
106 ans.push_back(current);
107 input = vector<unsigned short>(quotient.size());
108 for(unsigned int i = 0; i < input.size(); i++) {
109 input[i] = quotient.front();
110 quotient.pop();
111 }
112 } while(!input.empty());
113 for(auto i = ans.rbegin(); i != ans.rend(); i++) {
114 if(*i < 10) {
115 cout << *i;
116 } else {
117 cout << (char) (*i - 10 + 'a');
118 }
119 }
120 return 0;
121 }
122 }// namespace acwing3374
123
124 namespace acwing3757 {
125 void rearrangedList(struct ListNode *head) {
126 if(head == NULL || head->next == NULL)
127 return;
128 int len = 0;
129 for(struct ListNode *p = head; p != NULL; p = p->next)
130 len++;
131 int mid = (len + 1) / 2;
132 struct ListNode *a = head;
133 for(int i = 0; i < mid - 1; i++) {
134 a = a->next;
135 }
136 struct ListNode *b = a->next;
137 struct ListNode *c = b->next;
138 a->next = NULL;
139 b->next = NULL;
140 while(c) {
141 struct ListNode *tmp = c->next;
142 c->next = b;
143 b = c;
144 c = tmp;
145 }
146 struct ListNode *p = head;
147 struct ListNode *q = b;
148 while(q) {
149 auto qq = q->next;
150 // 插入结点
151 q->next = p->next;
152 p->next = q;
153 // 移动p和q
154 p = q->next;
155 q = qq;
156 }
157 }
158 }// namespace acwing3757
159
160 namespace acwing3607 {
161 int main(istream &cin, ostream &cout) {
162 int year, day;
163 int day_of_month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
164 int day_of_month_leap[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
165 int start_day_of_month[14] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, INT_MAX};
166 while(cin >> year >> day) {
167 bool leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
168 if(leap) {
169 for(int i = 1; i <= 12; i++) {
170 start_day_of_month[i] = start_day_of_month[i - 1] + day_of_month_leap[i - 1];
171 }
172 } else {
173 for(int i = 1; i <= 12; i++) {
174 start_day_of_month[i] = start_day_of_month[i - 1] + day_of_month[i - 1];
175 }
176 }
177 cout << setw(4) << setfill('0') << year << '-';
178 for(int i = 1; i <= 13; i++) {
179 if(start_day_of_month[i] > day) {
180 cout << setw(2) << setfill('0') << i - 1 << '-';
181 day -= start_day_of_month[i - 1];
182 day++;
183 cout << setw(2) << setfill('0') << day << endl;
184 break;
185 }
186 }
187 memset(start_day_of_month, 0, sizeof(start_day_of_month));
188 start_day_of_month[0] = 1;
189 start_day_of_month[13] = INT_MAX;
190 }
191 return 0;
192 }
193 }// namespace acwing3607
194
195 namespace acwing3573 {
196 int main(istream &cin, ostream &cout) {
197 int t, year, month, day, a;
198 int day_of_month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
199 int day_of_month_leap[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
200 int(*day_of_month_p)[13] = nullptr;
201
202 cin >> t;
203 while(t--) {
204 cin >> year >> month >> day >> a;
205 day_of_month_p = ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? &day_of_month_leap : &day_of_month;
206 day += a;
207 while(day > (*day_of_month_p)[month]) {
208 day -= (*day_of_month_p)[month];
209 month++;
210 if(month > 12) {
211 month = 1;
212 year++;
213 day_of_month_p = ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? &day_of_month_leap : &day_of_month;
214 }
215 }
216 cout << setw(4) << setfill('0') << year << '-' << setw(2) << setfill('0') << month << '-' << setw(2) << setfill('0') << day << endl;
217 }
218 return 0;
219 }
220 }// namespace acwing3573
221
222 namespace acwing3302_408 {
223 int main(istream &cin, ostream &cout) {
224 int x;
225 char y;
226 unordered_map<char, int> priority = {
227 {'+', 1},
228 {'-', 1},
229 {'*', 2},
230 {'/', 2}};
231 stack<char> op = stack<char>();
232 stack<int> num = stack<int>();
233 while(!cin.eof() || !cin.fail() || !cin.bad()) {
234 if(cin.peek() == EOF)
235 break;
236 if(isdigit(cin.peek())) {
237 cin >> x;
238 num.push(x);
239 } else {
240 cin >> y;
241 if(y == '(') {
242 op.push(y);
243 } else if(y == ')') {
244 while(!op.empty() && op.top() != '(') {
245 int b = num.top();
246 num.pop();
247 int a = num.top();
248 num.pop();
249 char c = op.top();
250 op.pop();
251 if(c == '+') {
252 num.push(a + b);
253 } else if(c == '-') {
254 num.push(a - b);
255 } else if(c == '*') {
256 num.push(a * b);
257 } else if(c == '/') {
258 num.push(a / b);
259 }
260 }
261 op.pop();
262 } else if(priority.find(y) != priority.end()) {
263 while(!op.empty() && op.top() != '(' && priority[op.top()] >= priority[y]) {
264 int b = num.top();
265 num.pop();
266 int a = num.top();
267 num.pop();
268 char c = op.top();
269 op.pop();
270 if(c == '+') {
271 num.push(a + b);
272 } else if(c == '-') {
273 num.push(a - b);
274 } else if(c == '*') {
275 num.push(a * b);
276 } else if(c == '/') {
277 num.push(a / b);
278 }
279 }
280 op.push(y);
281 }
282 }
283 }
284 while(!op.empty()) {
285 int b = num.top();
286 num.pop();
287 int a = num.top();
288 num.pop();
289 char c = op.top();
290 op.pop();
291 if(c == '+') {
292 num.push(a + b);
293 } else if(c == '-') {
294 num.push(a - b);
295 } else if(c == '*') {
296 num.push(a * b);
297 } else if(c == '/') {
298 num.push(a / b);
299 }
300 }
301 cout << num.top();
302 return 0;
303 }
304 }// namespace acwing3302_408
305
306 namespace acwing3766 {
308 return pathSum(root, 0);
309 }
310
311 int pathSum(TreeNode *root, int level) {
312 if(root == nullptr) {
313 return 0;
314 }
315 if(root->left == nullptr && root->right == nullptr) {
316 return level * root->val;
317 }
318 int leftVal = pathSum(root->left, level + 1);
319 int rightVal = pathSum(root->right, level + 1);
320 return leftVal + rightVal;
321 }
322 }// namespace acwing3766
323
324 namespace acwing148 {
325 int main(istream &cin, ostream &cout) {
326 priority_queue<int, vector<int>, greater<>> pq = priority_queue<int, vector<int>, greater<>>();
327 int n;
328 cin >> n;
329 int x;
330 while(n--) {
331 cin >> x;
332 pq.push(x);
333 }
334 int ans = 0;
335 while(pq.size() > 1) {
336 int a = pq.top();
337 pq.pop();
338 int b = pq.top();
339 pq.pop();
340 ans += a + b;
341 pq.push(a + b);
342 }
343 cout << ans;
344 return 0;
345 }
346 }// namespace acwing148
347
348 namespace acwing836_408 {
349 vector<int> root;
350
351 int find(int x) {
352 if(root[x] < 0)
353 return x;
354 else {
355 int grand_parent = find(root[x]);
356 root[x] = grand_parent;
357 return grand_parent;
358 }
359 }
360
361 void merge(int x, int y) {
362 int root_x = find(x);
363 int root_y = find(y);
364 if(root_x != root_y) {
365 if(root[root_x] < root[root_y]) {
366 root[root_x] += root[root_y];
367 root[root_y] = root_x;
368 } else {
369 root[root_y] += root[root_x];
370 root[root_x] = root_y;
371 }
372 }
373 }
374
375 int main(istream &cin, ostream &cout) {
376 int n, m;
377 cin >> n >> m;
378 root = vector<int>(n + 1, -1);
379 char op;
380 int a, b;
381 while(m--) {
382 cin >> op >> a >> b;
383 if(op == 'M') {
384 merge(a, b);
385 } else {
386 cout << (find(a) == find(b) ? "Yes" : "No") << endl;
387 }
388 }
389 return 0;
390 }
391 }// namespace acwing836_408
392
393 namespace acwing18 {
394 TreeNode *rebuild(vector<int> &inorder, int in_begin, int in_end, vector<int> &preorder, int pre_begin, int pre_end) {
395 if(in_begin == in_end) {
396 return new TreeNode(inorder[in_begin]);
397 }
398 TreeNode *root = new TreeNode(preorder[pre_begin]);
399
400 int left_in_cursor = in_begin;
401 int left_pre_cursor = pre_begin + 1;
402
403 if(inorder[in_begin] == preorder[pre_begin]) {
404 root->left = nullptr;
405 } else {
406 unordered_set<int> left = unordered_set<int>();
407 while(inorder[left_in_cursor] != preorder[pre_begin]) {
408 left.insert(inorder[left_in_cursor]);
409 left_in_cursor++;
410 }
411 while(left.find(preorder[left_pre_cursor]) != left.end()) {
412 left_pre_cursor++;
413 }
414 root->left = rebuild(inorder, in_begin, left_in_cursor - 1, preorder, pre_begin + 1, left_pre_cursor - 1);
415 }
416
417 if(inorder[in_end] == preorder[pre_begin]) {
418 root->right = nullptr;
419 } else {
420 root->right = rebuild(inorder, left_in_cursor + 1, in_end, preorder, left_pre_cursor, pre_end);
421 }
422
423 return root;
424 }
425
426 TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
427 if(preorder.empty() || inorder.empty()) {
428 return nullptr;
429 }
430 return rebuild(inorder, 0, inorder.size() - 1, preorder, 0, preorder.size() - 1);
431 }
432 }// namespace acwing18
433
437 namespace acwing3786 {
438 TreeNode *root = nullptr;
439 const int INF = 2e9;
440
441 int main(istream &cin, ostream &cout) {
442 int n;
443 cin >> n;
444 while(n--) {
445 int t, x;
446 cin >> t >> x;
447 if(t == 1)
448 insert(root, x);
449 else if(t == 2)
450 remove(root, x);
451 else if(t == 3)
452 cout << get_pre(root, x) << endl;
453 else
454 cout << get_suc(root, x) << endl;
455 }
456 return 0;
457 }
458
459 // 插入操作
460 void insert(TreeNode *&root, int x) {
461 /*
462 1. 递归找到x的待插入的位置
463 2. 如果x < 当前root就递归到左子树,反之,递归到右子树。
464 */
465 if(!root)
466 root = new TreeNode(x);
467 // 如果发现数值相同的话就判断一下;
468 if(root->val == x)
469 return;
470 else if(x < root->val)
471 insert(root->left, x);
472 else
473 insert(root->right, x);
474 }
475
476 void remove(TreeNode *&root, int x) {
477 /*
478 1. 待删除的结点只有左子树。立即可推出,左子树上的结点都是小于待删除的结点的,我们只需要把待删除结点删了然后左子树接上待删除结点的父节点就可以了。
479 2. 待删除的结点只有右子树。立即可推出,右子树上的结点都是大于待删除的结点的,我们只需要把待删除结点删了然后右子树接上待删除结点的父节点就可以了。
480 3. 待删除的结点既有左子树又有右子树。这个比上两个情况麻烦一点,但也不麻烦,需要读者理解的是,怎么能删除结点后还不改变中序遍历的结果,并且操作代价最小,显而易见,我们根据待删除结点的左子树可以得到最右下角的最后结点满足$<x$并且最接近x的结点,把这个结点覆盖待删除的结点,然后把这个结点删除,就完成了我们的操作。
481 */
482
483 // 如果不存在直接return
484 if(!root)
485 return;
486 if(x < root->val)
487 remove(root->left, x);
488 else if(x > root->val)
489 remove(root->right, x);
490 else {
491 if(!root->left && !root->right)
492 root = NULL;
493 else if(!root->left)
494 root = root->right;
495 else if(!root->right)
496 root = root->left;
497 else {
498 auto p = root->left;
499 while(p->right)
500 p = p->right;
501 root->val = p->val;
502 remove(root->left, p->val);
503 }
504 }
505 }
506
507 // 输出数值 x 的前驱(前驱定义为现有所有数中小于 x 的最大的数)。
508 int get_pre(TreeNode *root, int x) {
509 if(!root)
510 return -INF;
511 if(root->val >= x)
512 return get_pre(root->left, x);
513 else
514 return max(root->val, get_pre(root->right, x));
515 }
516
517 // 输出数值 x 的后继(后继定义为现有所有数中大于 x 的最小的数)。
518 int get_suc(TreeNode *root, int x) {
519 if(!root)
520 return INF;
521 if(root->val <= x)
522 return get_suc(root->right, x);
523 else
524 return min(root->val, get_suc(root->left, x));
525 }
526 }// namespace acwing3786
527
528 namespace acwing149 {
529 bool Compare::operator()(huff_tree *const &a, huff_tree *const &b) {
530 if(a->val != b->val)
531 return a->val > b->val;
532 else
533 return a->height > b->height;
534 }
535
536 int main(istream &cin, ostream &cout) {
537 u_int64_t n, k, w;
538 cin >> n >> k;
539 priority_queue<huff_tree *, vector<huff_tree *>, Compare> pq;
540 for(int i = 0; i < n; i++) {
541 cin >> w;
542 pq.push(new huff_tree(w, 1, k));
543 }
544 while((n - 1) % (k - 1)) {
545 pq.push(new huff_tree(0, 1, k));
546 n++;
547 }
548 while(pq.size() > 1) {
549 huff_tree *ht = new huff_tree(0, 0, k);
550 for(int i = 0; i < k && !pq.empty(); i++) {
551 huff_tree *least = pq.top();
552 ht->height = max(ht->height, least->height + 1);
553 ht->children[i] = least;
554 ht->val += least->val;
555 pq.pop();
556 }
557 pq.push(ht);
558 }
559 huff_tree *root = pq.top();
560 queue<pair<huff_tree *, u_int64_t>> q;
561 q.push(make_pair(root, 0));
562 u_int64_t min_level = 0;
563 u_int64_t sum = 0;
564 while(!q.empty()) {
565 auto [ht, level] = q.front();
566 q.pop();
567 bool flag = true;
568 for(int i = 0; i < k; i++) {
569 if(ht->children[i] != nullptr) {
570 flag = false;
571 q.push(make_pair(ht->children[i], level + 1));
572 }
573 }
574 if(flag) {
575 sum += ht->val * level;
576 min_level = max(min_level, level);
577 }
578 }
579 cout << sum << endl
580 << min_level << endl;
581 return 0;
582 }
583 }// namespace acwing149
584
588 namespace acwing831_408 {
589 vector<int> get_next(string p) {
590 vector<int> next(p.size(), 0);
591 for(int i = 1, j = 0; i < p.size(); i++) {
592 while(j && p[i] != p[j])
593 j = next[j - 1];
594 if(p[i] == p[j])
595 j++;
596 next[i] = j;
597 }
598 return next;
599 }
600
601 int main(istream &cin, ostream &cout) {
602 int n, m;
603 string p, s;
604 cin >> n >> p >> m >> s;
605 vector<int> next = get_next(p);
606 for(int i = 0, j = 0; i < m; i++) {
607 while(j && s[i] != p[j])
608 j = next[j - 1];
609 if(s[i] == p[j])
610 j++;
611 if(j == n) {
612 cout << i - n + 1 << ' ';
613 j = next[j - 1];
614 }
615 }
616 return 0;
617 }
618 }// namespace acwing831_408
619
623 namespace acwing3385 {
624 int main(istream &cin, ostream &cout) {
625 int n;
626 string s;
627 cin >> n >> s;
628 unordered_set<string> used;
629 queue<pair<string, int>> q = queue<pair<string, int>>();
630 q.push(make_pair(s, 0));
631 used.insert(s);
632 while(!q.empty()) {
633 auto [current_s, current_step] = q.front();
634 if(current_s.find("2012") != string::npos) {
635 cout << current_step;
636 return 0;
637 }
638 q.pop();
639 for(int i = 0, j = 1; j < n; i++, j++) {
640 string next_s = current_s;
641 swap(next_s[i], next_s[j]);
642 if(used.find(next_s) == used.end()) {
643 used.insert(next_s);
644 q.push(make_pair(next_s, current_step + 1));
645 }
646 }
647 }
648 cout << -1;
649 return 0;
650 }
651 }// namespace acwing3385
652
656 namespace acwing3429 {
657 void dfs(vector<char> &stk, int p, ostream &cout, string s) {
658 if(p == s.length()) {
659 for(char c: stk) {
660 cout << c;
661 }
662 cout << endl;
663 return;
664 }
665 for(int i = 0; i < s.length(); i++) {
666 bool contain = false;
667 for(int j = 0; j < p; j++) {
668 if(stk[j] == s[i]) {
669 contain = true;
670 break;
671 }
672 }
673 if(!contain) {
674 stk[p] = s[i];
675 dfs(stk, p + 1, cout, s);
676 }
677 }
678 }
679
680 int main(istream &cin, ostream &cout) {
681 string s;
682 cin >> s;
683 vector<char> stk = vector<char>(s.length(), 0);
684 dfs(stk, 0, cout, s);
685 return 0;
686 }
687 }// namespace acwing3429
688
692 namespace acwing858_408 {
693 // Custom hash function for pair<int, int>
694 template<class T1, class T2>
695 size_t pair_hash::operator()(const pair<T1, T2> &p) const {
696 auto h1 = hash<T1>{}(p.first);
697 auto h2 = hash<T2>{}(p.second);
698 return h1 ^ h2;
699 }
700
701 // Custom equal function for pair<int, int>
702 template<class T1, class T2>
703 bool pair_equal::operator()(const pair<T1, T2> &p1, const pair<T1, T2> &p2) const {
704 return p1.first == p2.first && p1.second == p2.second;
705 }
706
707 // Custom comparator for tuple<int, int, int>
708 bool tuple_compare::operator()(const tuple<int, int, int> &t1, const tuple<int, int, int> &t2) {
709 return get<2>(t1) > get<2>(t2);
710 }
711
712 int main(istream &cin, ostream &cout) {
713 int n, m;
714 int ans = 0;
715 unordered_set<int> mst;
716 unordered_map<int, unordered_set<pair<int, int>, pair_hash, pair_equal>> g;
717 priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, tuple_compare> pq;
718 cin >> n >> m;
719 while(m--) {
720 int u, v, w;
721 cin >> u >> v >> w;
722 if(g.find(u) == g.end()) {
723 g[u] = unordered_set<pair<int, int>, pair_hash, pair_equal>();
724 }
725 g[u].insert(make_pair(v, w));
726 if(g.find(v) == g.end()) {
727 g[v] = unordered_set<pair<int, int>, pair_hash, pair_equal>();
728 }
729 g[v].insert(make_pair(u, w));
730 }
731 mst.insert(g.begin()->first);
732 for(const auto &p: g[g.begin()->first]) {
733 pq.push(make_tuple(g.begin()->first, p.first, p.second));
734 }
735 while(!pq.empty()) {
736 auto [u, v, w] = pq.top();
737 pq.pop();
738 bool u_in = mst.find(u) != mst.end();
739 bool v_in = mst.find(v) != mst.end();
740 if(u_in && v_in)
741 continue;
742 int to_insert = u_in ? v : u;
743 mst.insert(to_insert);
744 ans += w;
745 for(const auto &p: g[to_insert]) {
746 if(mst.find(p.first) != mst.end() && mst.find(to_insert) != mst.end())
747 continue;
748 pq.push(make_tuple(to_insert, p.first, p.second));
749 }
750 }
751 if(mst.size() == n) {
752 cout << ans;
753 } else {
754 cout << "impossible";
755 }
756 return 0;
757 }
758 }// namespace acwing858_408
759
763 namespace acwing849_408 {
764 int main(istream &cin, ostream &cout) {
765 int n, m;
766 cin >> n >> m;
767 unordered_map<int, unordered_map<int, int>> g = unordered_map<int, unordered_map<int, int>>();
768 unordered_set<int> visited = unordered_set<int>();
769 while(m--) {
770 int x, y, z;
771 cin >> x >> y >> z;
772 if(g[x].count(y) == 0 || g[x][y] > z) {
773 g[x][y] = z;// Only keep the smallest weight for multiple edges
774 }
775 }
776 visited.insert(1);
777 // Custom comparator for priority_queue to prioritize smaller weights
778 auto cmp = [](pair<int, int> left, pair<int, int> right) {
779 return left.second > right.second;// Compare by distance
780 };
781 priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(cmp)> pq(cmp);
782 pq.emplace(1, 0);
783 while(!pq.empty()) {
784 auto [u, d] = pq.top();
785 pq.pop();
786 if(u != 1 && (visited.find(u) != visited.end()))
787 continue;
788 visited.insert(u);
789 if(u == n) {
790 cout << d;
791 return 0;
792 }
793 for(auto [v, w]: g[u]) {
794 if(visited.find(v) != visited.end())
795 continue;
796 pq.emplace(v, d + w);
797 }
798 }
799 cout << -1;
800 return 0;
801 }
802 }// namespace acwing849_408
803
807 namespace acwing854_408 {
808 int main(istream &cin, ostream &cout) {
809 int n, m, k;
810 cin >> n >> m >> k;
811 vector<vector<int>> g = vector<vector<int>>(n + 1, vector<int>(n + 1, INT_MAX));
812 for(int i = 1; i <= n; i++) {
813 g[i][i] = 0;
814 }
815 while(m--) {
816 int x, y, z;
817 cin >> x >> y >> z;
818 g[x][y] = min(g[x][y], z);
819 }
820 for(int k = 1; k <= n; k++) {
821 for(int i = 1; i <= n; i++) {
822 for(int j = 1; j <= n; j++) {
823 if(g[i][k] != INT_MAX && g[k][j] != INT_MAX) {
824 g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
825 }
826 }
827 }
828 }
829 while(k--) {
830 int x, y;
831 cin >> x >> y;
832 if(g[x][y] == INT_MAX) {
833 cout << "impossible" << endl;
834 } else {
835 cout << g[x][y] << endl;
836 }
837 }
838 return 0;
839 }
840 }// namespace acwing854_408
841
845 namespace acwing848_408 {
846 int main(istream &cin, ostream &cout) {
847 ostringstream oss;
848 int n, m;
849 cin >> n >> m;
850 vector<unordered_set<int>> g = vector<unordered_set<int>>(n + 1, unordered_set<int>());
851 vector<short> in = vector<short>(n + 1, 0);
852 while(m--) {
853 int x, y;
854 cin >> x >> y;
855 if(g[x].find(y) != g[x].end())
856 continue;
857 g[x].insert(y);
858 in[y]++;
859 }
860 bool flag = true;
861 while(flag) {
862 for(int i = 1; i <= n; i++) {
863 if(in[i] == 0) {
864 flag = false;
865 oss << i << ' ';
866 for(int j: g[i]) {
867 in[j]--;
868 }
869 in[i] = -1;
870 }
871 }
872 if(flag) {
873 break;
874 }
875 flag = true;
876 }
877 for(int i = 1; i <= n; i++) {
878 if(in[i] != -1) {
879 cout << -1;
880 return 0;
881 }
882 }
883 cout << oss.str();
884 return 0;
885 }
886 }// namespace acwing848_408
887
891 namespace acwing3402 {
892 int main(istream &cin, ostream &cout) {
893 int n, m;
894 cin >> n >> m;
895 vector<vector<int>> a(n + 1, vector<int>(m + 1, 0));
896 unordered_set<int> filled_rows = unordered_set<int>();
897 unordered_set<int> filled_cols = unordered_set<int>();
898 vector<int> row_cnt = vector<int>(n + 1, 0);
899 vector<int> col_cnt = vector<int>(m + 1, 0);
900 for(int i = 1; i <= n; i++) {
901 for(int j = 1; j <= m; j++) {
902 cin >> a[i][j];
903 if(a[i][j] > 0) {
904 row_cnt[i]++;
905 col_cnt[j]++;
906 }
907 }
908 }
909 for(int i = 1; i <= n; i++) {
910 for(int j = 1; j <= m; j++) {
911 if(row_cnt[i] == n) {
912 filled_rows.insert(i);
913 }
914 if(col_cnt[j] == m) {
915 filled_cols.insert(j);
916 }
917 }
918 }
919 vector<vector<int>> a_cpy(a.begin(), a.end());
920 queue<pair<bool, int>> q = queue<pair<bool, int>>();// <is_row,index>
921 for(int i = 1; i < n; i++) {
922 if(row_cnt[i] > 1) {
923 filled_rows.insert(i);
924 q.push(make_pair(true, i));
925 }
926 }
927 for(int i = 1; i < m; i++) {
928 if(col_cnt[i] > 1) {
929 filled_cols.insert(i);
930 q.push(make_pair(false, i));
931 }
932 }
933 while(!q.empty()) {
934 auto [is_row, index] = q.front();
935 q.pop();
936 if(is_row) {
937 int l = 0;
938 int r = 0;
939 for(int j = 1; j <= m; j++) {
940 if(a[index][j] > 0) {
941 if(l == 0) {
942 l = j;
943 } else if(r == 0) {
944 r = j;
945 } else {
946 break;
947 }
948 }
949 }
950 int d = (a[index][r] - a[index][l]) / (r - l);
951 for(int j = 1; j <= m; j++) {
952 if(a[index][j] == 0) {
953 a[index][j] = a[index][l] + (j - l) * d;
954 col_cnt[j]++;
955 if(col_cnt[j] > 1 && filled_cols.find(j) == filled_cols.end()) {
956 filled_cols.insert(j);
957 q.emplace(false, j);
958 }
959 }
960 }
961 } else {
962 int l = 0;
963 int r = 0;
964 for(int i = 1; i <= n; i++) {
965 if(a[i][index] > 0) {
966 if(l == 0) {
967 l = i;
968 } else if(r == 0) {
969 r = i;
970 } else {
971 break;
972 }
973 }
974 }
975 int d = (a[r][index] - a[l][index]) / (r - l);
976 for(int i = 1; i <= n; i++) {
977 if(a[i][index] == 0) {
978 a[i][index] = a[l][index] + (i - l) * d;
979 row_cnt[i]++;
980 if(row_cnt[i] > 1 && filled_rows.find(i) == filled_rows.end()) {
981 filled_rows.insert(i);
982 q.emplace(true, i);
983 }
984 }
985 }
986 }
987 }
988 for(int i = 1; i <= n; i++) {
989 for(int j = 1; j <= m; j++) {
990 if(a_cpy[i][j] != a[i][j]) {
991 cout << i << ' ' << j << ' ' << a[i][j] << endl;
992 }
993 }
994 }
995 return 0;
996 }
997 }// namespace acwing3402
998
1002 namespace acwing3472 {
1003 void dfs(vector<vector<bool>> board, int current_row, vector<string> &ans, vector<int> &ans_stk) {
1004 if(current_row == 8) {
1005 ostringstream oss;
1006 for(int i = 0; i < 8; i++) {
1007 oss << ans_stk[i] + 1;
1008 }
1009 ans.push_back(oss.str());
1010 return;
1011 }
1012 for(int k = 0; k < 8; k++) {
1013 if(board[current_row][k]) {
1014 vector<vector<bool>> next_board = vector<vector<bool>>(board.begin(), board.end());
1015 for(int i = 0; i < 8; i++) {
1016 for(int j = 0; j < 8; j++) {
1017 if(i == current_row || j == k || i + j == current_row + k || i - j == current_row - k) {
1018 next_board[i][j] = false;
1019 }
1020 }
1021 }
1022 ans_stk.push_back(k);
1023 dfs(next_board, current_row + 1, ans, ans_stk);
1024 ans_stk.pop_back();
1025 }
1026 }
1027 }
1028 int main(istream &cin, ostream &cout) {
1029 int n, b;
1030 cin >> n;
1031 vector<vector<bool>> board = vector<vector<bool>>(8, vector<bool>(8, true));
1032 vector<string> ans = vector<string>();
1033 vector<int> ans_stk = vector<int>();
1034 dfs(board, 0, ans, ans_stk);
1035 while(n--) {
1036 cin >> b;
1037 cout << ans[b - 1] << endl;
1038 }
1039 return 0;
1040 }
1041 }// namespace acwing3472
1042
1046 namespace acwing3439 {
1047 int main(istream &cin, ostream &cout) {
1048 bool ready = true;
1049 while(cin) {
1050 char c;
1051 c = cin.get();
1052 if(!cin) {
1053 break;
1054 }
1055 if(ready && islower(c)) {
1056 cout << (char) toupper(c);
1057 } else {
1058 cout << c;
1059 }
1060 ready = c == ' ';
1061 }
1062 return 0;
1063 }
1064 }// namespace acwing3439
1065
1069 namespace acwing3379 {
1070 int main(istream &cin, ostream &cout) {
1071 string s;
1072 while(cin >> s) {
1073 ranges::reverse(s.begin(), s.end());
1074 cout << s << endl;
1075 }
1076 return 0;
1077 }
1078 }// namespace acwing3379
1079
1083 namespace acwing3390 {
1084 int main(istream &cin, ostream &cout) {
1085 string a, b;
1086 cin >> a >> b;
1087 vector<int> a_vec(a.length(), 0);
1088 vector<int> b_vec(b.length(), 0);
1089 for(int i = 0; i < a.length(); i++) {
1090 a_vec[i] = a[i] - '0';
1091 }
1092 for(int i = 0; i < b.length(); i++) {
1093 b_vec[i] = b[i] - '0';
1094 }
1095 int sum = 0;
1096 for(int i = 0; i < a.length(); i++) {
1097 for(int j = 0; j < b.length(); j++) {
1098 sum += a_vec[i] * b_vec[j];
1099 }
1100 }
1101 cout << sum;
1102 return 0;
1103 }
1104 }// namespace acwing3390
1105
1109 namespace acwing3397 {
1110 int main(istream &cin, ostream &cout) {
1111 int n, m;
1112 cin >> n >> m;
1113 vector<map<int, int>> a = vector<map<int, int>>(m, map<int, int>());
1114 for(int i = 0; i < n; i++) {
1115 for(int j = 0; j < m; j++) {
1116 char c;
1117 cin >> c;
1118 int x = c - '0';
1119 if(a[j].find(x) == a[j].end()) {
1120 a[j][x] = 1;
1121 } else {
1122 a[j][x]++;
1123 }
1124 }
1125 }
1126 for(int i = m - 1; i >= 0; i--) {
1127 int max_val = 0;
1128 int max_key = 0;
1129 for(auto &p: a[i]) {
1130 if(p.second > max_val) {
1131 max_val = p.second;
1132 max_key = p.first;
1133 }
1134 }
1135 cout << max_key << endl;
1136 }
1137 return 0;
1138 }
1139 }// namespace acwing3397
1140
1144 namespace acwing3426 {
1145 bool ended(vector<int> &candy) {
1146 for(int i = 1; i < candy.size(); i++) {
1147 if(candy[i] != candy[i - 1]) {
1148 return false;
1149 }
1150 }
1151 return true;
1152 }
1153
1154 int main(istream &cin, ostream &cout) {
1155 int n;
1156 while(cin >> n) {
1157 if(n == 0)
1158 continue;
1159 vector<int> candy = vector<int>(n);
1160 for(int i = 0; i < n; i++) {
1161 cin >> candy[i];
1162 }
1163 int count = 0;
1164 while(!ended(candy)) {
1165 vector<int> next_candy = vector<int>(n);
1166 for(int i = 0; i < n; i++) {
1167 int next = (i + 1) % n;
1168 next_candy[next] = candy[next] / 2 + candy[i] / 2;
1169 if(next_candy[next] % 2) {
1170 next_candy[next]++;
1171 }
1172 }
1173 candy = next_candy;
1174 count++;
1175 }
1176 cout << count << ' ' << candy[0] << endl;
1177 }
1178 return 0;
1179 }
1180 }// namespace acwing3426
1181
1185 namespace acwing3406 {
1186 int main(istream &cin, ostream &cout) {
1187 vector<task> vec = vector<task>();
1188 string name, date, time, duration_s, line;
1189 double duration;
1190 while(getline(cin, line)) {
1191 istringstream iss(line);
1192 iss >> name >> date >> time >> duration >> duration_s;
1193 vec.push_back({name, date + " " + time, duration, line});
1194 }
1195 sort(vec.begin(), vec.end(), [](task &a, task &b) {
1196 if(a.duration != b.duration) {
1197 return a.duration < b.duration;
1198 } else {
1199 return a.date_time < b.date_time;
1200 }
1201 });
1202 for(const auto &record: vec) {
1203 cout << record.raw_line << endl;
1204 }
1205 return 0;
1206 }
1207 }// namespace acwing3406
1208
1212 namespace acwing3447 {
1213 int main(istream &cin, ostream &cout) {
1214 string s;
1215 map<string, int> m;
1216 while(cin >> s) {
1217 m.clear();
1218 for(int i = 0; i < s.length(); i++) {
1219 for(int j = 1; j <= s.length() - i; j++) {
1220 string sub = s.substr(i, j);
1221 if(m.find(sub) == m.end()) {
1222 m[sub] = 1;
1223 } else {
1224 m[sub]++;
1225 }
1226 }
1227 }
1228 for(const auto &p: m) {
1229 if(p.second > 1)
1230 cout << p.first << ' ' << p.second << endl;
1231 }
1232 }
1233 return 0;
1234 }
1235 }// namespace acwing3447
1236
1240 namespace acwing3820 {
1241 int findMissMin(vector<int> &nums) {
1242 unordered_set<int> s = unordered_set<int>(nums.begin(), nums.end());
1243 int i = 1;
1244 while(s.find(i) != s.end()) {
1245 i++;
1246 }
1247 return i;
1248 }
1249 }// namespace acwing3820
1250
1254 namespace acwing840_408 {
1255 int main(istream &cin, ostream &cout) {
1256 const int N = 99991;
1257 array<list<int>, N> ht = array<list<int>, N>();
1258 int n;
1259 cin >> n;
1260 while(n--) {
1261 char op;
1262 int x;
1263 cin >> op >> x;
1264 int k = (x % N + N) % N;
1265 if(op == 'Q') {
1266 bool found = false;
1267 for(auto &i: ht[k]) {
1268 if(i == x) {
1269 found = true;
1270 break;
1271 }
1272 }
1273 cout << (found ? "Yes" : "No") << endl;
1274 } else {
1275 ht[k].push_back(x);
1276 }
1277 }
1278 return 0;
1279 }
1280 }// namespace acwing840_408
1281
1285 namespace acwing3542 {
1286 int main(istream &cin, ostream &cout) {
1287 int n, a;
1288 cin >> n;
1289 unordered_set<int> us = unordered_set<int>();
1290 while(n--) {
1291 cin >> a;
1292 us.insert(a);
1293 }
1294 cin >> n;
1295 while(n--) {
1296 cin >> a;
1297 cout << (us.find(a) != us.end() ? "YES" : "NO") << endl;
1298 }
1299 return 0;
1300 }
1301 }// namespace acwing3542
1302
1306 namespace acwing3581 {
1307 int main(istream &cin, ostream &cout) {
1308 map<string, int> dict = map<string, int>();
1309 ostringstream oss;
1310 while(!cin.eof() && !cin.fail() && cin.peek() != EOF) {
1311 char c = cin.get();
1312 if(isalpha(c)) {
1313 oss << char(tolower(c));
1314 } else {
1315 if(oss.str().empty())
1316 continue;
1317 dict[oss.str()]++;
1318 oss = ostringstream();
1319 }
1320 }
1321 if(!oss.str().empty()) {
1322 dict[oss.str()]++;
1323 }
1324 for(const auto &[word, cnt]: dict) {
1325 cout << word << ':' << cnt << endl;
1326 }
1327 return 0;
1328 }
1329 }// namespace acwing3581
1330
1334 namespace acwing785_408 {
1335 void qs(vector<int> &vec, int l, int r) {
1336 if(l >= r)
1337 return;
1338 int pivot = vec[(l + r) / 2];
1339 int lp = l - 1;
1340 int rp = r + 1;
1341 while(lp < rp) {
1342 while(vec[++lp] < pivot)
1343 ;
1344 while(vec[--rp] > pivot)
1345 ;
1346 if(lp < rp) {
1347 swap(vec[lp], vec[rp]);
1348 }
1349 }
1350 qs(vec, l, rp);
1351 qs(vec, rp + 1, r);
1352 }
1353
1354 int main(istream &cin, ostream &cout) {
1355 int n;
1356 cin >> n;
1357 vector<int> vec(n);
1358 for(int i = 0; i < n; i++) {
1359 cin >> vec[i];
1360 }
1361 qs(vec, 0, n - 1);
1362 for(int i = 0; i < n; i++) {
1363 cout << vec[i] << ' ';
1364 }
1365 return 0;
1366 }
1367 }// namespace acwing785_408
1368
1372 namespace acwing3504 {
1373 int main(istream &cin, ostream &cout) {
1374 string s;
1375 cin >> s;
1376 int num = 0;
1377 bool flag = false;
1378 for(char c: s) {
1379 if(isdigit(c)) {
1380 flag = true;
1381 num *= 10;
1382 num += c - '0';
1383 if(num < 0) {
1384 num = -1;
1385 break;
1386 }
1387 } else if(flag) {
1388 break;
1389 }
1390 }
1391 if(!flag) {
1392 num = -1;
1393 }
1394 cout << num;
1395 return 0;
1396 }
1397 }// namespace acwing3504
1398
1402 namespace acwing1603 {
1403 int main(istream &cin, ostream &cout) {
1404 int n;
1405 cin >> n;
1406 vector<int> vec(n);
1407 for(int i = 0; i < n; i++) {
1408 cin >> vec[i];
1409 }
1410 sort(vec.begin(), vec.end());
1411 if(n % 2 == 0) {
1412 int s1 = 0;
1413 int s2 = 0;
1414 for(int i = 0; i < n / 2; i++) {
1415 s1 += vec[i];
1416 }
1417 for(int i = n / 2; i < n; i++) {
1418 s2 += vec[i];
1419 }
1420 cout << "0 " << abs(s1 - s2);
1421 } else {
1422 int s1 = 0;
1423 int s2 = 0;
1424 for(int i = 0; i < n / 2; i++) {
1425 s1 += vec[i];
1426 }
1427 for(int i = n / 2 + 1; i < n; i++) {
1428 s2 += vec[i];
1429 }
1430 int res1 = abs((s1 + vec[n / 2]) - s2);
1431 int res2 = abs(s1 - (s2 + vec[n / 2]));
1432 int res = max(res1, res2);
1433 cout << "1 " << res;
1434 }
1435 return 0;
1436 }
1437 }// namespace acwing1603
1438
1442 namespace acwing3527 {
1443 int main(istream &cin, ostream &cout) {
1444 int matrix[3][2][2] = {
1445 {
1446 {0, 1},
1447 {-1, 0},
1448 },
1449 {{-1, 0},
1450 {0, -1}},
1451 {{0, -1},
1452 {1, 0}},
1453 };
1454 int n;
1455 cin >> n;
1456 vector<vector<int>> vec[5] = {vector(n, vector<int>(n, 0)), vector(n, vector<int>(n, 0)), vector(n, vector<int>(n, 0)), vector(n, vector<int>(n, 0)), vector(n, vector<int>(n, 0))};
1457 for(int i = 0; i < n; i++) {
1458 for(int j = 0; j < n; j++) {
1459 cin >> vec[0][i][j];
1460
1461 int i2 = matrix[0][0][0] * i + matrix[0][0][1] * j;
1462 int j2 = matrix[0][1][0] * i + matrix[0][1][1] * j + n - 1;
1463 vec[1][i2][j2] = vec[0][i][j];
1464
1465 int i3 = matrix[1][0][0] * i + matrix[1][0][1] * j + n - 1;
1466 int j3 = matrix[1][1][0] * i + matrix[1][1][1] * j + n - 1;
1467 vec[2][i3][j3] = vec[0][i][j];
1468
1469 int i4 = matrix[2][0][0] * i + matrix[2][0][1] * j + n - 1;
1470 int j4 = matrix[2][1][0] * i + matrix[2][1][1] * j;
1471 vec[3][i4][j4] = vec[0][i][j];
1472 }
1473 }
1474 for(int i = 0; i < n; i++) {
1475 for(int j = 0; j < n; j++) {
1476 cin >> vec[4][i][j];
1477 }
1478 }
1479 bool flag = true;
1480 for(int k = 0; k < 4; k++) {
1481 flag = true;
1482 for(int i = 1; i < n; i++) {
1483 for(int j = 0; j < n; j++) {
1484 if(vec[k][i][j] != vec[4][i][j]) {
1485 flag = false;
1486 break;
1487 }
1488 }
1489 if(!flag) {
1490 break;
1491 }
1492 }
1493 if(flag) {
1494 cout << k * 90;
1495 break;
1496 }
1497 }
1498 if(!flag) {
1499 cout << -1;
1500 }
1501 return 0;
1502 }
1503 }// namespace acwing3527
1504
1508 namespace acwing3534 {
1509 int main(istream &cin, ostream &cout) {
1510 int n, p;
1511 cin >> n >> p;
1512 vector<Matrix *> mats = vector<Matrix *>(p + 1, nullptr);
1513 mats[1] = new Matrix(n);
1514 for(int i = 0; i < n; i++) {
1515 for(int j = 0; j < n; j++) {
1516 cin >> (*mats[1])[i][j];
1517 }
1518 }
1519 Matrix mat = getMat(mats, p);
1520 for(int i = 0; i < n; i++) {
1521 for(int j = 0; j < n; j++) {
1522 cout << mat[i][j] << ' ';
1523 }
1524 cout << endl;
1525 }
1526 return 0;
1527 }
1528
1529 Matrix getMat(vector<Matrix *> &mat, int p) {
1530 if(mat[p] != nullptr) {
1531 return *mat[p];
1532 }
1533 Matrix res = Matrix::identity((*mat[1])[1].size());
1534 for(int i = 0; p >> i != 0; i++) {
1535 int masked = p & (1 << i);
1536 if(masked != 0) {
1537 if(masked != p) {
1538 res = res * getMat(mat, p & (1 << i));
1539 } else {
1540 Matrix m2 = getMat(mat, masked >> 1);
1541 res = res * m2 * m2;
1542 }
1543 }
1544 }
1545 mat[p] = new Matrix(res);
1546 return res;
1547 }
1548 }// namespace acwing3534
1549
1553 namespace acwing3535 {
1554 int main(istream &cin, ostream &cout) {
1555 int dir, len, x, y;
1556
1557 vector<vector<int>> mat = vector<vector<int>>(6, vector<int>(6, 0));
1558 for(int i = 1; i <= 5; i++) {
1559 for(int j = 1; j <= 5; j++) {
1560 cin >> mat[i][j];
1561 }
1562 }
1563 vector<vector<int>> ret = mat;
1564 cin >> dir >> len >> x >> y;
1565 int transform[2][2][2] = {
1566 {{0, 1},
1567 {-1, 0}},
1568 {{0, -1},
1569 {1, 0}},
1570 };
1571 dir--;
1572 for(int i = x; i < x + len; i++) {
1573 for(int j = y; j < y + len; j++) {
1574 int i0 = i - x + 1;
1575 int j0 = j - y + 1;
1576 int i2 = transform[dir][0][0] * i0 + transform[dir][0][1] * j0;
1577 int j2 = transform[dir][1][0] * i0 + transform[dir][1][1] * j0;
1578 if(dir == 0) {
1579 j2 += len + 1;
1580 } else {
1581 i2 += len + 1;
1582 }
1583 i2 += x - 1;
1584 j2 += y - 1;
1585 ret[i2][j2] = mat[i][j];
1586 }
1587 }
1588 for(int i = 1; i <= 5; i++) {
1589 for(int j = 1; j <= 5; j++) {
1590 cout << ret[i][j] << ' ';
1591 }
1592 cout << endl;
1593 }
1594 return 0;
1595 }
1596 }// namespace acwing3535
1597
1601 namespace acwing3874 {
1602 struct point {
1603 int64_t value;
1604 int64_t group;
1605 int64_t prev_index[4];
1606 };
1607 int main(istream &cin, ostream &cout) {
1608 int64_t l, m, n;
1609 cin >> l >> m >> n;
1610 vector<int64_t> s1(l), s2(m), s3(n);
1611 for(int i = 0; i < l; i++) {
1612 cin >> s1[i];
1613 }
1614 for(int i = 0; i < m; i++) {
1615 cin >> s2[i];
1616 }
1617 for(int i = 0; i < n; i++) {
1618 cin >> s3[i];
1619 }
1620 vector<point> s(l + m + n);
1621 int64_t p1 = 0, p2 = 0, p3 = 0, ps = 0, prev1 = -1, prev2 = -1, prev3 = -1;
1622 while(p1 < l || p2 < m || p3 < n || ps < l + m + n) {
1623 int64_t v1 = p1 < l ? s1[p1] : LONG_LONG_MAX;
1624 int64_t v2 = p2 < m ? s2[p2] : LONG_LONG_MAX;
1625 int64_t v3 = p3 < n ? s3[p3] : LONG_LONG_MAX;
1626 int64_t min_v = min(v1, min(v2, v3));
1627 if(min_v == v1) {
1628 s[ps++] = {v1, 1, {-1, prev1, prev2, prev3}};
1629 prev1 = ps - 1;
1630 p1++;
1631 } else if(min_v == v2) {
1632 s[ps++] = {v2, 2, {-1, prev1, prev2, prev3}};
1633 prev2 = ps - 1;
1634 p2++;
1635 } else {
1636 s[ps++] = {v3, 3, {-1, prev1, prev2, prev3}};
1637 prev3 = ps - 1;
1638 p3++;
1639 }
1640 }
1641 int64_t last1 = l + m + n, last2 = l + m + n, last3 = l + m + n;
1642 int64_t ans = LONG_LONG_MAX;
1643 for(int64_t i = l + m + n - 1; i >= 0; i--) {
1644 if(s[i].group == 1) {
1645 if(last2 != l + m + n && s[i].prev_index[3] != -1) {
1646 ans = min(ans, 2 * (s[last2].value - s[s[i].prev_index[3]].value));
1647 }
1648 if(last3 != l + m + n && s[i].prev_index[2] != -1) {
1649 ans = min(ans, 2 * (s[last3].value - s[s[i].prev_index[2]].value));
1650 }
1651 last1 = i;
1652 } else if(s[i].group == 2) {
1653 if(last1 != l + m + n && s[i].prev_index[3] != -1) {
1654 ans = min(ans, 2 * (s[last1].value - s[s[i].prev_index[3]].value));
1655 }
1656 if(last3 != l + m + n && s[i].prev_index[1] != -1) {
1657 ans = min(ans, 2 * (s[last3].value - s[s[i].prev_index[1]].value));
1658 }
1659 last2 = i;
1660 } else {
1661 if(last1 != l + m + n && s[i].prev_index[2] != -1) {
1662 ans = min(ans, 2 * (s[last1].value - s[s[i].prev_index[2]].value));
1663 }
1664 if(last2 != l + m + n && s[i].prev_index[1] != -1) {
1665 ans = min(ans, 2 * (s[last2].value - s[s[i].prev_index[1]].value));
1666 }
1667 last3 = i;
1668 }
1669 }
1670 cout << ans;
1671 return 0;
1672 }
1673 }// namespace acwing3874
1674
1678 namespace acwing52 {
1679 int moreThanHalfNum_Solution(vector<int> &nums) {
1680 int cnt = 0;
1681 int ans = 0;
1682 for(auto num: nums) {
1683 if(cnt == 0) {
1684 ans = num;
1685 cnt++;
1686 continue;
1687 }
1688 if(num == ans) {
1689 cnt++;
1690 } else {
1691 cnt--;
1692 }
1693 }
1694 return ans;
1695 }
1696 }// namespace acwing52
1697
1701 namespace acwing3392 {
1702 int main(istream &cin, ostream &cout) {
1703 const int limit = 10000;
1704 int a[3] = {};
1705 int p, q, k;
1706 cin >> a[0] >> a[1] >> p >> q >> k;
1707 for(int i = 2; i <= k; i++) {
1708 a[i % 3] = ((p * a[(i - 1 + 3) % 3]) % limit + (q * a[(i - 2 + 3) % 3]) % limit) % limit;
1709 }
1710 cout << a[k % 3] % limit;
1711 return 0;
1712 }
1713 }// namespace acwing3392
1714
1718 namespace acwing3433 {
1719 int main(istream &cin, ostream &cout) {
1720 int n;
1721 cin >> n;
1722 vector<unordered_set<string>> vec(n + 1, unordered_set<string>());
1723 vec[1].insert("1");
1724 vec[2].insert("2");
1725 vec[2].insert("11");
1726 for(int i = 3; i <= n; i++) {
1727 for(int j = 1; j < n; j++) {
1728 auto set1 = vec[j];
1729 auto set2 = vec[i - j];
1730 for(const auto &s1: set1) {
1731 for(const auto &s2: set2) {
1732 vec[i].insert(s1 + s2);
1733 }
1734 }
1735 }
1736 }
1737 cout << vec[n].size();
1738 return 0;
1739 }
1740 }// namespace acwing3433
1741
1745 namespace acwing3441 {
1746 void draw(const vector<vector<char>> &g, int n, int level, vector<vector<char>> &canvas, int x, int y, int space) {
1747 if(level > 1) {
1748 space /= n;
1749 for(int i = 0; i < n; i++) {
1750 for(int j = 0; j < n; j++) {
1751 if(!isspace(g[i][j])) {
1752 draw(g, n, level - 1, canvas, x + i * space, y + j * space, space);
1753 }
1754 }
1755 }
1756 return;
1757 }
1758
1759 for(int i = 0; i < n; i++) {
1760 for(int j = 0; j < n; j++) {
1761 canvas[x + i][y + j] = g[i][j];
1762 }
1763 }
1764 }
1765
1766 int main(istream &cin, ostream &cout) {
1767 int n;
1768 while(cin >> n) {
1769 int q;
1770 if(n == 0) {
1771 break;
1772 }
1773 vector<vector<char>> graph = vector<vector<char>>(n, vector<char>(n, ' '));
1774 for(int i = 0; i < n; i++) {
1775 for(int j = 0; j < n; j++) {
1776 char ch = cin.get();
1777 if(ch != '\n') {
1778 graph[i][j] = ch;
1779 } else {
1780 j--;
1781 }
1782 }
1783 }
1784 cin >> q;
1785 int canvas_size = 1;
1786 for(int i = 0; i < q; i++) {
1787 canvas_size *= n;
1788 }
1789 vector<vector<char>> canvas = vector<vector<char>>(canvas_size, vector<char>(canvas_size, ' '));
1790 draw(graph, n, q, canvas, 0, 0, canvas_size);
1791 for(int i = 0; i < canvas_size; i++) {
1792 for(int j = 0; j < canvas_size; j++) {
1793 cout << canvas[i][j];
1794 }
1795 cout << endl;
1796 }
1797 }
1798 return 0;
1799 }
1800 }// namespace acwing3441
1801
1805 namespace acwing2 {
1806 int main(istream &cin, ostream &cout) {
1807 int N, V;
1808 cin >> N >> V;
1809 vector<int> v = vector<int>(N);
1810 vector<int> w = vector<int>(N);
1811 vector<status> dp = vector<status>(V + 1, status{
1812 .w = 0,
1813 .free = vector<bool>(N, true)});
1814 for(int i = 0; i < N; i++) {
1815 cin >> v[i] >> w[i];
1816 }
1817 for(int i = 0; i <= V; i++) {
1818 for(int j = 0; j < N; j++) {
1819 if(dp[i].free[j]) {
1820 int next_w = dp[i].w + w[j];
1821 int next_v = i + v[j];
1822 if(next_v <= V && next_w > dp[next_v].w) {
1823 dp[next_v].w = next_w;
1824 dp[next_v].free = vector<bool>(dp[i].free);
1825 dp[next_v].free[j] = false;
1826 }
1827 }
1828 }
1829 }
1830 cout << dp[V].w;
1831 return 0;
1832 }
1833 }// namespace acwing2
1834
1838 namespace acwing3445 {
1839 int main(istream &cin, ostream &cout) {
1840 int c, n;
1841 cin >> c >> n;
1842 vector<int> p = vector<int>(n);
1843 vector<int> v = vector<int>(n);
1844 for(int i = 0; i < n; i++) {
1845 cin >> p[i] >> v[i];
1846 }
1847 vector<status> dp = vector<status>(c + 1, status{
1848 .v = 0,
1849 .free = vector<bool>(n, true)});
1850 int max_v = 0;
1851 for(int i = 0; i <= c; i++) {
1852 for(int j = 0; j < n; j++) {
1853 max_v = max(max_v, dp[i].v);
1854 if(dp[i].free[j]) {
1855 int next_v = dp[i].v + v[j];
1856 int next_c = i + p[j];
1857 if(next_c <= c && next_v > dp[next_c].v) {
1858 dp[next_c].v = next_v;
1859 dp[next_c].free = vector<bool>(dp[i].free);
1860 dp[next_c].free[j] = false;
1861 }
1862 }
1863 }
1864 }
1865 cout << max(max_v, dp[c].v);
1866 return 0;
1867 }
1868 }// namespace acwing3445
1869
1873 namespace acwing3442 {
1874 int main(istream &cin, ostream &cout) {
1875 int n;
1876 cin >> n;
1877 vector<int> a(n);
1878 for(int i = 0; i < n; i++) {
1879 cin >> a[i];
1880 }
1881 vector<vector<int>> dp(n + 1, vector<int>(41, 0));
1882 dp[0][0] = 1;
1883 for(int i = 1; i <= n; i++) {
1884 for(int j = 0; j <= 40; j++) {
1885 dp[i][j] = dp[i - 1][j];
1886 if(j >= a[i - 1]) {
1887 dp[i][j] += dp[i - 1][j - a[i - 1]];
1888 }
1889 }
1890 }
1891 cout << dp[n][40];
1892 return 0;
1893 }
1894 }// namespace acwing3442
1895
1899 namespace acwing3382 {
1900 int main(istream &cin, ostream &cout) {
1901 unsigned n;
1902 cin >> n;
1903 vector<unsigned> dp(n + 1);
1904 dp[0] = 1;
1905 for(int i = 1; i <= n; i *= 2) {
1906 for(int j = i; j <= n; j++) {
1907 dp[j] = (dp[j] + dp[j - i]) % 1000000000;
1908 }
1909 }
1910 cout << dp[n];
1911 return 0;
1912 }
1913 }// namespace acwing3382
1914
1918 namespace acwing3389 {
1919 int main(istream &cin, ostream &cout) {
1920 unsigned n;
1921 unordered_map<unsigned, BigInt> cache = unordered_map<unsigned, BigInt>();
1922 while(cin >> n) {
1923 BigInt res = 1;
1924 for(unsigned i = 1; i <= n; i++) {
1925 if(cache.find(i) != cache.end()) {
1926 res = cache[i];
1927 continue;
1928 }
1929 res *= i;
1930 cache[i] = res;
1931 }
1932 cout << res << endl;
1933 }
1934 return 0;
1935 }
1936 }// namespace acwing3389
1937
1941 namespace acwing3448 {
1942 int main(istream &cin, ostream &cout) {
1943 unsigned long a, b;
1944 cin >> a >> b;
1945 for(; a != 0 || b != 0; cin >> a >> b) {
1946 vector<unsigned short> va = vector<unsigned short>();
1947 vector<unsigned short> vb = vector<unsigned short>();
1948 while(a != 0) {
1949 va.push_back(a % 10);
1950 a /= 10;
1951 }
1952 while(b != 0) {
1953 vb.push_back(b % 10);
1954 b /= 10;
1955 }
1956 vector<unsigned short> res;
1957 unsigned short cnt = 0;
1958 unsigned short carry = 0;
1959 for(size_t i = 0; i < max(va.size(), vb.size()); i++) {
1960 unsigned short sum = (i < va.size() ? va[i] : 0) + (i < vb.size() ? vb[i] : 0) + carry;
1961 cnt += sum / 10;
1962 carry = sum / 10;
1963 }
1964 ostringstream ss = ostringstream();
1965 ss << cnt;
1966 cout << ((cnt != 0) ? ss.str() : "No") << " carry operation" << ((cnt <= 1) ? "" : "s") << '.' << endl;
1967 }
1968 return 0;
1969 }
1970 }// namespace acwing3448
1971
1975 namespace acwing3453 {
1976 int main(istream &cin, ostream &cout) {
1977 BigInt sum = 0;
1978 BigInt input;
1979 string str;
1980 while(cin >> str) {
1981 input = BigInt(str);
1982 sum += input;
1983 }
1984 cout << sum;
1985 return 0;
1986 }
1987 }// namespace acwing3453
1988
1992 namespace acwing3380 {
1993 bool add(unsigned long long n) {
1994 if(n < 2)
1995 return false;
1996 for(unsigned long long i = 2; i * i <= n; i++)
1997 if(n % i == 0)
1998 return false;
1999 return true;
2000 }
2001
2002 int main(istream &cin, ostream &cout) {
2003 unsigned long long n;
2004 while(cin >> n) {
2005 unsigned long long k = 0;
2006 for(unsigned long long i = 2; i * i <= n; i++) {
2007 if(add(i)) {
2008 while(n % i == 0)
2009 n /= i, k++;
2010 }
2011 }
2012 if(add(n))
2013 k++;
2014 cout << k << endl;
2015 }
2016 return 0;
2017 }
2018 }// namespace acwing3380
2019
2023 namespace acwing3377 {
2024 int main(istream &cin, ostream &cout) {
2025 unsigned long n;
2026 cin >> n;
2027 unsigned long a;
2028 while(n--) {
2029 cin >> a;
2030 unsigned long cnt = 2;
2031 for(unsigned long i = 2; i <= sqrt(a); i++) {
2032 if(a % i == 0) {
2033 cnt += 2;
2034 if(a / i == i) {
2035 cnt--;
2036 }
2037 }
2038 }
2039 if(a == 1) {
2040 cnt = 1;
2041 }
2042 cout << cnt << endl;
2043 }
2044 return 0;
2045 }
2046 }// namespace acwing3377
2047
2051 namespace acwing3507 {
2052 int main(istream &cin, ostream &cout) {
2053 unsigned long n;
2054 cin >> n;
2055 unsigned long cnt2 = 0;
2056 unsigned long cnt5 = 0;
2057 for(unsigned long i = 2; i <= n; i *= 2) {
2058 cnt2 += n / i;
2059 }
2060 for(unsigned long i = 5; i <= n; i *= 5) {
2061 cnt5 += n / i;
2062 }
2063 cout << min(cnt2, cnt5);
2064 return 0;
2065 }
2066 }// namespace acwing3507
2067
2071 namespace acwing3484 {
2072 int main(istream &cin, ostream &cout) {
2073 int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
2074 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,
2075 101, 103, 107, 109, 113, 127, 131, 137, 139, 149,
2076 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199,
2077 211, 223, 227, 229, 233, 239, 241,
2078 251, 257, 263, 269, 271, 277, 281, 283, 293,
2079 307, 311, 313, 317, 331, 337, 347, 349,
2080 353, 359, 367, 373, 379, 383, 389, 397,
2081 401, 409, 419, 421, 431, 433, 439, 443, 449,
2082 457, 461, 463, 467, 479, 487, 491, 499,
2083 503, 509, 521, 523, 541, 547,
2084 557, 563, 569, 571, 577, 587, 593, 599,
2085 601, 607, 613, 617, 619, 631, 641, 643, 647,
2086 653, 659, 661, 673, 677, 683, 691,
2087 701, 709, 719, 727, 733, 739, 743,
2088 751, 757, 761, 769, 773, 787, 797,
2089 809, 811, 821, 823, 827, 829, 839,
2090 853, 857, 859, 863, 877, 881, 883, 887,
2091 907, 911, 919, 929, 937, 941, 947,
2092 953, 967, 971, 977, 983, 991, 997};
2093 int n, a;
2094 cin >> n >> a;
2095 map<int, int> prime_combination_a = map<int, int>();
2096 for(auto prime: primes) {
2097 while(a % prime == 0) {
2098 prime_combination_a[prime]++;
2099 a /= prime;
2100 }
2101 }
2102 map<int, int> prime_combination_n = map<int, int>();
2103 for(int i = 1; i <= n; i++) {
2104 for(const auto &[k, v]: prime_combination_a) {
2105 int j = i;
2106 while(j % k == 0) {
2107 prime_combination_n[k]++;
2108 j /= k;
2109 }
2110 }
2111 }
2112 int ans = INT_MAX;
2113 for(const auto &[k, v]: prime_combination_a) {
2114 ans = min(ans, prime_combination_n[k] / v);
2115 }
2116 if(ans == INT_MAX) {
2117 ans = 0;
2118 }
2119 cout << ans;
2120 return 0;
2121 }
2122 }// namespace acwing3484
2123}// namespace acwing
int main(int argc, char **argv)
Definition: main.cpp:5
const int N
Definition: acwing.h:146
bool cmp(const pair< int, int > &a, const pair< int, int > &b)
Definition: acwing.cpp:6470
struct acwing::acwing3378::student student
int main(istream &cin, ostream &cout)
Definition: acwing408.cpp:24
void reverse(struct ListNode *head)
void rearrangedList(struct ListNode *head)
Definition: acwing408.cpp:125
int pathSum(TreeNode *root, int level)
Definition: acwing408.cpp:311
void merge(int x, int y)
Definition: acwing408.cpp:361
TreeNode * rebuild(vector< int > &inorder, int in_begin, int in_end, vector< int > &preorder, int pre_begin, int pre_end)
Definition: acwing408.cpp:394
TreeNode * buildTree(vector< int > &preorder, vector< int > &inorder)
Definition: acwing408.cpp:426
void remove(TreeNode *&root, int x)
Definition: acwing408.cpp:476
int get_suc(TreeNode *root, int x)
Definition: acwing408.cpp:518
int get_pre(TreeNode *root, int x)
Definition: acwing408.cpp:508
void insert(TreeNode *&root, int x)
Definition: acwing408.cpp:460
vector< int > get_next(string p)
Definition: acwing408.cpp:589
void dfs(vector< vector< bool > > board, int current_row, vector< string > &ans, vector< int > &ans_stk)
Definition: acwing408.cpp:1003
bool ended(vector< int > &candy)
Definition: acwing408.cpp:1145
int findMissMin(vector< int > &nums)
Definition: acwing408.cpp:1241
void qs(vector< int > &vec, int l, int r)
Definition: acwing408.cpp:1335
Matrix getMat(vector< Matrix * > &mat, int p)
Definition: acwing408.cpp:1529
int moreThanHalfNum_Solution(vector< int > &nums)
Definition: acwing408.cpp:1679
void draw(const vector< vector< char > > &g, int n, int level, vector< vector< char > > &canvas, int x, int y, int space)
Definition: acwing408.cpp:1746
bool add(unsigned long long n)
Definition: acwing408.cpp:1993
int vec[100010]
Definition: pat.cpp:5095
TreeNode * right
Definition: acwing408.h:24
TreeNode * left
Definition: acwing408.h:23
struct ListNode * next
Definition: acwing408.h:52
vector< huff_tree * > children
Definition: acwing408.h:109
高精度整数
Definition: templates.h:23
矩阵
Definition: templates.h:107
static Matrix identity(int n)
Definition: templates.cpp:557