# Sắp xếp (Sortings) ------------------ [TOC] ------------------ ## **Bài tập** ### **Bài 1:** [**Young Explorers**](https://codeforces.com/contest/1355/problem/B) - **Tóm đề** - Cho mảng $a$ có $n$ phần tử, đếm số subset tạo được sao cho mọi phần tử trong subset phải bé hơn kích thước của subset. (*Lưu ý: Mỗi phần tử của mảng chỉ thuộc **duy nhất** một subset*). - Cho mảng $a$ có $n$ phần tử, tính tổng của tích tất cả bộ 3 số $a[i], a[j], a[k]$ $(1 ≤ i < j < k ≤ n)$ ### **Bài 2:** [**Less or Equal**](https://codeforces.com/problemset/problem/977/C) - **Tóm đề** - Cho mảng $a$ có $n$ phần tử và số nguyên $k$, in ra $x$ sao cho có đúng $k$ phần tử trong mảng $a$ bé hơn hoặc bằng $x$. ## **Lời giải** ### [**Young Explorers**](https://codeforces.com/contest/1355/problem/B) - **Code mẫu** ```cpp= #include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n; int a[N]; void sol(){ cin >> n; for(int i = 1; i <= n; ++i) cin >> a[i]; sort(a + 1, a + n + 1); int ans = 0, cnt = 0; for(int i = 1; i <= n; ++i){ ++cnt; if(cnt >= a[i]){ ++ans; cnt = 0; } } cout << ans << "\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while(t--) sol(); return 0; } ``` ### [**Less or Equal**](https://codeforces.com/problemset/problem/977/C) - **Code mẫu** ```cpp= #include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n, k, a[N]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> k; for(int i = 1; i <= n; ++i) cin >> a[i]; sort(a + 1, a + n + 1); if((a[k+1] == a[k]) || (k == 0 && a[1] - 1 <= 0)) cout << -1; else { if(k == 0) cout << a[1] - 1; else cout << a[k]; } return 0; } ```