tailieuktlt

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

#include<iostream> #include<iostream> #include<iostream>

#include<vector> #include<vector> #include<vector>


using namespace std; #include<string> #include<string>
int n,ok; #include<algorithm> #include<algorithm>
//xâu nhị phân độ dài n using namespace std; using namespace std;
void sinh(vector<int>&a) { //sinh tổ hợp chập k của n //sinh hoán vị
int main() {
int i = n; int main() {
int n, k;cin >> n >> k;
while (i >= 1 && a[i] == 1) { vector<int> a(n); int n, k;
a[i] = 0; for (int i = 0; i < n; i++) cin >> a[i]; cin >> n >> k;
i--; string order; vector<int> a(n);
} for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) cin >>
if (i == 0) { if (i < k) { a[i];
ok = 0; order.push_back('1'); do {
} } for (int i = 0; i < n; i+
else { else +) {
a[i] = 1; order.push_back('0'); cout <<
} } a[i];
} do { }cout << '\n';
//neu them dk khac chi them ham check neu du for (int i = 0; i < n; i++) } while
{
dk thi in ra (next_permutation(a.begin(), a.end()));
if
int main() { //next_permutation với từ nhỏ tới
(order[i] == '1') {
cin >> n; lớn
ok = 1; cout << a[i]; //prev_permutation với từ lớn tới
vector<int>a(n+1, 0); } nhỏ
while (ok) { }cout << '\n'; return 0;
for (int i = 1; i <= n; } while }
i++) { (prev_permutation(order.begin(), order.end()));
cout << return 0;
a[i]; }
}cout << '\n';
sinh(a);
}
}

SUM SUB KẾT HỢP MỚI HÀM SUM ĐỂ THÀNH MUL


