constint N = 10000010; // test data amount range int q[N]; // original array int tmp[N]; // temporary array voidmerge_sort(int q[], int lo, int hi) { if (lo >= hi) return; // end of recursion int mid = (lo + hi) >> 1; merge_sort(q, lo, mid), merge_sort(q, mid + 1, hi); // recursively divides into two // backtrack progress int i = lo, j = mid + 1; int t = 0; // index of temporary array for (;i <= mid && j <= hi;) // puts smaller one into tmp[] if (q[i] <= q[j]) tmp[t++] = q[i++]; else tmp[t++] = q[j++]; for (;i <= mid;) tmp[t++] = q[i++]; // puts remaining array into tmp[] for (;j <= hi;) tmp[t++] = q[j++]; for (int i = lo, j = 0; i <= hi;) q[i++] = tmp[j++]; // overrides original array }
// finds edge A boolis_satisfied_A(int x){/*---*/}; boolis_satisfied_B(int x){/*---*/};
intbinary_search(int lo, int hi) { for (;lo < hi;) { int mid = (lo + hi) >> 1; // 1 if (is_satisfied_B(mid)) hi = mid; else lo = mid + 1; } return lo; }
查找B边界
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// finds edge B boolis_satisfied_A(int x){/*---*/}; boolis_satisfied_B(int x){/*---*/};
intbinary_search(int lo, int hi) { for (;lo < hi;) { int mid = (lo + hi + 1) >> 1; // 2 if (is_satisfied_A(mid)) lo = mid; else hi = mid - 1; } return lo; }
// C = A + B, A >= 0, B >= 0 #include<iostream> #include<vector>
usingnamespacestd;
vector<int> add(vector<int> &A, vector<int> &B)//1 { vector<int> C; int t = 0; for (int i = 0; i < A.size() || i < B.size(); i++) //2 { if (i <= A.size()) t += A[i]; if (i <= B.size()) t += B[i]; C.push_back(t % 10); t /= 10; } if (t) C.push_back(1); //3 return C; }
intmain() { string a, b; cin >> a >> b; vector<int> A, B; for (int i = a.size() - 1; i >= 0; i-- ) A.push_back(a[i] - '0'); for (int i = b.size() - 1; i >= 0; i -- ) B.push_back(b[i] - '0'); auto C = add(A, B); for (int i = C.size() - 1; i >= 0; i --) cout << C[i]; }
注释:
以数组引用传入参数,可以避免没必要的数组拷贝。
A或B至少有一个没有遍历完,都需要继续计算。
注意结束之后进位有可能非0.
该模板可以改写成总是模拟以一个更长的数组加长度较小的数组: 模板2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// C = A + B, A >= 0, B >= 0 vector<int> add(vector<int> &A, vector<int> &B) { if (A.size() < B.size()) return add(B, A);
vector<int> C; int t = 0; for (int i = 0; i < A.size(); i ++ ) { t += A[i]; if (i < B.size()) t += B[i]; C.push_back(t % 10); t /= 10; }
//C = A - B, A >= 0, B >= 0 #include<iostream> #include<vector>
usingnamespacestd;
// return if A >= B boolcmp(vector<int> &A, vector<int> &B) { if (A.size() != B.size()) return A.size() > B.size(); for (int i = A.size() - 1; i >= 0; i -- ) { if (A[i] != B[i]) return A[i] > B[i]; } returntrue; }
// A - B while A >= B vector<int> sub(vector<int> &A, vector<int> &B) { vector<int> C; for (int i = 0, t = 0; i < A.size(); i ++ ) { t = A[i] - t; // consider if should sub slot of B if (i < B.size()) t -= B[i]; // t might be a negative // t will be the right slot whatever its negative or positive // t + 10 means it will borrow 10 from next slot C.push_back((t + 10) % 10); //1 // update t if (t < 0) t = 1; else t = 0; } // there might be some '0' in back of C // dont't have to remove while C.size() == 1 for (; C.size() > 1 && C.back() == 0; ) C.pop_back(); //2 return C; }
intmain() { string a, b; cin >> a >> b; vector<int> A, B, C; for (int i = a.size() - 1; i >= 0; i -- ) A.push_back(a[i] - '0'); for (int i = b.size() - 1; i >= 0; i -- ) B.push_back(b[i] - '0'); if (cmp(A, B)) C = sub(A, B); else { C = sub(B, A); printf("-"); } for (int i = C.size() - 1; i >= 0; i -- ) printf("%d", C[i]); return0; }
// C = A * b, A >= 0, b > 0 vector<int> mul(vector<int> &A, int b) { vector<int> C;
int t = 0; for (int i = 0; i < A.size() || t; i ++ ) //1 { if (i < A.size()) t += A[i] * b; C.push_back(t % 10); //2 t /= 10; } while (C.size() > 1 && C.back() == 0) C.pop_back(); return C; }
vector<int> mul(vector<int> &A, int b) { vector<int> C; int t = 0; for (int i = 0; i < A.size(); i ++ ) { t = A[i] * b + t; C.push_back(t % 10); t /= 10; } if (t) C.push_back(t); for (;C.size() > && C.back() == 0;) C.pop_back(); return C; }
// A / b = C ... r, A >= 0, b > 0 vector<int> div(vector<int> &A, int b, int &r) { vector<int> C; r = 0; // 1 for (int i = A.size() - 1; i >= 0; i -- ) { r = r * 10 + A[i]; // 2 C.push_back(r / b); // 3 r %= b; // 4 } reverse(C.begin(), C.end()); // 5 while (C.size() > 1 && C.back() == 0) C.pop_back(); return C; }
// 二分求出x对应的离散化的值 intfind(int x)// 找到第一个大于等于x的位置 { int l = 0, r = alls.size() - 1; while (l < r) { int mid = l + r >> 1; if (alls[mid] >= x) r = mid; else l = mid + 1; } return r + 1; // 映射到1, 2, ...n }
int st = -2e9, ed = -2e9; // 2 for (auto seg : segs) if (ed < seg.first) // 3 { if (st != -2e9) res.push_back({st, ed}); // 4 st = seg.first, ed = seg.second; // 5 } else ed = max(ed, seg.second); // 6