bool isSmaller(string& a, string& b) {
string ADD(string a, string b) { if (a.size() < b.size()) return true; string MULINT(string a, int b) {
if (b.size() < a.size()) return false; int len = a.size();
int len = max((int)a.size(), return a < b; string res = "";
(int)b.size()); } int carry = 0;
while (a.size() < len) a = "0" + string SUB(string a, string b) { for (int i = len - 1; i >= 0; i--) {
a; bool sign = false; int temp = (a[i] - '0') * b + carry;
while (b.size() < len) b = "0" if (isSmaller(a, b)) { int dv = temp % 10;
+ b; swap(a, b); carry = temp / 10;
string res = ""; sign = true; res.push_back(dv + '0');
} }
int carry = 0; int len = max((int)a.size(), (int)b.size()); if (carry != 0) res.push_back(carry + '0');
for (int i = len - 1; i >= 0; i--) while (a.size() < len) a = "0" + a; reverse(res.begin(), res.end());
{ while (b.size() < len) b = "0" + b; return res;
int temp = (a[i] - int carry = 0; }
'0') + (b[i] - '0') + carry; string res = ""; string MULSTR(string a, string b) {
int dv = temp % for (int i = len - 1; i >= 0; i--) { int len = b.size();
10; carry = temp / 10; int temp = (a[i] - '0') - (b[i] - '0' + carry); string res = "0";
if (temp < 0) { string bonus = "";
res.push_back(dv temp += 10; for (int i = len - 1; i >= 0; i--) {
+ '0'); carry = 1; string temp = MULINT(a, b[i] - '0') +
} if (carry != 0) } bonus;
res.push_back(carry + '0'); else carry = 0; res = ADD(res, temp);
reverse(res.begin(), res.end()); res.push_back(temp + '0'); bonus += "0";
return res; } }
} while (res.back() == '0' && res.size() > 1) while (res[0] == '0' && res.size() > 1)
res.pop_back(); res.erase(res.begin());
if (sign) res.push_back('-'); return res;
reverse(res.begin(), res.end()); }
return res;
}
#include<iostream> #include<iostream> #include<set>
#include<vector> Vé xem hòa nhạc
#include<set> void Solution(multiset<long long >a, vector<long
#include<algorithm> Sang sông long>b, int m) {
#include<set> for (int i = 0; i < m; i++) {
Căn hộ
using namespace std;
auto it = a.lower_bound(b[i]);
using namespace std; int Solution(multiset<int> a, int n, int x) { if (it == a.end()) {
int solution(vector<int>& a, multiset<int>& b, int res = 0; auto previt = prev(it);
int n, int n1, int k) { while (a.size() > 1) { cout << *previt << endl;
a.erase(previt);
sort(a.begin(), a.end()); res++; continue;
int res = 0; int t = *a.rbegin(); }
for (int i = 0; i < n; i++) { a.erase(a.find(t)); if (it != a.end() && *it == b[i]) {
auto it = b.lower_bound(a[i] - k); cout << *it << endl;
if (it == b.end() || *it > a[i] + k) {
auto it = a.upper_bound(x - t); a.erase(it);
continue; if (it == a.begin()) { continue;
continue; }
}
if (it == a.begin() && *it != b[i]) {
else { } cout << -1 << endl;
b.erase(it); it--; continue;
res++; a.erase(it); }
} }
} if (it != a.end() && *it != b[i]) {
return res;
return res + a.size(); auto previt = prev(it);
} cout << *previt << endl;
} a.erase(previt);
int main() { int main() { continue;
int n, n1, k; int n, x; }
cin >> n >> n1 >> k; cin >> n >> x; cout << -1 << endl;
vector<int> a(n); }
multiset<int> a; }
multiset<int> b; for (int i = 0; i < n; i++) { int main() {
for (int i = 0; i < n; i++) { int n, m;
cin >> a[i];
int t;
cin >> n >> m;
} cin >> t; multiset<long long> a;
for (int i = 0; i < n1; i++) { a.insert(t); vector<long long> b(m);
int x; cin >> x; } for (int i = 0; i < n; i++) {
int x;
b.insert(x); cout << Solution(a, n, x); cin >> x;
} return 0; a.insert(x);
cout << solution(a, b, n, n1, k); } }
return 0; for (int i = 0; i < m; i++)cin >>b[i];
} Solution(a, b, m);
}
#include<iostream> #include<iostream> int main() { DP ĐỔI TIỀN
#include<cmath> #include<algorithm> ios_base::sync_with_stdio(false);
#include<climits> Backtracking cân tạ
cin.tie(NULL);
Backtracking chia táo using namespace std;
using namespace std; void solution(int i, int a[], int sum1, int sum2, int& max, int n, x;
void solution(int i, int a[], int sum, int sums, int n, int& int& res, int n) { cin >> n >> x;
min) { if (i == n) { vector<int> a(n);
if (i == n) { if (sum1 == sum2 && sum1 > max) { for (int i = 0; i < n; i++) {
int x = abs(sums - sum); max = sum1; cin >> a[i];
if (abs(x - sum) <= min) { res = sum2 + sum1;
}
min = abs(x }
- sum); return; vector<int> dp(x + 1, 1e7);
} } dp[0] = 0;
return; solution(i + 1, a, sum1, sum2, max, res, n); for (int i = 0; i < n; i++) {
} solution(i + 1, a, sum1 + a[i], sum2, max, res, n); for (int j = a[i]; j <= x; j++) {
solution(i + 1, a, sum, sums, n, min); solution(i + 1, a, sum1, sum2 + a[i], max, res, n); dp[j] = min(dp[j], dp[j - a[i]] + 1);
solution(i + 1, a, sum + a[i], sums, n, // tong tats ca truong howp 2 mang con
}
min); }
} int main() { }
int main() { int n; if (dp[x] == 1e7) {
int n; cin >> n; cout << -1;
cin >> n; int res = 0; return 0;
int a[100]; int a[100]; }
int sum = 0; int max = 0;
cout << dp[x];
int min = 9999999999; for (int i = 0; i < n; i++) {
for (int i = 0; i < n; i++) { cin >> a[i]; }
cin >> a[i]; }
sum += a[i]; sort(a, a + n, greater<int>());
} solution(0, a, 0, 0, max, res, n);
solution(1, a, a[0], sum, n, min); cout << res;
cout << min; return 0;
return 0; }
}
#include<iostream> int main() { DP CHUỖI CON CHUNG DÀI int main() {
#include<vector> string a, b; NHẢY CÓC VỚI GIÁ TRỊ NHỎ NHẤT
DP LIS cin >> a >> b; ios_base::sync_with_stdio(false);
using namespace std; a.insert(a.begin(), ' '); cin.tie(NULL);
int main() { b.insert(b.begin(), ' '); int n;
int n; vector<vector<int>> dp(a.size(), vector<int>(b.size(), cin >> n;
cin >> n; 0)); vector<int> a(n + 1);
vector<long long> a(n); for (int i = 1; i < a.size(); i++) { for (int i = 1; i <= n; i++) {
for (int i = 0; i < n; i++) { for (int j = 1; j < b.size(); j++) { cin >> a[i];
cin >> a[i]; if (a[i] == b[j]) { }
} dp[i][j] = dp[i - 1][j - 1] + 1; vector<int> dp(n + 1, 0);
vector<long long> L; } dp[1] = 0;
for (int i = 0; i < n; i++) { else { dp[2] = abs(a[2] - a[1]);
auto it = lower_bound(L.begin(), L.end(), a[i]); dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); for (int i = 3; i <= n; i++) {
if (it == L.end())L.push_back(a[i]); } dp[i] = min(dp[i - 1] + abs(a[i - 1] - a[i]), dp[i - 2] +
else { } abs(a[i - 2] - a[i]));
*it = a[i]; } }
} cout << dp[a.size() - 1][b.size() - 1]; cout << dp[n];
} } }
cout << L.size();
}

int main() { int main() {DP CÁI TÚI KHÓ int main() {
DP CÁI TÚI DỄ ios_base::sync_with_stdio(false); ios_base::sync_with_stdio(false); DP SỐ CÁCH ĐỔ XÚC SẮC BẰNG N
cin.tie(NULL); cin.tie(NULL);
int n;
int n, k; long long n, k;
cin >> n >> k; cin >> n >> k; cin >> n;
vector<pair<long long, long long>> a(n + 1); int sum = 0; vector<long long > dp(n + 1, 0);
for (int i = 1; i <= n; i++) { vector<pair<long long, long long>> a(n + 1); dp[0] = 1;
cin >> a[i].first >> a[i].second; for (int i = 1; i <= n; i++) { for (int i = 1; i <= n; i++) {
} cin >> a[i].first >> a[i].second; for (int j = 1; j <= 6; j++) {
vector<vector<long long>> dp(n + 1, vector<long sum += a[i].second;
if (i >= j) {
long>(k + 1, 0)); }
for (int i = 1; i <= n; i++) { sort(a.begin() + 1, a.end()); dp[i] = (dp[i] + dp[i - j]) % M;
for (int j = 0; j <= k; j++) { vector<long long> dp(sum + 1, 1e9); }
if (j < a[i].first) { dp[0] = 0; }
dp[i][j] = dp[i - 1][j]; for (int i = 1; i <= n; i++) { }
} for (int j = sum; j >= a[i].second; j--) { cout << dp[n];
else {
}
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - dp[j] = min(dp[j], dp[j - a[i].second] + a[i].first);
a[i].first] + a[i].second);
} }
} }
} for (int i = sum; i >= 0; i--) {
cout << dp[n][k]; if (dp[i] <= k) {
} cout << i;
return 0;
}
}
}
queue<pair<int, int>> p; VD 2 CON TRỎ VỚI PHÂN ĐOẠN NHỎ VD __int128 VÀ DP TỔ HỢP
TÌM ĐƯỜNG NGẮN NHẤT A-B TRONG ME NHẤT using namespace std;
void xuat(__int128 x) {
CUNG (BFS) int main() {
string s;
p.push({ A.first,A.second }); long long n, s; while (x > 0) {
vector<vector<int>> visited(n, vector<int>(m, - cin >> n >> s; s += x % 10 + 48;
1)); vector<long long> a(n); x /= 10;
visited[A.first][A.second] = 0; for (int i = 0; i < n; i++) { }
while (!p.empty()) { cin >> a[i]; for (int i = s.size() - 1; i >= 0; i--) {
cout << s[i];
pair<int, int> t; }
}
t = p.front(); long long ans = 0, sum = 0, count = 0; }
p.pop(); for (int l = 0, r = 0; r < n; r++) { int main() {
for (int i = 0; i < 4; i++) { sum += a[r]; int n, k;
int x1 = t.first + x[i]; count++; cin >> n >> k;
int y1 = t.second + y[i]; while (sum > s) { vector<vector<__int128>> dp(101,
vector<__int128>(100 + 1, 0));
if (x1 >= 0 && y1 >= 0 && x1 < n && y1 sum -= a[l];
dp[1][0] = 1;
< m && visited[x1][y1] == -1 && a[x1][y1] != l++; dp[1][1] = 1;
'#') { count--; for (int i = 2; i <= 100; i++) {
p.push({ x1,y1 }); if (ans < count) { for (int j = 0; j <= i; j++) {
visited[x1][y1] = visited[t.first][t.second] ans = count; if (i == j || j == 0)dp[i][j] = 1;
+ 1; } else {
dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
} }
}
} } }
} cout << ans; }
cout << visited[B.first][B.second]; } xuat(dp[n][k]);
return 0; }

TÌM SỐ NT
SÀNG CT NHANH
void sang(vector<int>& nT) { bool laNT(int n) {
nT[0] = 0; if (n <= 1)return 0;
nT[1] = 0; if (n == 2 || n == 3)return 1;
if (n % 2 == 0 || n % 3 == 0 || (n % 5 == 0 && n != 5) || (n % 7 == 0
for (int i = 2; i <= 1000; i++) { && n != 7))return 0;
if (nT[i] == 1) { int i = 5;
for (int j = i * i; j <= 1000000; j += i) { while (i <= sqrt(n)) {
nT[j] = 0; if (n % i == 0 || n % (i + 2) == 0) {
} return 0;
} }
} i += 6;
}
} return 1;
}

#include<iostream>
#include<string>
#include<queue>
VD LOANG DẦU (BFS)
using namespace std;
int x[] = { 0,1,0,-1 };
int y[] = { -1,0,1,0 };
int solution(char a[1000][1000], int n, int m) {
vector<vector<int>> visited(n, vector<int>(m, 0));
queue<pair<int, int>> q;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == '2') {
q.push({ i, j });
visited[i][j] = 1;
}
}
}
int res = 0;
while (!q.empty()) {
res++;
int s = q.size();
for (int i = 0; i < s; i++) {
pair<int, int> t = q.front();
q.pop();
for (int j = 0; j < 4; j++) {
int x1 = t.first + x[j];
int y1 = t.second + y[j];
if (x1 >= 0 && y1 >= 0 && x1 < n && y1 < m && a[x1][y1] == '1' && visited[x1][y1] == 0) {
q.push({ x1,y1 });
visited[x1][y1] = 1;
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == '1' && visited[i][j] == 0) {
return -1;
}
}
}
return res - 1;
}
int main() {
int n, m;
char a[1000][1000];
cin >> n >> m;
for (int i = 0; i < n; i++) {
string t;
cin >> t;
for (int j = 0; j < m; j++) {
a[i][j] = t[j];
}
}
cout << solution(a, n, m);
}
Stack:- Queue: #include <bits/stdc++.h> priority_queue <int,vector<int>,greater<int> > q; //
using namespace std;
size : trả về kích thước hiện tại của - size : trả về kích thước hiện tại của đầu luôn là giá trị nhỏ nhất
SINH CHỈNH HƠP KO LẶP priority_queue<int>q //đầu luôn là giá trị lớn nhất
stack. ĐPT O(1). queue. ĐPT O(1). int n, k, a[100], c[100]; - size : trả về kích thước hiện tại của priority queue.
- empty : true stack nếu rỗng, và ngược - empty : true nếu queue rỗng, và
void printResult() { ĐPT O(1)
lại. ĐPT O(1). ngược lại. ĐPT O(1). for (int i = 1; i <= k; -i++) { : true nếu priority queue rỗng, và ngược
empty
- push : đẩy phần tử vào stack. ĐPT - push : đẩy vào cuối queue. ĐPT O(1).
cout << a[i] << " ";
lại. ĐPT O(1).
O(1). - pop: loại bỏ phần tử (ở } đầu). ĐPT - push : đẩy vào priority queue. ĐPT O(logN).
- pop : loại bỏ phẩn tử ở đỉnh của stack. O(1). cout << endl; - pop: loại bỏ phần tử ở đỉnh priority queue. ĐPT
} O(logN).
ĐPT O(1). - front : trả về phần tử ở đầu. ĐPT O(1).
void Try(int i) { - top : trả về phần tử ở đỉnh priority queue. ĐPT
- top : truy cập tới phần tử ở đỉnh stack. - back: trả về phần tử ở cuối. ĐPT O(1).
for (int j = 1; j <= n; j++) {
O(1).
ĐPT O(1). if (!c[j]) {
Set: Multiset (Tập hợp): a[i] = j; st.equal_range(val):
Capacity: - Multiset giống như Set nhưng có thể c[j]chứa
= 1; các -Trong đó val là giá trị phần tử cần tìm trong set st.
- size : trả về kích thước hiện tại của set. ĐPT khóa có giá trị giống nhau. if (i == k) { Hàm equal_range() sẽ trả về một cặp giá trị, với giá
O(1) - Khai báo : giống như set. printResult();trị đầu tiên trỏ đến đầu phạm vi (tương đương kết
- empty : true nếu set rỗng, và ngược lại. ĐPT Capacity: } else { quả hàm lower_bound), và giá trị thứ hai trỏ đến
- size : trả về kích thước hiện tại của multiset.
Try(i + 1); cuối phạm vi (tương đương kết quả hàm
O(1).
- empty : true nếu multiset rỗng, và ngược} lại.
Modifiers: upper_bound)chứa tất cả các phần tử có giá trị
Chỉnh sửa: c[j] = 0;
- insert : Chèn phần tử vào set. ĐPT O(logN). - insert : Chèn phần tử vào set. ĐPT}O(logN).
giống giá trị chỉ định.
- erase : có 2 kiểu xóa: xóa theo iterator, hoặc - erase :
là xóa theo khóa. ĐPT } st.lower_bound(val);
o xóa theo iterator ĐPT O(logN)
O(logN). } Trong đó val là giá trị của phần tử cần tìm trong set
o xóa theo khóa: xóa tất cả các phần tử bằng
- clear : xóa tất cả set. ĐPT O(n). int main() { st.
khóa trong multiset
- swap : đổi 2 set cho nhau. ĐPT O(n). ĐPT: O(logN) + số phần tử bị xóa.cin >> n >> k; hàm lower_bound() sẽ trả về trình lặp trỏ đến vị trí
Operations: - clear : xóa tất cả set. ĐPT O(n). Try(1); vị trí phần tử đầu tiên có giá trị lớn hơn hoặc bằng
- find : trả về itarator trỏ đến phần tử cần tìm return 0;
- swap : đổi 2 set cho nhau. ĐPT O(n). với giá trị chỉ định. Và nếu không tìm thấy, hàm sẽ
kiếm. Nếu không tìm Operations: } trả về trình lặp trỏ đến vị trí cuối cùng trong set.
thấy itarator trỏ về “end” của set. ĐPT - find : trả về itarator trỏ đến phần tử cần tìm
O(logN). kiếm. Nếu không tìm
thấy itarator trỏ về “end” của set. ĐPT O(logN). st.upper_bound(key);
- lower_bound : trả về iterator đến vị trí phần
Dù trong multiset có
tử bé nhất mà không bé Trong đó key là giá trị của phần tử cần
nhiều phần tử bằng khóa thì nó cũng chỉ iterator
hơn (lớn hơn hoặc bằng) khóa (dĩ nhiên là đến một phần tử. tìm trong set st.
theo phép so sánh), nếu - lower_bound : trả về iterator đến vị trí phần tử Hàm upper_bound() sẽ trả về trình lặp
không tìm thấy trả về vị trí “end” của set. ĐPT bé nhất mà không bé trỏ đến vị trí phần tử đầu tiên có giá
O(logN). hơn (lớn hơn hoặc bằng) khóa (dĩ nhiên là theo trị lớn hơn giá trị chỉ định. Và nếu
- upper_bound: trả về iterator đến vị trí phần phép so sánh), nếu không tìm thấy, hàm sẽ trả về trình lặp
tử bé nhất mà lớn hơn không tìm thấy trả về vị trí “end” của set. ĐPT
trỏ đến vị trí cuối cùng trong map.
khóa, nếu không tìm thấy trả về vị trí “end” O(logN).
của set.. ĐPT O(logN). - upper_bound: trả về iterator đến vị trí phần tử
bé nhất mà lớn hơn st.count(val);
- count : trả về số lần xuất hiện của khóa
trong container. Nhưng trong khóa, nếu không tìm thấy trả về vị trí “end” của Trong đó val là giá trị của phần tử cần
set, các phần tử chỉ xuất hiện một lần, nên set.. ĐPT O(logN). đếm số lần xuất hiện trong set st.
hàm này có ý nghĩa là sẽ - count : trả về số lần xuất hiện của khóa trong Do vậy, kết quả trả về của hàm count()
multiset. ĐPT O(logN) cũng chỉ là 0 tương ứng với phần tử
return 1 nếu khóa có trong container, và 0
+ số phần tử tìm được.
nếu không có. ĐPT O(logN). không tồn tại, hoặc 1 tương ứng với
phần tử tồn tại trong set đó.
Với multilset thì nó trả về số lượng giá
trị cần tìm
10.Map (Ánh xạ): 3.2. lower_bound:
- Map là một loại associative container. Mỗi phần tử của map là sự kết hợp của - Hàm lower_bound và upper_bound tương tự như ở trong container set hay map.
khóa(key value) và ánh xạ của nó (mapped value). Cũng giống như set, trong map - Trả về iterator đến phần tử đầu tiên trong nửa đoạn [first,last] mà không bé hơn
không chứa các khóa mang giá trị giống nhau. khóa
- Trong map, các khóa được sử dụng để xác định giá trị các phần tử. Kiểu của khóa và tìm kiếm.
ánh xạ có thể khác nhau. - Dạng 1: lower_bound( đầu , cuối , khóa );
- Và cũng giống như set, các phần tử trong map được sắp xếp theo một trình tự nào - Dạng 2: lower_bound( đầu , cuối , khóa , phép toán so sánh của đoạn)
đótheo cách so sánh. 3.3. upper_bound
- Map được cài đặt bằng red-black tree (cây đỏ đen) – một loại cây tìm kiếm nhị - Trả về iterator đến phần tử đầu tiên trong nửa đoạn [first,last] mà lớn hơn khóa tìm
phântự cân bằng. Mỗi phần tử của map lại được cài đặt theo kiểu pair (xem thêm ở kiếm.
thư viện utility). - Dạng 1: upper_bound ( đầu , cuối , khóa );
Capacity: - Dạng 2: upper_bound ( đầu , cuối , khóa, phép toán so sánh của đoạn)
- size : trả về kích thước hiện tại của map. ĐPT O(1)
- empty : true nếu map rỗng, và ngược lại. ĐPT O(1).Truy cập tới phần tử: 3.1. binary_search:
- operator [khóa]: Nếu khóa đã có trong map, thì hàm này sẽ trả về giá - tìm kiếm xem khóa có trong đoạn cần tìm không. Lưu ý: đoạn tìm kiếm phải được
trị mà khóa ánh xạ đến. Ngược lại, nếu khóa chưa có trong map, thìkhi gọi [] nó sẽ sắp
thêm vào map khóa đó. ĐPT O(logN) xếp theo một trật tự nhất đinh. Nếu tìm được sẽ return true, ngược lại return false.
Chỉnh sửa - Dạng 1: binary_search(vị trí bắt đầu, vị trí kết thúc, khóa);
- insert : Chèn phần tử vào map. Chú ý: phần tử chèn vào phải ở kiểu “pair”. - Dạng 2: binary_search(vị trí bắt đầu, vị trí kết thúc, khóa, phép so sánh);
- erase :
o xóa theo iterator ĐPT O(logN)
o xóa theo khóa: xóa khóa trong map. ĐPT: O(logN).
- clear : xóa tất cả set. ĐPT O(n).
- swap : đổi 2 set cho nhau. ĐPT O(n).
Operations:
- find : trả về itarator trỏ đến phần tử cần tìm kiếm. Nếu không tìm
thấy iterator trỏ về “end” của map. ĐPT O(logN).
- lower_bound : trả về iterator đến vị trí phần tử bé nhất mà không bé
hơn (lớn hơn hoặc bằng) khóa (dĩ nhiên là theo phép so sánh), nếu
không tìm thấy trả về vị trí “end” của map. ĐPT O(logN).
- upper_bound: trả về iterator đến vị trí phần tử bé nhất mà lớn hơn
khóa, nếu không tìm thấy trả về vị trí “end” của map. ĐPT O(logN).
- count : trả về số lần xuất hiện của khóa trong multiset. ĐPT O(logN)
#include<bits/stdc++.h>
using namespace std;
SINH CHỈNH HỢP LẶP
int n, k, a[100];

void printResult() {
for (int i = 1; i <= k; i++) {
cout << a[i] << " ";
}
cout << endl;
}

void Try(int i) {
for (int j = 1; j <= n; j++) {
a[i] = j;
if (i == k) {
printResult();
} else {
Try(i + 1);
}
}
}

int main() {
cin >> n >> k;
Try(1);
return 0;
}

#include<iostream> __int128 THCKN(int n, int k) {


#include<vector>
#include<algorithm> if (k > n - k) {
#define MAX 200000 k = n - k;
using namespace std;
MẢNG CON CO TỔNG LƠN NHÂT }
long long Divede(long long a[], int l, int m, int h) { __int128 result = 1;
long long sum = 0, l_sum = -9999999999, r_sum = -9999999999;
for (int i = m; i >= l; i--) { for (int i = 0; i < k; ++i) {
sum = sum + a[i]; result *= (n - i);
if (sum > l_sum) {
l_sum = sum; result /= (i + 1);
} }
}
sum = 0;
for (int i = m + 1; i <= h; i++) { return result;
sum = sum + a[i];
if (sum > r_sum) { }
r_sum = sum; CT TINH TỔ HỢP KO CẦN GT
}
}
return l_sum + r_sum; VD MAP
}
int main(){
int n;
long long conquer(long long a[], int l, int h) {
long long b[100000];
if (l == h) return a[l];
cin>>n;
int m = (l + h) / 2;
map<long long,long long> a;
return max(max(conquer(a, l, m), conquer(a, m + 1, h)), Divede(a, l, m,
for(int i=0;i<n;i++){
h));
long long x;
}
cin>>x;
a[x]++;
int main() {
cin>>x;
int n;
a[x]--;
cin >> n;
}
long long a[MAX];
int max=0;
for (int i = 0; i < n; i++) {
int sum=0;
cin >> a[i];
for(auto it:a){
}
sum+=it.second;
cout << conquer(a, 0, n-1 );
if(sum>max){
return 0;
max=sum;
}
}
}
cout<<max;
return 0;
}
MẢNG CỘNG DỒN DEMO QUEUE DEMO PRIORITY_QUEUE
vector<int> buildPrefixSum(const #include <iostream> #include <iostream>
vector<int>& a, int C = 0) { #include <queue> #include <queue>
using namespace std; #include <vector>
int n = (int)a.size(); using namespace std;
queue <int> q;
vector<int> prefixSum(n + 1); main() {
int i;
priority_queue < int , vector <int> , greater <int> >
main() {
prefixSum[0] = C; p; // p={}
for (i=1;i<=5;i++) q.push(i); // q={1,2,3,4,5}
p.push(1); // p={1}
q.push(100); // q={1,2,3,4,5,100} p.push(5); // p={1,5}
for (int i = 0; i < n; i++) cout << q.front() << endl; // In ra 1 cout << p.top() << endl; // In ra 1
prefixSum[i + 1] = prefixSum[i] + a[i]; q.pop(); // q={2,3,4,5,100} p.pop(); // p={5}
cout << q.back() << endl; // In ra 100 cout << p.top() << endl; // In ra 5
cout << q.empty() << endl; // In ra 0 p.push(9); // p={5,9}
return prefixSum;
cout << q.size() << endl; // In ra 5 cout << p.top() << endl; // In ra 5
} system("pause"); system("pause");
} }
DEMO STACK DEMO SET DEMO MAP
#include <iostream> #include <iostream>
#include <stack> #include <set> #include <iostream>
using namespace std; using namespace std; #include <map>
stack <int> s; main() {
int i;
#include <vector>
set <int> s;
main() { set <int> ::iterator it;
using namespace std;
for (i=1;i<=5;i++) s.push(i); // s={1,2,3,4,5}
s.insert(9); // s={9} main() {
s.push(100); // s={1,2,3,4,5,100}
cout << s.top() << endl; // In ra 100
s.insert(5); // s={5,9} map <char,int> m;
s.pop(); // s={1,2,3,4,5}
cout << *s.begin() << endl; //In ra 5 map <char,int> :: iterator it;
cout << s.empty() << endl; // In ra 0 s.insert(1); // s={1,5,9}
cout << *s.begin() << endl; // In ra 1 m['a']=1; // m={{'a',1}}
cout << s.size() << endl; // In ra 5
system("pause"); it=s.find(5); m.insert(make_pair('b',2)); //
if (it==s.end()) cout << "Khong co trong m={{'a',1};{'b',2}}
container" << endl;
XÓA CÁC PHẦN TỬ GIỐNG NHAU else cout << "Co trong container" << endl;
m.insert(pair<char,int>('c',3) ); //
v.erase(std::unique(v.begin(), v.end()), v.end()); s.erase(it); // s={1,9} m={{'a',1};{'b',2};{'c',3}}
GẮN TỪNG CHỮ VÀO VECTOR STRING s.erase(1); // s={9} cout << m['b'] << endl; // In ra 2
s.insert(3); // s={3,9} m['b']++; // m={{'a',1};{'b',3};{'c',3}}
s.insert(4); // s={3,4,9}
it=s.lower_bound(4); it=m.find('c'); // it point to key 'c'
if (it==s.end()) cout << "Khong co phan tu nao cout << it->first << endl; // In ra 'c'
trong set khong be hon 4" << endl; cout << it->second << endl; // In ra 3
else cout << "Phan tu be nhat khong be hon 4
la " << *it << endl; // In ra 4
m['e']=100; //m={{'a',1};{'b',3};{'c',3};
it=s.lower_bound(10); {'e',100}}
if (it==s.end()) cout << "Khong co phan tu nao it=m.lower_bound('d'); // it point to
trong set khong be hon 10" << endl; 'e'
else cout << "Phan tu be nhat khong be hon
10 la " << *it << endl; // Khong co ptu nao cout << it->first << endl; // In ra 'e'
it=s.upper_bound(4); cout << it->second << endl; // In ra
if (it==s.end()) cout << "Khong co phan tu nao 100
trong set lon hon 4" << endl;
else cout << "Phan tu be nhat lon hon 4 la " <<
system("pause");
*it << endl; // In ra 9 }
/* Duyet set */
for (it=s.begin();it!=s.end();it++) {
cout << *it << " ";
}
// In ra 3 4 9
cout << endl;
system("pause");

#include<iostream> #include<iostream> #include<iostream>


using namespace std; using namespace std; using namespace std;
MERGE SORT QUICK SORT BINARY SEARCH
void merge(int arr[], int l, int m, int r){ int phanhoachphantucuoi(int a[],int p,int bool binarysearch(int a[],int n,int b){
int n1 = m - l + 1; int x=n;
r){
int n2 = r - m; int y=1;
int L[1000], R[1000];
int x=a[r]; if(a[0]==b||a[1]==b){
for(int i = 0; i < n1; i++)
return true;
L[i] = arr[l + i]; int i=p-1;
}
for(int j = 0; j < n2; j++) for(int j=p;j<=r-1;j++){
R[j] = arr[m + 1 + j]; while(y<=x){
if(a[j]<x){ int mid=y+(x-y)/2;
int i = 0; i++;
int j = 0; if(a[mid]==b){
swap(a[i],a[j]); return true;
int k = l;
while (i < n1 && j < n2) { } }
if (L[i] <= R[j]) { } else if(a[mid]<b){
arr[k] = L[i]; swap(a[i+1],a[r]); y=mid+1;
i++; return i+1; }
} } else{
else { void quicksort(int a[],int p,int r){ x=mid-1;
arr[k] = R[j]; }
if(p<r){
j++; }
//cnt res+=n1-i; int q=phanhoachphantucuoi(a,p,r);
quicksort(a,p,q-1); return false;
}
quicksort(a,q+1,r); }
k++;
} }
while (i < n1) { int main(){
}
arr[k] = L[i]; int a[1000]={2,4,5,7,1,2,3,6};
void xuat(int a[],int n){
i++; int n=8,x1=4,x2=8;
for(int i=0;i<n;i++){ if(binarysearch(a,n,x1)==1){
k++;
} cout<<a[i]<<" "; cout<<"tim thay x";
while (j < n2) { } }else{
arr[k] = R[j]; } cout<<"khong tim thay x";
j++; int main(){ }
k++; int a[1000]={2,4,5,7,1,2,3,6}; cout<<endl;
} int r=8; if(binarysearch(a,n,x2)==1){
} cout<<"tim thay x";
quicksort(a,0,r-1);
void mergeSort(int arr[], int l, int r) {
xuat(a,r); }else{
if (l < r) {
} cout<<"khong tim thay x";
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
}
mergeSort(arr, m + 1, r); return 0;
merge(arr, l, m, r); }
}
}
int main() {
int arr[1000]={2,4,5,7,1,2,3,6};
int arr_size=8;
mergeSort(arr, 0, arr_size - 1);
return 0;
}

You might also like