output
stringlengths
52
181k
instruction
stringlengths
296
182k
#include <bits/stdc++.h> using namespace std; const int N = int(2e5) + 99; const int INF = int(1e9) + 100; int t; int n; long long s; pair<int, int> p[N]; bool ok(int mid) { long long sum = 0; int cnt = 0; vector<int> v; for (int i = 0; i < n; ++i) { if (p[i].second < mid) sum += p[i].first; else if (p[i].first >= mid) { sum += p[i].first; ++cnt; } else v.push_back(p[i].first); } assert(is_sorted(v.begin(), v.end())); int need = max(0, (n + 1) / 2 - cnt); if (need > v.size()) return false; for (int i = 0; i < v.size(); ++i) { if (i < v.size() - need) sum += v[i]; else sum += mid; } return sum <= s; } int main() { scanf("%d", &t); for (int tc = 0; tc < t; ++tc) { scanf("%d %lld", &n, &s); for (int i = 0; i < n; ++i) scanf("%d %d", &p[i].first, &p[i].second); sort(p, p + n); int lf = 1, rg = INF; while (rg - lf > 1) { int mid = (lf + rg) / 2; if (ok(mid)) lf = mid; else rg = mid; } printf("%d\n", lf); } return 0; }
### Prompt In Cpp, your task is to solve the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = int(2e5) + 99; const int INF = int(1e9) + 100; int t; int n; long long s; pair<int, int> p[N]; bool ok(int mid) { long long sum = 0; int cnt = 0; vector<int> v; for (int i = 0; i < n; ++i) { if (p[i].second < mid) sum += p[i].first; else if (p[i].first >= mid) { sum += p[i].first; ++cnt; } else v.push_back(p[i].first); } assert(is_sorted(v.begin(), v.end())); int need = max(0, (n + 1) / 2 - cnt); if (need > v.size()) return false; for (int i = 0; i < v.size(); ++i) { if (i < v.size() - need) sum += v[i]; else sum += mid; } return sum <= s; } int main() { scanf("%d", &t); for (int tc = 0; tc < t; ++tc) { scanf("%d %lld", &n, &s); for (int i = 0; i < n; ++i) scanf("%d %d", &p[i].first, &p[i].second); sort(p, p + n); int lf = 1, rg = INF; while (rg - lf > 1) { int mid = (lf + rg) / 2; if (ok(mid)) lf = mid; else rg = mid; } printf("%d\n", lf); } return 0; } ```
#include <bits/stdc++.h> using namespace std; vector<pair<int, int>> v; int t, n; long long s; bool ok(int x) { vector<pair<int, int>> b, c; int cnt = 0; long long aux = 0; for (const auto& [l, r] : v) if (r < x) cnt++, aux += l; else if (x < l) aux += l; else c.emplace_back(l, r); sort(c.begin(), c.end()); reverse(c.begin(), c.end()); if (cnt > n / 2) return false; while (cnt < n / 2 && !c.empty()) { cnt++; aux += c.back().first; c.pop_back(); } aux += 1LL * x * c.size(); return aux <= s; } int bs() { int beg = 0, end = 1e9, mid; while (beg < end) { mid = (beg + end + 1) >> 1; if (ok(mid)) beg = mid; else end = mid - 1; } return beg; } int main() { for (scanf("%d", &t); t--;) { scanf("%d %lld", &n, &s); v.clear(); for (int i = 1, l, r; i <= n; ++i) scanf("%d %d", &l, &r), v.emplace_back(l, r); printf("%d\n", bs()); } }
### Prompt Please provide a Cpp coded solution to the problem described below: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<pair<int, int>> v; int t, n; long long s; bool ok(int x) { vector<pair<int, int>> b, c; int cnt = 0; long long aux = 0; for (const auto& [l, r] : v) if (r < x) cnt++, aux += l; else if (x < l) aux += l; else c.emplace_back(l, r); sort(c.begin(), c.end()); reverse(c.begin(), c.end()); if (cnt > n / 2) return false; while (cnt < n / 2 && !c.empty()) { cnt++; aux += c.back().first; c.pop_back(); } aux += 1LL * x * c.size(); return aux <= s; } int bs() { int beg = 0, end = 1e9, mid; while (beg < end) { mid = (beg + end + 1) >> 1; if (ok(mid)) beg = mid; else end = mid - 1; } return beg; } int main() { for (scanf("%d", &t); t--;) { scanf("%d %lld", &n, &s); v.clear(); for (int i = 1, l, r; i <= n; ++i) scanf("%d %d", &l, &r), v.emplace_back(l, r); printf("%d\n", bs()); } } ```
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 100; const int N = 2e5 + 100; pair<int, int> vec[N]; int n; long long s; bool check(int mid) { vector<int> temp; long long sum = 0; int cnt = 0; for (int i = 0; i < n; ++i) { if (vec[i].second < mid) { sum += vec[i].first; } else if (vec[i].first >= mid) { sum += vec[i].first; cnt++; } else { temp.emplace_back(vec[i].first); } } int req = max(0, (n + 1) / 2 - cnt); if (req > temp.size()) return false; for (int i = 0; i < temp.size(); ++i) { if (i < temp.size() - req) sum += temp[i]; else sum += mid; } return sum <= s; } int main() { int t; cin >> t; while (t--) { cin >> n >> s; for (int i = 0; i < n; ++i) cin >> vec[i].first >> vec[i].second; sort(vec, vec + n); int lf = 1, rg = INF; while (rg - lf > 1) { int mid = (lf + rg) / 2; if (check(mid)) lf = mid; else rg = mid; } cout << lf << "\n"; } return 0; }
### Prompt Create a solution in cpp for the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 100; const int N = 2e5 + 100; pair<int, int> vec[N]; int n; long long s; bool check(int mid) { vector<int> temp; long long sum = 0; int cnt = 0; for (int i = 0; i < n; ++i) { if (vec[i].second < mid) { sum += vec[i].first; } else if (vec[i].first >= mid) { sum += vec[i].first; cnt++; } else { temp.emplace_back(vec[i].first); } } int req = max(0, (n + 1) / 2 - cnt); if (req > temp.size()) return false; for (int i = 0; i < temp.size(); ++i) { if (i < temp.size() - req) sum += temp[i]; else sum += mid; } return sum <= s; } int main() { int t; cin >> t; while (t--) { cin >> n >> s; for (int i = 0; i < n; ++i) cin >> vec[i].first >> vec[i].second; sort(vec, vec + n); int lf = 1, rg = INF; while (rg - lf > 1) { int mid = (lf + rg) / 2; if (check(mid)) lf = mid; else rg = mid; } cout << lf << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10, maxn = 2e5 + 10; int n, m; long long s; struct node { int l, r; bool operator<(const node &x) const { return l < x.l; } } a[maxn]; bool ok(int x) { long long sum = 0; for (int i = 0; i < n; ++i) sum += a[i].l; int c = 0; for (int i = n - 1; i >= 0 && c < m; --i) { if (a[i].r >= x) { sum += max(x - a[i].l, 0); c++; } } return c == m && sum <= s; } int erfen() { int l = 0, r = 1e9 + 7; int mi = 0; while (l <= r) { int mid = (l + r) / 2; if (ok(mid)) { mi = max(mi, mid); l = mid + 1; } else r = mid - 1; } return mi; } int main() { int t; scanf("%d", &t); while (t--) { scanf("%d%I64d", &n, &s); m = (n + 1) / 2; for (int i = 0; i < n; ++i) scanf("%d%d", &a[i].l, &a[i].r); sort(a, a + n); printf("%d\n", erfen()); } return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10, maxn = 2e5 + 10; int n, m; long long s; struct node { int l, r; bool operator<(const node &x) const { return l < x.l; } } a[maxn]; bool ok(int x) { long long sum = 0; for (int i = 0; i < n; ++i) sum += a[i].l; int c = 0; for (int i = n - 1; i >= 0 && c < m; --i) { if (a[i].r >= x) { sum += max(x - a[i].l, 0); c++; } } return c == m && sum <= s; } int erfen() { int l = 0, r = 1e9 + 7; int mi = 0; while (l <= r) { int mid = (l + r) / 2; if (ok(mid)) { mi = max(mi, mid); l = mid + 1; } else r = mid - 1; } return mi; } int main() { int t; scanf("%d", &t); while (t--) { scanf("%d%I64d", &n, &s); m = (n + 1) / 2; for (int i = 0; i < n; ++i) scanf("%d%d", &a[i].l, &a[i].r); sort(a, a + n); printf("%d\n", erfen()); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long INF = 1000 * 1000 * 1000; const long long int mod = 1000000007; long long int l[300000], r[300000]; int main() { std::ios::sync_with_stdio(false); int t; cin >> t; while (t--) { int n, c; cin >> n; long long int s, sum = 0; cin >> s; for (int i = 0; i < n; i++) { cin >> l[i] >> r[i]; s -= l[i]; } long long int a = 0, b = INF, ans, mid; bool flag = false; c = n / 2 + 1; while (a <= b) { priority_queue<long long int> p; flag = false; mid = (a + b) / 2; sum = 0; for (int i = 0; i < n; i++) { if (r[i] >= mid) { p.push(l[i]); } } if (p.size() >= c) { for (int i = 0; i < c; i++) { sum += max(0ll, mid - p.top()); p.pop(); } if (sum <= s) { flag = true; } } if (flag) { ans = mid; a = mid + 1; } else { b = mid - 1; } } cout << ans << '\n'; } }
### Prompt Develop a solution in CPP to the problem described below: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long INF = 1000 * 1000 * 1000; const long long int mod = 1000000007; long long int l[300000], r[300000]; int main() { std::ios::sync_with_stdio(false); int t; cin >> t; while (t--) { int n, c; cin >> n; long long int s, sum = 0; cin >> s; for (int i = 0; i < n; i++) { cin >> l[i] >> r[i]; s -= l[i]; } long long int a = 0, b = INF, ans, mid; bool flag = false; c = n / 2 + 1; while (a <= b) { priority_queue<long long int> p; flag = false; mid = (a + b) / 2; sum = 0; for (int i = 0; i < n; i++) { if (r[i] >= mid) { p.push(l[i]); } } if (p.size() >= c) { for (int i = 0; i < c; i++) { sum += max(0ll, mid - p.top()); p.pop(); } if (sum <= s) { flag = true; } } if (flag) { ans = mid; a = mid + 1; } else { b = mid - 1; } } cout << ans << '\n'; } } ```
#include <bits/stdc++.h> using namespace std; void solve() { int n; long long int k; cin >> n >> k; vector<pair<int, int> > a(n); for (int i = 0; i < n; i++) { cin >> a[i].first >> a[i].second; } sort(a.begin(), a.end()); int ans = -1; long long int l = 0, r = 2e9; for (int i = 0; i < 60; i++) { long long int m = l + (r - l) / 2; int less = 0, right = 0, center = 0; long long int money = 0; vector<int> c; for (auto &e : a) { if (e.second < m) { money += e.first; } else if (e.first >= m) { money += e.first; right++; } else if (e.first < m && m <= e.second) { c.push_back(e.first); } } int need = max(0, (n + 1) / 2 - right); if (need > c.size()) { r = m - 1; } else { for (int i = 0; i < c.size(); i++) { if (i < c.size() - need) money += c[i]; else money += m; } if (money <= k) { ans = m; l = m + 1; } else { r = m - 1; } } } cout << ans << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { solve(); } return 0; }
### Prompt Please create a solution in cpp to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void solve() { int n; long long int k; cin >> n >> k; vector<pair<int, int> > a(n); for (int i = 0; i < n; i++) { cin >> a[i].first >> a[i].second; } sort(a.begin(), a.end()); int ans = -1; long long int l = 0, r = 2e9; for (int i = 0; i < 60; i++) { long long int m = l + (r - l) / 2; int less = 0, right = 0, center = 0; long long int money = 0; vector<int> c; for (auto &e : a) { if (e.second < m) { money += e.first; } else if (e.first >= m) { money += e.first; right++; } else if (e.first < m && m <= e.second) { c.push_back(e.first); } } int need = max(0, (n + 1) / 2 - right); if (need > c.size()) { r = m - 1; } else { for (int i = 0; i < c.size(); i++) { if (i < c.size() - need) money += c[i]; else money += m; } if (money <= k) { ans = m; l = m + 1; } else { r = m - 1; } } } cout << ans << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { solve(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; int N; int l[maxn], r[maxn]; long long s; int check(int x) { int must_g = 0, must_s = 0; long long sum = 0; vector<int> num; for (int i = 1; i <= N; i++) { if (r[i] < x) must_s++, sum += l[i]; else if (l[i] > x) must_g++, sum += l[i]; else if (l[i] <= x && r[i] >= x) num.push_back(l[i]); } if (must_g > (N - 1) / 2) return 1; if (must_s > (N - 1) / 2) return 0; sort(num.begin(), num.end()); int remain = (N - 1) / 2 - must_s; for (int i = 0; i <= remain - 1; i++) sum += num[i]; remain = (N - 1) / 2 - must_g + 1; sum += (long long)remain * (long long)x; return sum <= s; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int T; cin >> T; while (T--) { cin >> N >> s; for (int i = 1; i <= N; i++) cin >> l[i] >> r[i]; int lll = 1, rrr = 1e9, ans = 0; while (lll <= rrr) { int mid = (lll + rrr) >> 1; if (check(mid)) { lll = mid + 1; ans = max(ans, mid); } else rrr = mid - 1; } cout << ans << "\n"; } return 0; }
### Prompt In Cpp, your task is to solve the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; int N; int l[maxn], r[maxn]; long long s; int check(int x) { int must_g = 0, must_s = 0; long long sum = 0; vector<int> num; for (int i = 1; i <= N; i++) { if (r[i] < x) must_s++, sum += l[i]; else if (l[i] > x) must_g++, sum += l[i]; else if (l[i] <= x && r[i] >= x) num.push_back(l[i]); } if (must_g > (N - 1) / 2) return 1; if (must_s > (N - 1) / 2) return 0; sort(num.begin(), num.end()); int remain = (N - 1) / 2 - must_s; for (int i = 0; i <= remain - 1; i++) sum += num[i]; remain = (N - 1) / 2 - must_g + 1; sum += (long long)remain * (long long)x; return sum <= s; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int T; cin >> T; while (T--) { cin >> N >> s; for (int i = 1; i <= N; i++) cin >> l[i] >> r[i]; int lll = 1, rrr = 1e9, ans = 0; while (lll <= rrr) { int mid = (lll + rrr) >> 1; if (check(mid)) { lll = mid + 1; ans = max(ans, mid); } else rrr = mid - 1; } cout << ans << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e6; int n, vis[N + 2]; long long s; vector<pair<int, int> > v; bool ok(int md) { for (int i = 0; i < n; i++) vis[i] = 0; int cnt = (n + 1) / 2; long long ss = s; for (int i = n - 1; i >= 0; i--) { if (v[i].second >= md && cnt) vis[i] = 1, cnt--, ss -= max(md, v[i].first); } if (cnt || ss < 0) return false; for (int i = 0; i < n; i++) { if (!vis[i]) ss -= v[i].first; if (ss < 0) return false; } return true; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { cin >> n >> s; v.resize(n); for (int i = 0; i < n; i++) cin >> v[i].first >> v[i].second; sort(v.begin(), v.end()); int lo = v[n / 2].first, hi = 1e9, md; while (hi - lo > 2) { md = (lo + hi) / 2; if (ok(md)) lo = md; else hi = md; } for (md = hi; md >= lo; md--) if (ok(md)) break; cout << md << "\n"; } return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e6; int n, vis[N + 2]; long long s; vector<pair<int, int> > v; bool ok(int md) { for (int i = 0; i < n; i++) vis[i] = 0; int cnt = (n + 1) / 2; long long ss = s; for (int i = n - 1; i >= 0; i--) { if (v[i].second >= md && cnt) vis[i] = 1, cnt--, ss -= max(md, v[i].first); } if (cnt || ss < 0) return false; for (int i = 0; i < n; i++) { if (!vis[i]) ss -= v[i].first; if (ss < 0) return false; } return true; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { cin >> n >> s; v.resize(n); for (int i = 0; i < n; i++) cin >> v[i].first >> v[i].second; sort(v.begin(), v.end()); int lo = v[n / 2].first, hi = 1e9, md; while (hi - lo > 2) { md = (lo + hi) / 2; if (ok(md)) lo = md; else hi = md; } for (md = hi; md >= lo; md--) if (ok(md)) break; cout << md << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; #pragma GCC optimization("Ofast") #pragma GCC optimization("unroll-loops") #pragma GCC target("avx2,avx,fma") int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int i, j, t; cin >> t; for (i = 0; i < t; i++) { int n; long long int s; cin >> n >> s; vector<pair<long long int, long long int> > v; long long int l, r; for (j = 0; j < n; j++) { cin >> l >> r; v.push_back(make_pair(l, r)); } sort(v.rbegin(), v.rend()); l = 1; r = s; while (l <= r) { long long int mid = (l + r) / 2; long long int sum = 0; int k = 0; for (j = 0; j < n; j++) { if (v[j].second >= mid && k < n / 2 + 1) { sum += max(v[j].first, mid); k++; } else sum += v[j].first; } if (k < (n / 2 + 1) || sum > s) r = mid - 1; else l = mid + 1; } cout << r << "\n"; } return 0; }
### Prompt Create a solution in Cpp for the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; #pragma GCC optimization("Ofast") #pragma GCC optimization("unroll-loops") #pragma GCC target("avx2,avx,fma") int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int i, j, t; cin >> t; for (i = 0; i < t; i++) { int n; long long int s; cin >> n >> s; vector<pair<long long int, long long int> > v; long long int l, r; for (j = 0; j < n; j++) { cin >> l >> r; v.push_back(make_pair(l, r)); } sort(v.rbegin(), v.rend()); l = 1; r = s; while (l <= r) { long long int mid = (l + r) / 2; long long int sum = 0; int k = 0; for (j = 0; j < n; j++) { if (v[j].second >= mid && k < n / 2 + 1) { sum += max(v[j].first, mid); k++; } else sum += v[j].first; } if (k < (n / 2 + 1) || sum > s) r = mid - 1; else l = mid + 1; } cout << r << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 69; const int64_t INF = 100000000000000018LL; long long p[N], n, szz[N], m, k; long long check(vector<pair<long long, long long> >& v, long long m, long long s) { long long i, l_b = (long long)v.size() / 2, r_b = 1 + (long long)v.size() / 2; vector<long long> visited((long long)v.size(), 0); long long f = 0, sum = 0, cnt = 0, y; for (i = 0; i < (long long)v.size(); i++) { if (v[i].first >= m) { visited[i] = 1; sum += v[i].first; cnt++; if (v[i].first == m) f = 1; } } if (sum > s) return 0; if (cnt >= r_b && f == 0) return 1; vector<pair<long long, long long> > v1; for (i = 0; i < (long long)v.size(); i++) { if (!visited[i]) v1.push_back({v[i].first, v[i].second}); } sort((v1).begin(), (v1).end()); if ((long long)v1.size() == 0) { if (sum <= s && f == 1) return 1; return 0; } vector<long long> vis((long long)v1.size(), 0); if (cnt < r_b) { y = r_b - cnt; for (i = (long long)v1.size() - 1; i >= 0 && y; i--) { if (v1[i].second >= m) { sum += m; f = 1; y--; vis[i] = 1; } } if (y) return 0; } long long yoyo = INT_MIN; for (i = 0; i < (long long)v1.size(); i++) { if (!vis[i]) { if (v1[i].first == m) f = 1; yoyo = max(yoyo, v1[i].first); sum += v1[i].first; } } if (f == 0 && yoyo != INT_MIN) { sum = sum - yoyo + m; f = 1; } if (sum <= s && f == 1) return 1; return 0; } void solve() { long long n, s, i, l_min = INT_MAX, r_mx = INT_MIN; cin >> n >> s; vector<pair<long long, long long> > v; for (i = 0; i < n; i++) { long long l, r; cin >> l >> r; v.push_back({l, r}); l_min = min(l_min, l); r_mx = max(r_mx, r); } sort((v).begin(), (v).end()); long long l = l_min, r = r_mx, mid, ans; while (l <= r) { mid = l + (r - l) / 2; if (check(v, mid, s)) { l = mid + 1; ans = mid; } else r = mid - 1; } cout << ans << "\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1; cin >> t; while (t--) { solve(); } }
### Prompt Your task is to create a Cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 2e5 + 69; const int64_t INF = 100000000000000018LL; long long p[N], n, szz[N], m, k; long long check(vector<pair<long long, long long> >& v, long long m, long long s) { long long i, l_b = (long long)v.size() / 2, r_b = 1 + (long long)v.size() / 2; vector<long long> visited((long long)v.size(), 0); long long f = 0, sum = 0, cnt = 0, y; for (i = 0; i < (long long)v.size(); i++) { if (v[i].first >= m) { visited[i] = 1; sum += v[i].first; cnt++; if (v[i].first == m) f = 1; } } if (sum > s) return 0; if (cnt >= r_b && f == 0) return 1; vector<pair<long long, long long> > v1; for (i = 0; i < (long long)v.size(); i++) { if (!visited[i]) v1.push_back({v[i].first, v[i].second}); } sort((v1).begin(), (v1).end()); if ((long long)v1.size() == 0) { if (sum <= s && f == 1) return 1; return 0; } vector<long long> vis((long long)v1.size(), 0); if (cnt < r_b) { y = r_b - cnt; for (i = (long long)v1.size() - 1; i >= 0 && y; i--) { if (v1[i].second >= m) { sum += m; f = 1; y--; vis[i] = 1; } } if (y) return 0; } long long yoyo = INT_MIN; for (i = 0; i < (long long)v1.size(); i++) { if (!vis[i]) { if (v1[i].first == m) f = 1; yoyo = max(yoyo, v1[i].first); sum += v1[i].first; } } if (f == 0 && yoyo != INT_MIN) { sum = sum - yoyo + m; f = 1; } if (sum <= s && f == 1) return 1; return 0; } void solve() { long long n, s, i, l_min = INT_MAX, r_mx = INT_MIN; cin >> n >> s; vector<pair<long long, long long> > v; for (i = 0; i < n; i++) { long long l, r; cin >> l >> r; v.push_back({l, r}); l_min = min(l_min, l); r_mx = max(r_mx, r); } sort((v).begin(), (v).end()); long long l = l_min, r = r_mx, mid, ans; while (l <= r) { mid = l + (r - l) / 2; if (check(v, mid, s)) { l = mid + 1; ans = mid; } else r = mid - 1; } cout << ans << "\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1; cin >> t; while (t--) { solve(); } } ```
#include <bits/stdc++.h> using namespace std; struct Data { long long l, r; } a[200100]; long long n, s, rmax, lmin; void Docfile() { scanf("%lld%lld", &n, &s); rmax = 0; lmin = 1e18; for (int i = 1; i <= n; i++) { scanf("%lld%lld", &a[i].l, &a[i].r); rmax = max(rmax, a[i].r); lmin = min(lmin, a[i].l); } } bool cmp(Data &a, Data &b) { return ((a.l < b.l) || ((a.l == b.l) && (a.r < b.r))); } void Xuli() { sort(a + 1, a + 1 + n, cmp); long long sum1, sum2, dem1, dem2, dem3, F[200100]; long long d = lmin, c = rmax, g, kq = 0; while (d <= c) { g = (d + c) / 2; dem1 = dem2 = dem3 = sum1 = sum2 = 0; F[0] = 0; for (int i = 1; i <= n; i++) { if (a[i].r < g) { dem1++; sum1 += a[i].l; } else { if (a[i].l > g) { dem2++; sum2 += a[i].l; } else { dem3++; F[dem3] = F[dem3 - 1] + a[i].l; } } } if (dem1 > n / 2) { c = g - 1; continue; } if (dem2 > n / 2) { d = g + 1; continue; } if (dem1 < n / 2) sum1 += F[n / 2 - dem1]; if (dem2 < n / 2) sum2 += g * (n / 2 - dem2); if (sum1 + sum2 + g <= s) { kq = max(kq, g); d = g + 1; } else c = g - 1; } printf("%lld\n", kq); } int main() { long t; scanf("%d", &t); for (int i = 1; i <= t; i++) { Docfile(); Xuli(); } }
### Prompt Your challenge is to write a Cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct Data { long long l, r; } a[200100]; long long n, s, rmax, lmin; void Docfile() { scanf("%lld%lld", &n, &s); rmax = 0; lmin = 1e18; for (int i = 1; i <= n; i++) { scanf("%lld%lld", &a[i].l, &a[i].r); rmax = max(rmax, a[i].r); lmin = min(lmin, a[i].l); } } bool cmp(Data &a, Data &b) { return ((a.l < b.l) || ((a.l == b.l) && (a.r < b.r))); } void Xuli() { sort(a + 1, a + 1 + n, cmp); long long sum1, sum2, dem1, dem2, dem3, F[200100]; long long d = lmin, c = rmax, g, kq = 0; while (d <= c) { g = (d + c) / 2; dem1 = dem2 = dem3 = sum1 = sum2 = 0; F[0] = 0; for (int i = 1; i <= n; i++) { if (a[i].r < g) { dem1++; sum1 += a[i].l; } else { if (a[i].l > g) { dem2++; sum2 += a[i].l; } else { dem3++; F[dem3] = F[dem3 - 1] + a[i].l; } } } if (dem1 > n / 2) { c = g - 1; continue; } if (dem2 > n / 2) { d = g + 1; continue; } if (dem1 < n / 2) sum1 += F[n / 2 - dem1]; if (dem2 < n / 2) sum2 += g * (n / 2 - dem2); if (sum1 + sum2 + g <= s) { kq = max(kq, g); d = g + 1; } else c = g - 1; } printf("%lld\n", kq); } int main() { long t; scanf("%d", &t); for (int i = 1; i <= t; i++) { Docfile(); Xuli(); } } ```
#include <bits/stdc++.h> long long n, s; std::vector<std::pair<long long, long long>> employees(200000, std::make_pair(-1, -1)); bool baz(long long mid) { long long g_cnt = 0; std::vector<long long> limbo; long long sal = 0; for (long long i = 0; i < n; ++i) { if (employees[i].second < mid) sal += employees[i].first; else if (employees[i].first >= mid) { ++g_cnt; sal += employees[i].first; } else limbo.push_back(i); } long long req = ((n + 1) / 2) - g_cnt; if (req > (long long)limbo.size()) return false; req = std::max((long long)0, req); long long rem = (limbo.size() - req); for (long long i = 0; i < rem; ++i) sal += employees[limbo[i]].first; for (long long i = 0; i < req; ++i) sal += mid; if (sal <= s) return true; return false; } void foo() { scanf("%lld %lld", &n, &s); for (long long i = 0; i < n; ++i) scanf("%lld %lld", &employees[i].first, &employees[i].second); std::sort(employees.begin(), employees.begin() + n); long long left = 1, right = 200000000001111; long long ans = 1; while ((right - left) > 1) { long long mid = (right + left) / 2; if (baz(mid)) { ans = mid; left = mid; } else { right = mid - 1; } } if (left > ans && baz(left)) ans = left; if (right > ans && baz(right)) ans = right; printf("%lld\n", ans); } int main(void) { long long t; scanf("%lld", &t); while (t--) foo(); return 0; }
### Prompt Your task is to create a cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> long long n, s; std::vector<std::pair<long long, long long>> employees(200000, std::make_pair(-1, -1)); bool baz(long long mid) { long long g_cnt = 0; std::vector<long long> limbo; long long sal = 0; for (long long i = 0; i < n; ++i) { if (employees[i].second < mid) sal += employees[i].first; else if (employees[i].first >= mid) { ++g_cnt; sal += employees[i].first; } else limbo.push_back(i); } long long req = ((n + 1) / 2) - g_cnt; if (req > (long long)limbo.size()) return false; req = std::max((long long)0, req); long long rem = (limbo.size() - req); for (long long i = 0; i < rem; ++i) sal += employees[limbo[i]].first; for (long long i = 0; i < req; ++i) sal += mid; if (sal <= s) return true; return false; } void foo() { scanf("%lld %lld", &n, &s); for (long long i = 0; i < n; ++i) scanf("%lld %lld", &employees[i].first, &employees[i].second); std::sort(employees.begin(), employees.begin() + n); long long left = 1, right = 200000000001111; long long ans = 1; while ((right - left) > 1) { long long mid = (right + left) / 2; if (baz(mid)) { ans = mid; left = mid; } else { right = mid - 1; } } if (left > ans && baz(left)) ans = left; if (right > ans && baz(right)) ans = right; printf("%lld\n", ans); } int main(void) { long long t; scanf("%lld", &t); while (t--) foo(); return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long MOD = (long long)(1e9 + 7); const long long N = 200007; vector<pair<long long, long long>> v; long long s; int n; int sure(long long median) { long long cnt1 = 0; long long cnt2 = 0; long long cnt3 = 0; long long sum = 0; vector<long long> tmp; for (auto ele : v) { long long l = ele.first; long long r = ele.second; if (r < median) { cnt1 += 1; sum += l; } else if (l > median) { cnt3 += 1; sum += l; } else { cnt2 += 1; tmp.push_back(l); } } long long mn = (n - 1) / 2; if (cnt1 > mn) { return 0; } else if (cnt3 > mn) { return 1; } int idx = 0; while (cnt1 < mn) { cnt1 += 1; sum += tmp[idx]; idx += 1; } for (int i = idx; i < tmp.size(); i++) { sum += median; } if (sum > s) { return 0; } else { return 1; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; int tt; cin >> tt; for (int t = 1; t <= tt; t++) { v.clear(); cin >> n >> s; for (int i = 0; i < n; i++) { long long x, y; cin >> x >> y; v.push_back({x, y}); } sort(v.begin(), v.end()); long long l = 1; long long r = MOD; long long ans = 0; while (l <= r) { long long mid = (l + r) / 2; if (sure(mid) == 1) { ans = mid; l = mid + 1; } else { r = mid - 1; } } cout << ans << "\n"; } return 0; }
### Prompt In CPP, your task is to solve the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long MOD = (long long)(1e9 + 7); const long long N = 200007; vector<pair<long long, long long>> v; long long s; int n; int sure(long long median) { long long cnt1 = 0; long long cnt2 = 0; long long cnt3 = 0; long long sum = 0; vector<long long> tmp; for (auto ele : v) { long long l = ele.first; long long r = ele.second; if (r < median) { cnt1 += 1; sum += l; } else if (l > median) { cnt3 += 1; sum += l; } else { cnt2 += 1; tmp.push_back(l); } } long long mn = (n - 1) / 2; if (cnt1 > mn) { return 0; } else if (cnt3 > mn) { return 1; } int idx = 0; while (cnt1 < mn) { cnt1 += 1; sum += tmp[idx]; idx += 1; } for (int i = idx; i < tmp.size(); i++) { sum += median; } if (sum > s) { return 0; } else { return 1; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; int tt; cin >> tt; for (int t = 1; t <= tt; t++) { v.clear(); cin >> n >> s; for (int i = 0; i < n; i++) { long long x, y; cin >> x >> y; v.push_back({x, y}); } sort(v.begin(), v.end()); long long l = 1; long long r = MOD; long long ans = 0; while (l <= r) { long long mid = (l + r) / 2; if (sure(mid) == 1) { ans = mid; l = mid + 1; } else { r = mid - 1; } } cout << ans << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int t, n; long long s, sum; pair<int, int> opt[maxn]; bool check(int mid) { int k = 0; long long total = sum; for (int i = n; i >= 1; --i) { if (opt[i].second >= mid) { k++; if (mid >= opt[i].first) total += mid - opt[i].first; if (k == (n + 1) / 2) break; } } return k == (n + 1) / 2 && total <= s; } int main() { scanf("%d", &t); while (t--) { sum = 0; scanf("%d%lld", &n, &s); int L, R; for (int i = 1; i <= n; ++i) { int l, r; scanf("%d%d", &l, &r); opt[i] = {l, r}; L = min(L, l); R = max(R, r); sum += l; } sort(opt + 1, opt + 1 + n); while (L < R) { int mid = (L + R + 1) >> 1; if (check(mid)) L = mid; else R = mid - 1; } printf("%d\n", L); } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int t, n; long long s, sum; pair<int, int> opt[maxn]; bool check(int mid) { int k = 0; long long total = sum; for (int i = n; i >= 1; --i) { if (opt[i].second >= mid) { k++; if (mid >= opt[i].first) total += mid - opt[i].first; if (k == (n + 1) / 2) break; } } return k == (n + 1) / 2 && total <= s; } int main() { scanf("%d", &t); while (t--) { sum = 0; scanf("%d%lld", &n, &s); int L, R; for (int i = 1; i <= n; ++i) { int l, r; scanf("%d%d", &l, &r); opt[i] = {l, r}; L = min(L, l); R = max(R, r); sum += l; } sort(opt + 1, opt + 1 + n); while (L < R) { int mid = (L + R + 1) >> 1; if (check(mid)) L = mid; else R = mid - 1; } printf("%d\n", L); } return 0; } ```
#include <bits/stdc++.h> using namespace std; vector<pair<long long, long long>> a; long long s, n; bool check(long long mid) { long long cnt = 0; vector<long long> b; long long sum = 0; for (long long i = 0; i < n; i++) { if (a[i].second < mid) sum += a[i].first; else if (a[i].first >= mid) { cnt++; sum += a[i].first; } else { b.push_back(a[i].first); } } long long cur = max(0LL, (n + 1) / 2 - cnt); if (cur > b.size()) { return false; } for (long long i = 0; i < b.size(); i++) { if (b.size() - i <= cur) { sum += mid; } else sum += b[i]; } return sum <= s; } void solve() { cin >> n >> s; a.resize(n); for (long long i = 0; i < n; i++) { cin >> a[i].first >> a[i].second; } sort(a.begin(), a.end()); long long l = 0, r = 2e9; while (r - l > 1) { long long mid = (l + r) >> 1; if (check(mid)) l = mid; else r = mid; } cout << l << "\n"; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout << fixed; cout.precision(30); long long t = 1; cin >> t; while (t--) solve(); }
### Prompt Your task is to create a CPP solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<pair<long long, long long>> a; long long s, n; bool check(long long mid) { long long cnt = 0; vector<long long> b; long long sum = 0; for (long long i = 0; i < n; i++) { if (a[i].second < mid) sum += a[i].first; else if (a[i].first >= mid) { cnt++; sum += a[i].first; } else { b.push_back(a[i].first); } } long long cur = max(0LL, (n + 1) / 2 - cnt); if (cur > b.size()) { return false; } for (long long i = 0; i < b.size(); i++) { if (b.size() - i <= cur) { sum += mid; } else sum += b[i]; } return sum <= s; } void solve() { cin >> n >> s; a.resize(n); for (long long i = 0; i < n; i++) { cin >> a[i].first >> a[i].second; } sort(a.begin(), a.end()); long long l = 0, r = 2e9; while (r - l > 1) { long long mid = (l + r) >> 1; if (check(mid)) l = mid; else r = mid; } cout << l << "\n"; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout << fixed; cout.precision(30); long long t = 1; cin >> t; while (t--) solve(); } ```
#include <bits/stdc++.h> using namespace std; struct node { long long l, r; } a[200010]; long long n, s; inline long long v_in() { char ch = getchar(); long long sum = 0, f = 1; while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) sum = (sum << 3) + (sum << 1) + (ch ^ 48), ch = getchar(); return sum * f; } inline bool cmp(const node &a, const node &b) { return a.l < b.l; } bool judge(long long x) { long long cnt = 0, k = s; for (register long long i = n; i >= 1; i--) { if (a[i].l >= x) { cnt++; if (cnt >= n / 2 + 1) return true; continue; } if (a[i].r >= x) { if (k >= x - a[i].l) { k -= x - a[i].l; cnt++; if (cnt >= n / 2 + 1) return true; } } } return false; } int main() { long long q = v_in(); while (q--) { n = v_in(), s = v_in(); long long lmin = 2000000000, rmax = 0; for (register long long i = 1; i <= n; i++) { a[i].l = v_in(); s -= a[i].l; a[i].r = v_in(); lmin = ((lmin) < (a[i].l) ? (lmin) : (a[i].l)); rmax = ((rmax) > (a[i].r) ? (rmax) : (a[i].r)); } sort(a + 1, a + n + 1, cmp); long long ans = 0, l = lmin, r = rmax; while (l <= r) { long long mid = (l + r) >> 1; if (judge(mid)) { ans = mid; l = mid + 1; } else r = mid - 1; } printf("%lld\n", ans); } return 0; }
### Prompt Your task is to create a cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct node { long long l, r; } a[200010]; long long n, s; inline long long v_in() { char ch = getchar(); long long sum = 0, f = 1; while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) sum = (sum << 3) + (sum << 1) + (ch ^ 48), ch = getchar(); return sum * f; } inline bool cmp(const node &a, const node &b) { return a.l < b.l; } bool judge(long long x) { long long cnt = 0, k = s; for (register long long i = n; i >= 1; i--) { if (a[i].l >= x) { cnt++; if (cnt >= n / 2 + 1) return true; continue; } if (a[i].r >= x) { if (k >= x - a[i].l) { k -= x - a[i].l; cnt++; if (cnt >= n / 2 + 1) return true; } } } return false; } int main() { long long q = v_in(); while (q--) { n = v_in(), s = v_in(); long long lmin = 2000000000, rmax = 0; for (register long long i = 1; i <= n; i++) { a[i].l = v_in(); s -= a[i].l; a[i].r = v_in(); lmin = ((lmin) < (a[i].l) ? (lmin) : (a[i].l)); rmax = ((rmax) > (a[i].r) ? (rmax) : (a[i].r)); } sort(a + 1, a + n + 1, cmp); long long ans = 0, l = lmin, r = rmax; while (l <= r) { long long mid = (l + r) >> 1; if (judge(mid)) { ans = mid; l = mid + 1; } else r = mid - 1; } printf("%lld\n", ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int T; cin >> T; while (T--) { int N; long long S; cin >> N >> S; long long R[N]; pair<long long, long long> LR[N]; for (int i = 0; i < N; ++i) { long long l, r; cin >> l >> r; R[i] = r; LR[i] = {l, r}; } sort(R, R + N); sort(LR, LR + N); long long result = 0; for (long long b = R[N / 2]; b >= 1; b /= 2) { while (result + b <= R[N / 2]) { long long spent = 0; int over = 0; for (int i = N - 1; i >= 0; --i) { long long l, r; tie(l, r) = LR[i]; if (over < (N + 1) / 2 && result + b <= r) { ++over; spent += max(l, result + b); } else { spent += l; } } if (spent > S) break; result += b; } } cout << result << '\n'; } }
### Prompt Please formulate a CPP solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int T; cin >> T; while (T--) { int N; long long S; cin >> N >> S; long long R[N]; pair<long long, long long> LR[N]; for (int i = 0; i < N; ++i) { long long l, r; cin >> l >> r; R[i] = r; LR[i] = {l, r}; } sort(R, R + N); sort(LR, LR + N); long long result = 0; for (long long b = R[N / 2]; b >= 1; b /= 2) { while (result + b <= R[N / 2]) { long long spent = 0; int over = 0; for (int i = N - 1; i >= 0; --i) { long long l, r; tie(l, r) = LR[i]; if (over < (N + 1) / 2 && result + b <= r) { ++over; spent += max(l, result + b); } else { spent += l; } } if (spent > S) break; result += b; } } cout << result << '\n'; } } ```
#include <bits/stdc++.h> using namespace std; long long n, s, l[200005]; pair<long long, long long> arr[200005]; bool valid(long long x) { long long cnt = 0; long long sum = 0, z; vector<long long> v; for (int i = 1; i <= n; i++) { if (arr[i].second < x) { sum += arr[i].first; } else if (arr[i].first >= x) { cnt++; sum += arr[i].first; } else { v.push_back(arr[i].first); } } int butuh = (n + 1) / 2; butuh -= cnt; butuh = max(0, butuh); if (butuh > v.size()) return false; int sz = v.size(); for (int i = 0; i < sz - butuh; i++) { sum += v[i]; } sum += (x * butuh); return sum <= s; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t; cin >> t; while (t--) { cin >> n >> s; for (long long i = 1; i <= n; i++) { cin >> arr[i].first >> arr[i].second; } sort(arr + 1, arr + n + 1); long long ki = 1, ka = 1e9; long long pos = ki; while (ki <= ka) { long long mid = (ki + ka) / 2; if (valid(mid)) { pos = mid; ki = mid + 1; } else ka = mid - 1; } cout << pos << endl; } }
### Prompt Your challenge is to write a cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, s, l[200005]; pair<long long, long long> arr[200005]; bool valid(long long x) { long long cnt = 0; long long sum = 0, z; vector<long long> v; for (int i = 1; i <= n; i++) { if (arr[i].second < x) { sum += arr[i].first; } else if (arr[i].first >= x) { cnt++; sum += arr[i].first; } else { v.push_back(arr[i].first); } } int butuh = (n + 1) / 2; butuh -= cnt; butuh = max(0, butuh); if (butuh > v.size()) return false; int sz = v.size(); for (int i = 0; i < sz - butuh; i++) { sum += v[i]; } sum += (x * butuh); return sum <= s; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t; cin >> t; while (t--) { cin >> n >> s; for (long long i = 1; i <= n; i++) { cin >> arr[i].first >> arr[i].second; } sort(arr + 1, arr + n + 1); long long ki = 1, ka = 1e9; long long pos = ki; while (ki <= ka) { long long mid = (ki + ka) / 2; if (valid(mid)) { pos = mid; ki = mid + 1; } else ka = mid - 1; } cout << pos << endl; } } ```
#include <bits/stdc++.h> int II() { int n; scanf("%d", &n); return n; } long long int LL() { long long int n; scanf("%lld", &n); return n; } void IIn(int n) { printf("%d\n", n); } char* SS(char* str) { scanf("%s", str); return str + strlen(str); }; void SSn(char* str) { printf("%s\n", str); } constexpr int MAXN = 200000; typedef std::pair<int, int> ii; ii V[MAXN]; int main() { for (int T = II(); T--;) { int N = II(); long long int S = LL(); for (int i = 0; i < N; i++) V[i] = ii{II(), II()}; std::sort(V, V + N, std::greater<ii>()); int M = std::max_element(V, V + N, [](const ii& a, const ii& b) { return a.second < b.second; })->second; int bit = 1; while (bit <= M) bit <<= 1; bit >>= 1; int ans = 0; S -= std::accumulate(V, V + N, 0LL, [](long long int s, const ii& x) { return s + x.first; }); while (bit) { int m = ans | bit; { int to_sat = (N + 1) >> 1; long long int budget = S; for (int i = 0; i < N; i++) if (m <= V[i].second) { if (budget < m - V[i].first) break; if (V[i].first < m) budget -= m - V[i].first; --to_sat; if (to_sat == 0) break; } if (to_sat == 0) ans |= bit; } bit >>= 1; } IIn(ans); } }
### Prompt In cpp, your task is to solve the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> int II() { int n; scanf("%d", &n); return n; } long long int LL() { long long int n; scanf("%lld", &n); return n; } void IIn(int n) { printf("%d\n", n); } char* SS(char* str) { scanf("%s", str); return str + strlen(str); }; void SSn(char* str) { printf("%s\n", str); } constexpr int MAXN = 200000; typedef std::pair<int, int> ii; ii V[MAXN]; int main() { for (int T = II(); T--;) { int N = II(); long long int S = LL(); for (int i = 0; i < N; i++) V[i] = ii{II(), II()}; std::sort(V, V + N, std::greater<ii>()); int M = std::max_element(V, V + N, [](const ii& a, const ii& b) { return a.second < b.second; })->second; int bit = 1; while (bit <= M) bit <<= 1; bit >>= 1; int ans = 0; S -= std::accumulate(V, V + N, 0LL, [](long long int s, const ii& x) { return s + x.first; }); while (bit) { int m = ans | bit; { int to_sat = (N + 1) >> 1; long long int budget = S; for (int i = 0; i < N; i++) if (m <= V[i].second) { if (budget < m - V[i].first) break; if (V[i].first < m) budget -= m - V[i].first; --to_sat; if (to_sat == 0) break; } if (to_sat == 0) ans |= bit; } bit >>= 1; } IIn(ans); } } ```
#include <bits/stdc++.h> template <typename T> T GCD(T a, T b) { return a ? GCD(b % a, a) : b; } template <typename T> T LCM(T a, T b) { return (a * b) / GCD(a, b); } template <typename T> std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) { for (auto ob : v) os << ob << " "; return os; } template <typename T, typename S> std::ostream &operator<<(std::ostream &os, const std::map<T, S> &v) { for (auto ob : v) os << ob.first << " : " << ob.second << std::endl; return os; } using ld = double; using ll = long long int; using ul = unsigned long long int; using namespace std; class DSalaryChanging { bool Possible(vector<pair<int, int>> money, ll s, int mid, int n) { ll sum = 0; int cnt = 0; vector<int> v; for (int i = 0; i < n; ++i) { if (money[i].second < mid) { sum += money[i].first; } else if (money[i].first >= mid) { cnt++; sum += money[i].first; } else { v.push_back(money[i].first); } } sort(v.begin(), v.end()); int need = max(0, ((n + 1) / 2) - cnt); if (need > v.size()) return false; else { for (int i = v.size() - 1; i >= 0; --i) { if (need) { need--; sum += mid; } else sum += v[i]; } return sum <= s; } } public: void solve(std::istream &in, std::ostream &out) { ll n, s; in >> n >> s; vector<pair<int, int>> money; for (int i = 0; i < n; ++i) { int l, r; in >> l >> r; money.push_back(make_pair(l, r)); } sort(money.begin(), money.end()); int low = 1, high = 1e9 + 10, ans = 0; while (low + 1 < high) { int mid = (low + high) / 2; bool isPossible = Possible(money, s, mid, n); if (isPossible) { ans = mid; low = mid; } else { high = mid; } } out << ans << endl; } }; int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(nullptr); DSalaryChanging solver; std::istream &in(std::cin); std::ostream &out(std::cout); int n; in >> n; for (int i = 0; i < n; ++i) { solver.solve(in, out); } return 0; }
### Prompt In CPP, your task is to solve the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> template <typename T> T GCD(T a, T b) { return a ? GCD(b % a, a) : b; } template <typename T> T LCM(T a, T b) { return (a * b) / GCD(a, b); } template <typename T> std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) { for (auto ob : v) os << ob << " "; return os; } template <typename T, typename S> std::ostream &operator<<(std::ostream &os, const std::map<T, S> &v) { for (auto ob : v) os << ob.first << " : " << ob.second << std::endl; return os; } using ld = double; using ll = long long int; using ul = unsigned long long int; using namespace std; class DSalaryChanging { bool Possible(vector<pair<int, int>> money, ll s, int mid, int n) { ll sum = 0; int cnt = 0; vector<int> v; for (int i = 0; i < n; ++i) { if (money[i].second < mid) { sum += money[i].first; } else if (money[i].first >= mid) { cnt++; sum += money[i].first; } else { v.push_back(money[i].first); } } sort(v.begin(), v.end()); int need = max(0, ((n + 1) / 2) - cnt); if (need > v.size()) return false; else { for (int i = v.size() - 1; i >= 0; --i) { if (need) { need--; sum += mid; } else sum += v[i]; } return sum <= s; } } public: void solve(std::istream &in, std::ostream &out) { ll n, s; in >> n >> s; vector<pair<int, int>> money; for (int i = 0; i < n; ++i) { int l, r; in >> l >> r; money.push_back(make_pair(l, r)); } sort(money.begin(), money.end()); int low = 1, high = 1e9 + 10, ans = 0; while (low + 1 < high) { int mid = (low + high) / 2; bool isPossible = Possible(money, s, mid, n); if (isPossible) { ans = mid; low = mid; } else { high = mid; } } out << ans << endl; } }; int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(nullptr); DSalaryChanging solver; std::istream &in(std::cin); std::ostream &out(std::cout); int n; in >> n; for (int i = 0; i < n; ++i) { solver.solve(in, out); } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long int max(long long int a, long long int b) { if (a > b) return a; else return b; } long long int min(long long int a, long long int b) { if (a < b) return a; else return b; } const int dx[4] = {-1, 1, 0, 0}; const int dy[4] = {0, 0, -1, 1}; int XX[] = {-1, -1, -1, 0, 0, 1, 1, 1}; int YY[] = {-1, 0, 1, -1, 1, -1, 0, 1}; bool compare(pair<long long int, long long int>& a, pair<long long int, long long int>& b) { if (a.first != b.first) return a.first < b.first; return a.second < b.second; } vector<pair<long long int, long long int> > vec; long long int ans, n, s; void func(long long int low, long long int high) { long long int z = vec.size(); z = z / 2; long long int i, j; while (low <= high) { long long int mid = (low + high) >> 1; set<long long int> s1, s2; for (i = 0; i < n; i++) { if (vec[i].second >= mid) { s2.insert(i); } } long long int count = z, sum = 0, flag = 0; for (i = 0; i < n; i++) { if (count <= 0) break; if (vec[i].first <= mid) { if (s2.find(i) != s2.end()) { if (s2.size() <= z + 1) continue; s2.erase(i); } sum += vec[i].first; count--; } } if (s2.size() >= z + 1) { vector<long long int> vec3; for (auto itr : s2) { long long int mini = max(mid, vec[itr].first); vec3.push_back(mini); } sort(vec3.begin(), vec3.end()); for (i = 0; i < z + 1; i++) sum += vec3[i]; } if (count <= 0 && s2.size() >= (z + 1) && sum <= s) { ans = max(ans, mid); low = mid + 1; } else { high = mid - 1; } } } int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); long long int i, j, k, x, y, t, e, d, m, z; long long int val = 0; cin >> t; while (t--) { val++; cin >> n >> s; vec.clear(); for (i = 0; i < n; i++) { cin >> x >> y; vec.push_back(make_pair(x, y)); } sort(vec.begin(), vec.end(), compare); ans = -9000000000000000000; long long int maxi = -9000000000000000000; for (i = 0; i <= n / 2; i++) { maxi = max(maxi, vec[i].second); } func(vec[n / 2].first, maxi); cout << ans << "\n"; } }
### Prompt Develop a solution in Cpp to the problem described below: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long int max(long long int a, long long int b) { if (a > b) return a; else return b; } long long int min(long long int a, long long int b) { if (a < b) return a; else return b; } const int dx[4] = {-1, 1, 0, 0}; const int dy[4] = {0, 0, -1, 1}; int XX[] = {-1, -1, -1, 0, 0, 1, 1, 1}; int YY[] = {-1, 0, 1, -1, 1, -1, 0, 1}; bool compare(pair<long long int, long long int>& a, pair<long long int, long long int>& b) { if (a.first != b.first) return a.first < b.first; return a.second < b.second; } vector<pair<long long int, long long int> > vec; long long int ans, n, s; void func(long long int low, long long int high) { long long int z = vec.size(); z = z / 2; long long int i, j; while (low <= high) { long long int mid = (low + high) >> 1; set<long long int> s1, s2; for (i = 0; i < n; i++) { if (vec[i].second >= mid) { s2.insert(i); } } long long int count = z, sum = 0, flag = 0; for (i = 0; i < n; i++) { if (count <= 0) break; if (vec[i].first <= mid) { if (s2.find(i) != s2.end()) { if (s2.size() <= z + 1) continue; s2.erase(i); } sum += vec[i].first; count--; } } if (s2.size() >= z + 1) { vector<long long int> vec3; for (auto itr : s2) { long long int mini = max(mid, vec[itr].first); vec3.push_back(mini); } sort(vec3.begin(), vec3.end()); for (i = 0; i < z + 1; i++) sum += vec3[i]; } if (count <= 0 && s2.size() >= (z + 1) && sum <= s) { ans = max(ans, mid); low = mid + 1; } else { high = mid - 1; } } } int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); long long int i, j, k, x, y, t, e, d, m, z; long long int val = 0; cin >> t; while (t--) { val++; cin >> n >> s; vec.clear(); for (i = 0; i < n; i++) { cin >> x >> y; vec.push_back(make_pair(x, y)); } sort(vec.begin(), vec.end(), compare); ans = -9000000000000000000; long long int maxi = -9000000000000000000; for (i = 0; i <= n / 2; i++) { maxi = max(maxi, vec[i].second); } func(vec[n / 2].first, maxi); cout << ans << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; void itval(istream_iterator<string> it) {} template <typename T, typename... Args> void itval(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; itval(++it, args...); } const long long int MOD = 1e9 + 7; template <typename T> inline void print(T x) { cout << x << "\n"; } template <typename T> inline void printvec(T x) { for (auto a : x) cout << a << ' '; cout << '\n'; } struct custom { bool operator()(const pair<int, int> &p1, const pair<int, int> &p2) const { return p1.first + p2.second < p1.second + p2.first; } }; long long int get_pow(long long int a, long long int b, long long int M) { long long int res = 1; while (b) { if (b & 1) res = (res * a) % M; a = (a * a) % M; b >>= 1; } return res; } const long long int N = 1e5 + 5, inf = 2e18; void solve() { long long int n, s; cin >> n >> s; std::vector<pair<long long int, long long int> > v(n); for (long long int i = (long long int)0; i < (long long int)(n); i++) { cin >> v[i].first >> v[i].second; } sort(v.begin(), v.end()); long long int lo = 0, hi = 1e9 + 5; while (lo + 1 < hi) { long long int mid = (lo + hi) / 2; long long int sum = 0, m = n / 2, ok = 1, cnt = 0, c = 0; vector<bool> used(n, false); long long int l = 0, h = 0; for (long long int i = (long long int)0; i < (long long int)(n); i++) { if (v[i].second < mid) { sum += v[i].first; used[i] = 1; l++; } if (v[i].first > mid) { sum += v[i].first; used[i] = 1; h++; } } if (l > n / 2 || h > n / 2) ok = 0; if (l > n / 2) { hi = mid; continue; } if (h > n / 2) { lo = mid; continue; } if (ok) { for (long long int i = (long long int)0; i < (long long int)(n); i++) { if (used[i]) continue; if (l < n / 2) { sum += v[i].first; l++; continue; } if (h < n / 2) { sum += mid; h++; continue; } } sum += mid; if (sum > s) ok = 0; } if (ok) lo = mid; else hi = mid; } cout << lo << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int test = 1; cin >> test; clock_t z = clock(); for (long long int tes = (long long int)0; tes < (long long int)(test); tes++) { solve(); } fprintf(stderr, "Total Time:%.4f\n", (double)(clock() - z) / CLOCKS_PER_SEC), fflush(stderr); return 0; }
### Prompt In cpp, your task is to solve the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void itval(istream_iterator<string> it) {} template <typename T, typename... Args> void itval(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; itval(++it, args...); } const long long int MOD = 1e9 + 7; template <typename T> inline void print(T x) { cout << x << "\n"; } template <typename T> inline void printvec(T x) { for (auto a : x) cout << a << ' '; cout << '\n'; } struct custom { bool operator()(const pair<int, int> &p1, const pair<int, int> &p2) const { return p1.first + p2.second < p1.second + p2.first; } }; long long int get_pow(long long int a, long long int b, long long int M) { long long int res = 1; while (b) { if (b & 1) res = (res * a) % M; a = (a * a) % M; b >>= 1; } return res; } const long long int N = 1e5 + 5, inf = 2e18; void solve() { long long int n, s; cin >> n >> s; std::vector<pair<long long int, long long int> > v(n); for (long long int i = (long long int)0; i < (long long int)(n); i++) { cin >> v[i].first >> v[i].second; } sort(v.begin(), v.end()); long long int lo = 0, hi = 1e9 + 5; while (lo + 1 < hi) { long long int mid = (lo + hi) / 2; long long int sum = 0, m = n / 2, ok = 1, cnt = 0, c = 0; vector<bool> used(n, false); long long int l = 0, h = 0; for (long long int i = (long long int)0; i < (long long int)(n); i++) { if (v[i].second < mid) { sum += v[i].first; used[i] = 1; l++; } if (v[i].first > mid) { sum += v[i].first; used[i] = 1; h++; } } if (l > n / 2 || h > n / 2) ok = 0; if (l > n / 2) { hi = mid; continue; } if (h > n / 2) { lo = mid; continue; } if (ok) { for (long long int i = (long long int)0; i < (long long int)(n); i++) { if (used[i]) continue; if (l < n / 2) { sum += v[i].first; l++; continue; } if (h < n / 2) { sum += mid; h++; continue; } } sum += mid; if (sum > s) ok = 0; } if (ok) lo = mid; else hi = mid; } cout << lo << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int test = 1; cin >> test; clock_t z = clock(); for (long long int tes = (long long int)0; tes < (long long int)(test); tes++) { solve(); } fprintf(stderr, "Total Time:%.4f\n", (double)(clock() - z) / CLOCKS_PER_SEC), fflush(stderr); return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long MAXN = 2e5 + 5; pair<long long, long long> a[MAXN]; long long s; long long n; bool check(long long x) { long long sum = 0, cnt = 0; vector<long long> nums; for (long long i = 0; i < n; i++) { if (a[i].first >= x) { sum += a[i].first; cnt++; } else { if (a[i].second < x) { sum += a[i].first; } else { nums.push_back(a[i].first); } } } long long left = max((long long)0, (n + 1) / 2 - cnt); if (left > nums.size()) return 0; for (long long i = 0; i < nums.size() - left; i++) { sum += nums[i]; } for (long long i = nums.size() - left; i < nums.size(); i++) { sum += x; } return (sum <= s); } int main() { long long q; cin >> q; while (q--) { cin >> n >> s; for (long long i = 0; i < n; i++) { cin >> a[i].first >> a[i].second; } sort(a, a + n); long long l = 1, r = 1e9 + 100; while (l + 1 < r) { long long m = (l + r) >> 1; if (check(m)) { l = m; } else { r = m; } } cout << l << "\n"; } return 0; }
### Prompt Please formulate a cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long MAXN = 2e5 + 5; pair<long long, long long> a[MAXN]; long long s; long long n; bool check(long long x) { long long sum = 0, cnt = 0; vector<long long> nums; for (long long i = 0; i < n; i++) { if (a[i].first >= x) { sum += a[i].first; cnt++; } else { if (a[i].second < x) { sum += a[i].first; } else { nums.push_back(a[i].first); } } } long long left = max((long long)0, (n + 1) / 2 - cnt); if (left > nums.size()) return 0; for (long long i = 0; i < nums.size() - left; i++) { sum += nums[i]; } for (long long i = nums.size() - left; i < nums.size(); i++) { sum += x; } return (sum <= s); } int main() { long long q; cin >> q; while (q--) { cin >> n >> s; for (long long i = 0; i < n; i++) { cin >> a[i].first >> a[i].second; } sort(a, a + n); long long l = 1, r = 1e9 + 100; while (l + 1 < r) { long long m = (l + r) >> 1; if (check(m)) { l = m; } else { r = m; } } cout << l << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int n; long long s; vector<pair<long long, long long>> v; bool feasible(long long mid) { long long sum = 0; int cnt = 0, need; vector<long long> ans; for (int i = 0; i < v.size(); i++) { if (v[i].second < mid) sum += v[i].first; else if (v[i].first > mid) { cnt++; sum += v[i].first; } else ans.push_back(v[i].first); } need = max(0, ((n + 1) / 2 - cnt)); if (need > ans.size()) return false; for (int i = ans.size() - 1; i >= 0; i--) { if (need != 0) { need--; sum += mid; } else sum += ans[i]; } return sum <= s; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { cin >> n >> s; long long x, y; v.clear(); for (int i = 0; i < n; i++) { cin >> x >> y; v.push_back({x, y}); } sort(v.begin(), v.end()); long long l = 0, h = s, mid, best = -1; while (l <= h) { mid = l + (h - l) / 2; if (feasible(mid)) { l = mid + 1; best = mid; } else { h = mid - 1; } } cout << best << "\n"; } return 0; }
### Prompt In cpp, your task is to solve the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n; long long s; vector<pair<long long, long long>> v; bool feasible(long long mid) { long long sum = 0; int cnt = 0, need; vector<long long> ans; for (int i = 0; i < v.size(); i++) { if (v[i].second < mid) sum += v[i].first; else if (v[i].first > mid) { cnt++; sum += v[i].first; } else ans.push_back(v[i].first); } need = max(0, ((n + 1) / 2 - cnt)); if (need > ans.size()) return false; for (int i = ans.size() - 1; i >= 0; i--) { if (need != 0) { need--; sum += mid; } else sum += ans[i]; } return sum <= s; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { cin >> n >> s; long long x, y; v.clear(); for (int i = 0; i < n; i++) { cin >> x >> y; v.push_back({x, y}); } sort(v.begin(), v.end()); long long l = 0, h = s, mid, best = -1; while (l <= h) { mid = l + (h - l) / 2; if (feasible(mid)) { l = mid + 1; best = mid; } else { h = mid - 1; } } cout << best << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int t, n; long long s; struct node { long long l, r; } nod[200005]; vector<long long> ve; int main() { scanf("%d", &t); while (t--) { scanf("%d%lld", &n, &s); for (int i = 1; i <= n; ++i) scanf("%lld%lld", &nod[i].l, &nod[i].r); long long l = 1, r = s, mid, ans; while (l <= r) { mid = (l + r) >> 1; int cntl = 0, cntr = 0; long long cos = 0; ve.clear(); for (int i = 1; i <= n; ++i) { if (nod[i].r < mid) cos += nod[i].l, ++cntr; else if (nod[i].l > mid) cos += nod[i].l, ++cntl; else ve.push_back(nod[i].l); } if (cntl >= (n + 1) / 2) ans = mid, l = mid + 1; else if (cntr > n / 2) r = mid - 1; else { sort(ve.begin(), ve.end()); int k = n / 2 - cntr; for (int i = 0; i < k; ++i) cos += ve[i]; cos += (ve.size() - k) * mid; if (cos <= s) { l = mid + 1; ans = mid; } else r = mid - 1; } } printf("%lld\n", ans); } return 0; }
### Prompt Develop a solution in Cpp to the problem described below: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int t, n; long long s; struct node { long long l, r; } nod[200005]; vector<long long> ve; int main() { scanf("%d", &t); while (t--) { scanf("%d%lld", &n, &s); for (int i = 1; i <= n; ++i) scanf("%lld%lld", &nod[i].l, &nod[i].r); long long l = 1, r = s, mid, ans; while (l <= r) { mid = (l + r) >> 1; int cntl = 0, cntr = 0; long long cos = 0; ve.clear(); for (int i = 1; i <= n; ++i) { if (nod[i].r < mid) cos += nod[i].l, ++cntr; else if (nod[i].l > mid) cos += nod[i].l, ++cntl; else ve.push_back(nod[i].l); } if (cntl >= (n + 1) / 2) ans = mid, l = mid + 1; else if (cntr > n / 2) r = mid - 1; else { sort(ve.begin(), ve.end()); int k = n / 2 - cntr; for (int i = 0; i < k; ++i) cos += ve[i]; cos += (ve.size() - k) * mid; if (cos <= s) { l = mid + 1; ans = mid; } else r = mid - 1; } } printf("%lld\n", ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int l[200005], r[200005]; pair<int, int> p[200005]; int main() { int q; scanf("%d", &q); while (q--) { int n; long long s; scanf("%d%lld", &n, &s); for (int i = 1; i <= n; i++) { scanf("%d%d", &l[i], &r[i]); p[i] = pair<int, int>(l[i], r[i]); } sort(p + 1, p + 1 + n); int mid = (n + 1) / 2; int lo = p[mid].first, hi = 1e9, ans = 0; while (lo <= hi) { int mid = (lo + hi) / 2; int cnt1 = 0, cnt2 = 0; for (int i = 1; i <= n; i++) { if (p[i].first <= mid) cnt1++; } long long LO = 0; bool ok = 1; priority_queue<int> pq; for (int i = 1; i <= n / 2; i++) { if (p[i].first > mid) { LO += p[i].first; } else { LO += p[i].first; if (p[i].second >= mid) { pq.push(i); } } } for (int i = n / 2 + 1; i <= n; i++) { if (p[i].first <= mid && mid <= p[i].second) { LO += mid; } else if (p[i].second < mid) { if (pq.empty()) { ok = 0; break; } int j = pq.top(); pq.pop(); LO -= p[j].first; LO += p[i].first; LO += mid; } else { LO += p[i].first; } } if (!ok) { hi = mid - 1; continue; } if (LO <= s) ans = mid, lo = mid + 1; else hi = mid - 1; } printf("%d\n", ans); } }
### Prompt Construct a Cpp code solution to the problem outlined: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int l[200005], r[200005]; pair<int, int> p[200005]; int main() { int q; scanf("%d", &q); while (q--) { int n; long long s; scanf("%d%lld", &n, &s); for (int i = 1; i <= n; i++) { scanf("%d%d", &l[i], &r[i]); p[i] = pair<int, int>(l[i], r[i]); } sort(p + 1, p + 1 + n); int mid = (n + 1) / 2; int lo = p[mid].first, hi = 1e9, ans = 0; while (lo <= hi) { int mid = (lo + hi) / 2; int cnt1 = 0, cnt2 = 0; for (int i = 1; i <= n; i++) { if (p[i].first <= mid) cnt1++; } long long LO = 0; bool ok = 1; priority_queue<int> pq; for (int i = 1; i <= n / 2; i++) { if (p[i].first > mid) { LO += p[i].first; } else { LO += p[i].first; if (p[i].second >= mid) { pq.push(i); } } } for (int i = n / 2 + 1; i <= n; i++) { if (p[i].first <= mid && mid <= p[i].second) { LO += mid; } else if (p[i].second < mid) { if (pq.empty()) { ok = 0; break; } int j = pq.top(); pq.pop(); LO -= p[j].first; LO += p[i].first; LO += mid; } else { LO += p[i].first; } } if (!ok) { hi = mid - 1; continue; } if (LO <= s) ans = mid, lo = mid + 1; else hi = mid - 1; } printf("%d\n", ans); } } ```
#include <bits/stdc++.h> using namespace std; vector<pair<long long, long long>> v; long long n, s; bool ok(int mid) { long long sum = 0; long long cnt = 0; vector<long long> vec; for (int i = 0; i < n; i++) { if (v[i].second < mid) sum += v[i].first; else if (v[i].first >= mid) { sum += v[i].first; cnt++; } else { vec.push_back(v[i].first); } } long long need = max((long long)0, ((n + 1) >> 1) - cnt); if (vec.size() < need) return false; else { int l = vec.size(); for (int i = 0; i < l - need; i++) { sum += vec[i]; } sum += (need * mid); } if (sum <= s) return true; return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { cin >> n >> s; long long l, r; long long maxi = 0, mini = INT_MAX; for (int i = 0; i < n; i++) { cin >> l >> r; maxi = max(maxi, r); mini = min(mini, l); v.push_back(make_pair(l, r)); } sort(v.begin(), v.end()); long long b = mini, e = maxi, mid; long long ans = 0; while (b <= e) { mid = (b + e) >> 1; if (ok(mid)) { b = mid + 1; ans = mid; } else e = mid - 1; } cout << ans << endl; v.clear(); } return 0; }
### Prompt Generate a cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<pair<long long, long long>> v; long long n, s; bool ok(int mid) { long long sum = 0; long long cnt = 0; vector<long long> vec; for (int i = 0; i < n; i++) { if (v[i].second < mid) sum += v[i].first; else if (v[i].first >= mid) { sum += v[i].first; cnt++; } else { vec.push_back(v[i].first); } } long long need = max((long long)0, ((n + 1) >> 1) - cnt); if (vec.size() < need) return false; else { int l = vec.size(); for (int i = 0; i < l - need; i++) { sum += vec[i]; } sum += (need * mid); } if (sum <= s) return true; return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { cin >> n >> s; long long l, r; long long maxi = 0, mini = INT_MAX; for (int i = 0; i < n; i++) { cin >> l >> r; maxi = max(maxi, r); mini = min(mini, l); v.push_back(make_pair(l, r)); } sort(v.begin(), v.end()); long long b = mini, e = maxi, mid; long long ans = 0; while (b <= e) { mid = (b + e) >> 1; if (ok(mid)) { b = mid + 1; ans = mid; } else e = mid - 1; } cout << ans << endl; v.clear(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; using ll = long long; #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) { for (ll i = 0; i < v.size(); ++i) os << v[i] << " "; return os; } template <typename T> ostream& operator<<(ostream& os, const set<T>& v) { for (auto it : v) os << it << " "; return os; } template <typename T, typename S> ostream& operator<<(ostream& os, const pair<T, S>& v) { os << v.first << " " << v.second; return os; } const ll mod = 1e9 + 7; const ll inf = 2e18; const ll ninf = -2e18; ll takemod(ll a) { return ((a % mod) + mod) % mod; } ll pow(ll a, ll b, ll m) { ll ans = 1; a %= m; while (b) { if (b & 1) ans = (ans * a) % m; b /= 2; a = (a * a) % m; } return ans; } ll modinv(ll a) { return takemod(pow(takemod(a), mod - 2, mod)); } ll n, s; bool check(ll mid, vector<pair<ll, ll> >& arr) { vector<pair<ll, ll> > tmp; ll lt = 0; ll minc = 0; for (ll i = 0; i < n; i++) { if (arr[i].second >= mid) tmp.push_back(arr[i]); else { lt++; minc += arr[i].first; } } ll req = n / 2; req++; sort(tmp.begin(), tmp.end()); ll bound = (ll)tmp.size() - req; for (ll i = 0; i < bound; i++) { if (tmp[i].first <= mid) { lt++; minc += tmp[i].first; } } for (ll i = bound; i < tmp.size() && i >= 0; i++) { minc += max(tmp[i].first, mid); } if (lt != req - 1 || minc > s) return false; return true; } void solve() { cin >> n >> s; vector<pair<ll, ll> > arr(n); vector<ll> tmp; for (ll i = 0; i < n; i++) { ll x, y; cin >> x >> y; arr[i] = {x, y}; tmp.push_back(x); } sort(tmp.begin(), tmp.end()); ll lo = tmp[n / 2]; ll hi = 1e9; while (lo <= hi) { ll mid = (lo + hi) / 2; if (check(mid, arr)) { lo = mid + 1; } else hi = mid - 1; } cout << lo - 1 << '\n'; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); time_t t1, t2; t1 = clock(); ll t; cin >> t; while (t--) solve(); t2 = clock(); cerr << '\n' << t2 - t1 << '\n'; return 0; }
### Prompt Develop a solution in CPP to the problem described below: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long; #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) { for (ll i = 0; i < v.size(); ++i) os << v[i] << " "; return os; } template <typename T> ostream& operator<<(ostream& os, const set<T>& v) { for (auto it : v) os << it << " "; return os; } template <typename T, typename S> ostream& operator<<(ostream& os, const pair<T, S>& v) { os << v.first << " " << v.second; return os; } const ll mod = 1e9 + 7; const ll inf = 2e18; const ll ninf = -2e18; ll takemod(ll a) { return ((a % mod) + mod) % mod; } ll pow(ll a, ll b, ll m) { ll ans = 1; a %= m; while (b) { if (b & 1) ans = (ans * a) % m; b /= 2; a = (a * a) % m; } return ans; } ll modinv(ll a) { return takemod(pow(takemod(a), mod - 2, mod)); } ll n, s; bool check(ll mid, vector<pair<ll, ll> >& arr) { vector<pair<ll, ll> > tmp; ll lt = 0; ll minc = 0; for (ll i = 0; i < n; i++) { if (arr[i].second >= mid) tmp.push_back(arr[i]); else { lt++; minc += arr[i].first; } } ll req = n / 2; req++; sort(tmp.begin(), tmp.end()); ll bound = (ll)tmp.size() - req; for (ll i = 0; i < bound; i++) { if (tmp[i].first <= mid) { lt++; minc += tmp[i].first; } } for (ll i = bound; i < tmp.size() && i >= 0; i++) { minc += max(tmp[i].first, mid); } if (lt != req - 1 || minc > s) return false; return true; } void solve() { cin >> n >> s; vector<pair<ll, ll> > arr(n); vector<ll> tmp; for (ll i = 0; i < n; i++) { ll x, y; cin >> x >> y; arr[i] = {x, y}; tmp.push_back(x); } sort(tmp.begin(), tmp.end()); ll lo = tmp[n / 2]; ll hi = 1e9; while (lo <= hi) { ll mid = (lo + hi) / 2; if (check(mid, arr)) { lo = mid + 1; } else hi = mid - 1; } cout << lo - 1 << '\n'; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); time_t t1, t2; t1 = clock(); ll t; cin >> t; while (t--) solve(); t2 = clock(); cerr << '\n' << t2 - t1 << '\n'; return 0; } ```
#include <bits/stdc++.h> using namespace std; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } const long long mod = 1e9 + 7; void chmod(long long &M) { if (M >= mod) M %= mod; else if (M < 0) { M += (abs(M) / mod + 1) * mod; M %= mod; } } long long modpow(long long x, long long n) { if (n == 0) return 1; long long res = modpow(x, n / 2); if (n % 2 == 0) return res * res % mod; else return res * res % mod * x % mod; } int getl(int i, int N) { return i == 0 ? N - 1 : i - 1; }; int getr(int i, int N) { return i == N - 1 ? 0 : i + 1; }; double argument(const pair<double, double> &a, const pair<double, double> &b) { double ax = a.first, ay = a.second, bx = b.first, by = b.second; return atan2(by - ay, bx - ax); } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int t; cin >> t; while (t--) { int n; cin >> n; int half = n / 2; long long s; cin >> s; vector<long long> l(n), r(n); for (int i = 0; i < n; i++) cin >> l[i] >> r[i]; long long ok = 0, ng = 1e9 + 1; long long left_cnt, right_cnt, sum, mid; while (ng - ok > 1) { sum = 0, left_cnt = 0, right_cnt = 0; mid = (ok + ng) / 2; priority_queue<long long> pq; for (int i = 0; i < n; i++) { if (mid < l[i]) { ++right_cnt; sum += l[i]; } else if (mid > r[i]) { ++left_cnt; sum += l[i]; } else { pq.push(l[i]); } } if (right_cnt > half) { ok = mid; continue; } if (left_cnt > half) { ng = mid; continue; } while (!pq.empty()) { long long tmp = pq.top(); pq.pop(); if (right_cnt < half + 1) { ++right_cnt; sum += mid; } else { sum += tmp; } } (right_cnt == half + 1 && sum <= s ? ok : ng) = mid; } cout << ok << endl; } return 0; }
### Prompt Please formulate a cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } const long long mod = 1e9 + 7; void chmod(long long &M) { if (M >= mod) M %= mod; else if (M < 0) { M += (abs(M) / mod + 1) * mod; M %= mod; } } long long modpow(long long x, long long n) { if (n == 0) return 1; long long res = modpow(x, n / 2); if (n % 2 == 0) return res * res % mod; else return res * res % mod * x % mod; } int getl(int i, int N) { return i == 0 ? N - 1 : i - 1; }; int getr(int i, int N) { return i == N - 1 ? 0 : i + 1; }; double argument(const pair<double, double> &a, const pair<double, double> &b) { double ax = a.first, ay = a.second, bx = b.first, by = b.second; return atan2(by - ay, bx - ax); } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int t; cin >> t; while (t--) { int n; cin >> n; int half = n / 2; long long s; cin >> s; vector<long long> l(n), r(n); for (int i = 0; i < n; i++) cin >> l[i] >> r[i]; long long ok = 0, ng = 1e9 + 1; long long left_cnt, right_cnt, sum, mid; while (ng - ok > 1) { sum = 0, left_cnt = 0, right_cnt = 0; mid = (ok + ng) / 2; priority_queue<long long> pq; for (int i = 0; i < n; i++) { if (mid < l[i]) { ++right_cnt; sum += l[i]; } else if (mid > r[i]) { ++left_cnt; sum += l[i]; } else { pq.push(l[i]); } } if (right_cnt > half) { ok = mid; continue; } if (left_cnt > half) { ng = mid; continue; } while (!pq.empty()) { long long tmp = pq.top(); pq.pop(); if (right_cnt < half + 1) { ++right_cnt; sum += mid; } else { sum += tmp; } } (right_cnt == half + 1 && sum <= s ? ok : ng) = mid; } cout << ok << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; const long long INF = mod * mod; int n; long long s; pair<long long, long long> p[200010]; bool check(long long mid) { sort(p, p + n, [&mid](const pair<long long, long long> &a, const pair<long long, long long> &b) { if (mid <= a.second && mid <= b.second) return a.first < b.first; else if (mid <= a.second) return false; else if (mid <= b.second) return true; else return a.first < b.first; }); long long tmp = 0; for (int((i)) = (0); ((i)) < ((n / 2)); ++((i))) tmp += p[i].first; for (int(i) = (n / 2); (i) < (n); ++(i)) { if (p[i].second < mid) return false; tmp += max(mid, p[i].first); } return tmp <= s; } int main() { int q; scanf("%d", &q); while (q--) { scanf("%d%lld", &n, &s); for (int((i)) = (0); ((i)) < ((n)); ++((i))) scanf("%lld%lld", &p[i].first, &p[i].second); long long high = s + 1, low = 0; while (high - low > 1) { long long mid = (high + low) / 2; (check(mid) ? low : high) = mid; } printf("%lld\n", low); } }
### Prompt Generate a cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long mod = 998244353; const long long INF = mod * mod; int n; long long s; pair<long long, long long> p[200010]; bool check(long long mid) { sort(p, p + n, [&mid](const pair<long long, long long> &a, const pair<long long, long long> &b) { if (mid <= a.second && mid <= b.second) return a.first < b.first; else if (mid <= a.second) return false; else if (mid <= b.second) return true; else return a.first < b.first; }); long long tmp = 0; for (int((i)) = (0); ((i)) < ((n / 2)); ++((i))) tmp += p[i].first; for (int(i) = (n / 2); (i) < (n); ++(i)) { if (p[i].second < mid) return false; tmp += max(mid, p[i].first); } return tmp <= s; } int main() { int q; scanf("%d", &q); while (q--) { scanf("%d%lld", &n, &s); for (int((i)) = (0); ((i)) < ((n)); ++((i))) scanf("%lld%lld", &p[i].first, &p[i].second); long long high = s + 1, low = 0; while (high - low > 1) { long long mid = (high + low) / 2; (check(mid) ? low : high) = mid; } printf("%lld\n", low); } } ```
#include <bits/stdc++.h> using namespace std; int n; long long s; vector<array<int, 2>> v(200001); bool check(int x) { int cnt = n / 2 + 1; long long res = 0; for (int i = n - 1; i > -1 && res <= s; i--) { if (cnt && v[i][1] >= x) { res += max(x, v[i][0]); cnt--; } else res += v[i][0]; } return (res <= s && cnt == 0); } void solve() { cin >> n >> s; v.resize(n); for (int i = 0; i < n; i++) cin >> v[i][0] >> v[i][1]; sort(v.begin(), v.end()); int l = 0, r = 1e9 + 5, mid, ans = 1; while (l <= r) { mid = (l + r) / 2; if (check(mid)) { ans = mid; l = mid + 1; } else r = mid - 1; } cout << ans << '\n'; } int main() { ios::sync_with_stdio(0); cin.tie(0); int tt; cin >> tt; while (tt--) solve(); }
### Prompt Develop a solution in cpp to the problem described below: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n; long long s; vector<array<int, 2>> v(200001); bool check(int x) { int cnt = n / 2 + 1; long long res = 0; for (int i = n - 1; i > -1 && res <= s; i--) { if (cnt && v[i][1] >= x) { res += max(x, v[i][0]); cnt--; } else res += v[i][0]; } return (res <= s && cnt == 0); } void solve() { cin >> n >> s; v.resize(n); for (int i = 0; i < n; i++) cin >> v[i][0] >> v[i][1]; sort(v.begin(), v.end()); int l = 0, r = 1e9 + 5, mid, ans = 1; while (l <= r) { mid = (l + r) / 2; if (check(mid)) { ans = mid; l = mid + 1; } else r = mid - 1; } cout << ans << '\n'; } int main() { ios::sync_with_stdio(0); cin.tie(0); int tt; cin >> tt; while (tt--) solve(); } ```
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-8; const int MOD = 1000000007; const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; struct IOSetup { IOSetup() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(20); cerr << fixed << setprecision(10); } } iosetup; int main() { int t; cin >> t; while (t--) { int n; long long s; cin >> n >> s; vector<long long> l(n), r(n); for (int i = (0); i < (n); ++i) cin >> l[i] >> r[i]; vector<pair<int, int> > lr(n); for (int i = (0); i < (n); ++i) lr[i] = {l[i], r[i]}; sort((lr).begin(), (lr).end()); long long lb = lr[n / 2].first, ub = s + 1; while (ub - lb > 1) { long long mid = (lb + ub) / 2; vector<int> a, c; vector<pair<long long, long long> > lr; for (int i = (0); i < (n); ++i) { if (r[i] < mid) { a.emplace_back(i); } else if (mid < l[i]) { c.emplace_back(i); } else { lr.emplace_back(l[i], r[i]); } } if (a.size() >= (n + 1) / 2) { ub = mid; continue; } long long need = 0; for (int e : a) need += l[e]; for (int e : c) need += l[e]; sort((lr).begin(), (lr).end()); reverse((lr).begin(), (lr).end()); int nokori = (n + 1) / 2 - c.size(); need += mid * nokori; for (int i = (nokori); i < (lr.size()); ++i) need += lr[i].first; (need <= s ? lb : ub) = mid; } cout << lb << '\n'; } return 0; }
### Prompt Create a solution in Cpp for the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-8; const int MOD = 1000000007; const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; struct IOSetup { IOSetup() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(20); cerr << fixed << setprecision(10); } } iosetup; int main() { int t; cin >> t; while (t--) { int n; long long s; cin >> n >> s; vector<long long> l(n), r(n); for (int i = (0); i < (n); ++i) cin >> l[i] >> r[i]; vector<pair<int, int> > lr(n); for (int i = (0); i < (n); ++i) lr[i] = {l[i], r[i]}; sort((lr).begin(), (lr).end()); long long lb = lr[n / 2].first, ub = s + 1; while (ub - lb > 1) { long long mid = (lb + ub) / 2; vector<int> a, c; vector<pair<long long, long long> > lr; for (int i = (0); i < (n); ++i) { if (r[i] < mid) { a.emplace_back(i); } else if (mid < l[i]) { c.emplace_back(i); } else { lr.emplace_back(l[i], r[i]); } } if (a.size() >= (n + 1) / 2) { ub = mid; continue; } long long need = 0; for (int e : a) need += l[e]; for (int e : c) need += l[e]; sort((lr).begin(), (lr).end()); reverse((lr).begin(), (lr).end()); int nokori = (n + 1) / 2 - c.size(); need += mid * nokori; for (int i = (nokori); i < (lr.size()); ++i) need += lr[i].first; (need <= s ? lb : ub) = mid; } cout << lb << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; bool possible(long long val, long long s, vector<pair<int, int>> &pl, int n) { long long tot = 0, cnt = (n + 1) / 2; vector<int> llp; for (int i = 0; i < n; i++) { if (pl[i].first >= val) { tot += pl[i].first; cnt--; } else if (pl[i].second < val) { tot += pl[i].first; } else { llp.push_back(pl[i].first); } } if (cnt <= 0) return true; if (cnt > llp.size()) return false; sort(llp.begin(), llp.end()); for (int i = 0; i < llp.size() - cnt; i++) { tot += llp[i]; } tot += val * cnt; return tot <= s; } int main() { ios_base::sync_with_stdio(0), cin.tie(0); int t; cin >> t; while (t--) { int n; long long s; cin >> n >> s; long long high, low; vector<pair<int, int>> pl(n); for (int i = 0; i < n; i++) { cin >> pl[i].first >> pl[i].second; } if (n == 1) { cout << min(s, (long long)pl[0].second) << endl; continue; } low = 0; high = s; while (low + 1 < high) { long long mid = (low + high) / 2; if (possible(mid, s, pl, n)) { low = mid; } else { high = mid; } } cout << low << endl; } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; bool possible(long long val, long long s, vector<pair<int, int>> &pl, int n) { long long tot = 0, cnt = (n + 1) / 2; vector<int> llp; for (int i = 0; i < n; i++) { if (pl[i].first >= val) { tot += pl[i].first; cnt--; } else if (pl[i].second < val) { tot += pl[i].first; } else { llp.push_back(pl[i].first); } } if (cnt <= 0) return true; if (cnt > llp.size()) return false; sort(llp.begin(), llp.end()); for (int i = 0; i < llp.size() - cnt; i++) { tot += llp[i]; } tot += val * cnt; return tot <= s; } int main() { ios_base::sync_with_stdio(0), cin.tie(0); int t; cin >> t; while (t--) { int n; long long s; cin >> n >> s; long long high, low; vector<pair<int, int>> pl(n); for (int i = 0; i < n; i++) { cin >> pl[i].first >> pl[i].second; } if (n == 1) { cout << min(s, (long long)pl[0].second) << endl; continue; } low = 0; high = s; while (low + 1 < high) { long long mid = (low + high) / 2; if (possible(mid, s, pl, n)) { low = mid; } else { high = mid; } } cout << low << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long double pi = 3.14159265358979323; long long mod = 1000000000 + 7; long long modu = 998244353; const long double pii = acos(-1.0); const long long INF = 1e18; const long long inf = 1e9; long long power(long long x, long long y) { long long res = 1; x = x; while (y > 0) { if (y & 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } long long powe(long long x, long long y) { x = x % mod, y = y % (mod - 1); long long ans = 1; while (y > 0) { if (y & 1) { ans = (1ll * x * ans) % mod; } y >>= 1; x = (1ll * x * x) % mod; } return ans; } long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } long long lcm(long long a, long long b) { return (a / gcd(a, b) * b); } bool isPrime(long long n) { if (n < 2) return false; for (long long i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } long long ncr(long long n, long long r) { long long res = 1; if (r > n) return 0; if (r > n - r) r = n - r; for (long long i = 0; i < r; i++) { res *= (n - i); res /= (i + 1); } return res; } long long add(long long a, long long b) { return ((a % mod + b % mod) % mod); } long long sub(long long a, long long b) { return ((a % mod - b % mod + mod) % mod); } long long mul(long long a, long long b) { return (((a % mod) * (b % mod)) % mod); } long long divi(long long a, long long b) { return (mul(a, powe(b, mod - 2)) % mod); } void fun() {} const long long N = 2e9 + 7; void pre() {} bool ok(long long mid, long long s, std::vector<pair<long long, long long> > v, long long n) { long long cnt = 0; long long avail = s; for (long long i = 0; i < n; i++) { if (v[i].second >= mid) cnt++; } if (cnt < (n + 1) / 2) return false; long long ans = 0; for (long long i = n - 1; i >= 0; i--) { if (v[i].second >= mid) avail -= max(v[i].first, mid), ans++, v[i].first = -1; if (ans == (n + 1) / 2) break; } for (long long i = 0; i < n; i++) { if (v[i].first == -1) continue; avail -= v[i].first; } return (avail >= 0); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; fun(); pre(); long long tttt = 1; cin >> tttt; while (tttt--) { long long n, s; cin >> n >> s; std::vector<pair<long long, long long> > v; for (long long i = 0; i < n; i++) { long long l, r; cin >> l >> r; v.push_back(make_pair(l, r)); } if (n == 1) { cout << min(s, v[0].second) << "\n"; continue; } sort(v.begin(), v.end()); long long ans = 0; long long hi = inf, lo = 0; while (hi >= lo) { long long mid = (hi + lo) >> 1; if (ok(mid, s, v, n)) ans = mid, lo = mid + 1; else hi = mid - 1; } cout << ans << "\n"; } cerr << "time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n"; return 0; }
### Prompt Generate a cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long double pi = 3.14159265358979323; long long mod = 1000000000 + 7; long long modu = 998244353; const long double pii = acos(-1.0); const long long INF = 1e18; const long long inf = 1e9; long long power(long long x, long long y) { long long res = 1; x = x; while (y > 0) { if (y & 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } long long powe(long long x, long long y) { x = x % mod, y = y % (mod - 1); long long ans = 1; while (y > 0) { if (y & 1) { ans = (1ll * x * ans) % mod; } y >>= 1; x = (1ll * x * x) % mod; } return ans; } long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } long long lcm(long long a, long long b) { return (a / gcd(a, b) * b); } bool isPrime(long long n) { if (n < 2) return false; for (long long i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } long long ncr(long long n, long long r) { long long res = 1; if (r > n) return 0; if (r > n - r) r = n - r; for (long long i = 0; i < r; i++) { res *= (n - i); res /= (i + 1); } return res; } long long add(long long a, long long b) { return ((a % mod + b % mod) % mod); } long long sub(long long a, long long b) { return ((a % mod - b % mod + mod) % mod); } long long mul(long long a, long long b) { return (((a % mod) * (b % mod)) % mod); } long long divi(long long a, long long b) { return (mul(a, powe(b, mod - 2)) % mod); } void fun() {} const long long N = 2e9 + 7; void pre() {} bool ok(long long mid, long long s, std::vector<pair<long long, long long> > v, long long n) { long long cnt = 0; long long avail = s; for (long long i = 0; i < n; i++) { if (v[i].second >= mid) cnt++; } if (cnt < (n + 1) / 2) return false; long long ans = 0; for (long long i = n - 1; i >= 0; i--) { if (v[i].second >= mid) avail -= max(v[i].first, mid), ans++, v[i].first = -1; if (ans == (n + 1) / 2) break; } for (long long i = 0; i < n; i++) { if (v[i].first == -1) continue; avail -= v[i].first; } return (avail >= 0); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; fun(); pre(); long long tttt = 1; cin >> tttt; while (tttt--) { long long n, s; cin >> n >> s; std::vector<pair<long long, long long> > v; for (long long i = 0; i < n; i++) { long long l, r; cin >> l >> r; v.push_back(make_pair(l, r)); } if (n == 1) { cout << min(s, v[0].second) << "\n"; continue; } sort(v.begin(), v.end()); long long ans = 0; long long hi = inf, lo = 0; while (hi >= lo) { long long mid = (hi + lo) >> 1; if (ok(mid, s, v, n)) ans = mid, lo = mid + 1; else hi = mid - 1; } cout << ans << "\n"; } cerr << "time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n"; return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long int N = 5e5 + 5, mod = 1000000007, bit = 60; bool comp(pair<long long int, long long int> &a, pair<long long int, long long int> &b) { if (a.second != b.second) { return a.second < b.second; } return a.first < b.first; } bool comp1(pair<long long int, long long int> &a, pair<long long int, long long int> &b) { if (a.first != b.first) { return a.first < b.first; } return a.second < b.second; } bool check(long long int mid, long long int k, vector<pair<long long int, long long int> > &p) { long long int n = p.size(), cost = 0, i, compulsory = 0, need, rem; vector<pair<long long int, long long int> > v; for (i = 0; i < n; i++) { if (p[i].second < mid) { cost += p[i].first; } else if (p[i].first >= mid) { compulsory++; cost += p[i].first; } else { v.push_back({p[i].first, p[i].second}); } } need = max(0ll, ((n + 1) >> 1) - compulsory); if (v.size() < need) { return 0; } rem = v.size() - need; sort(v.begin(), v.end(), comp1); reverse(v.begin(), v.end()); while (rem--) { cost += v.back().first; if (v.back().first > mid) { return 0; } v.pop_back(); } while (need--) { cost += max(mid, v.back().first); if (v.back().second < mid) { return 0; } v.pop_back(); } return cost <= k; } signed main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); long long int pro = 1, temp, t, i, j, l, r, n, m, mid, z, k, x, y, rem, carry = 0, ind, ans = 0, mx = -LONG_LONG_MAX, mn = LONG_LONG_MAX, cnt = 0, curr = 0, prev, next, sum = 0, flag = 1, i1 = -1, i2 = -1; cin >> t; while (t--) { cin >> n >> k; vector<pair<long long int, long long int> > p(n); for (i = 0; i < n; i++) { cin >> p[i].first >> p[i].second; } sort(p.begin(), p.end()); l = p[n / 2].first; sort(p.begin(), p.end(), comp); r = p[n / 2].second; while (l <= r) { mid = (l + r) >> 1; if (check(mid, k, p)) { ans = mid; l = mid + 1; } else { r = mid - 1; } } cout << ans << '\n'; } return 0; }
### Prompt In CPP, your task is to solve the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long int N = 5e5 + 5, mod = 1000000007, bit = 60; bool comp(pair<long long int, long long int> &a, pair<long long int, long long int> &b) { if (a.second != b.second) { return a.second < b.second; } return a.first < b.first; } bool comp1(pair<long long int, long long int> &a, pair<long long int, long long int> &b) { if (a.first != b.first) { return a.first < b.first; } return a.second < b.second; } bool check(long long int mid, long long int k, vector<pair<long long int, long long int> > &p) { long long int n = p.size(), cost = 0, i, compulsory = 0, need, rem; vector<pair<long long int, long long int> > v; for (i = 0; i < n; i++) { if (p[i].second < mid) { cost += p[i].first; } else if (p[i].first >= mid) { compulsory++; cost += p[i].first; } else { v.push_back({p[i].first, p[i].second}); } } need = max(0ll, ((n + 1) >> 1) - compulsory); if (v.size() < need) { return 0; } rem = v.size() - need; sort(v.begin(), v.end(), comp1); reverse(v.begin(), v.end()); while (rem--) { cost += v.back().first; if (v.back().first > mid) { return 0; } v.pop_back(); } while (need--) { cost += max(mid, v.back().first); if (v.back().second < mid) { return 0; } v.pop_back(); } return cost <= k; } signed main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); long long int pro = 1, temp, t, i, j, l, r, n, m, mid, z, k, x, y, rem, carry = 0, ind, ans = 0, mx = -LONG_LONG_MAX, mn = LONG_LONG_MAX, cnt = 0, curr = 0, prev, next, sum = 0, flag = 1, i1 = -1, i2 = -1; cin >> t; while (t--) { cin >> n >> k; vector<pair<long long int, long long int> > p(n); for (i = 0; i < n; i++) { cin >> p[i].first >> p[i].second; } sort(p.begin(), p.end()); l = p[n / 2].first; sort(p.begin(), p.end(), comp); r = p[n / 2].second; while (l <= r) { mid = (l + r) >> 1; if (check(mid, k, p)) { ans = mid; l = mid + 1; } else { r = mid - 1; } } cout << ans << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long md = 1e9 + 7; const long long MX = 2e5 + 5; const long long INF = 1e18; const long double PI = 4 * atan((long double)1); long long power_md(long long a, long long n) { long long res = 1; while (n) { if (n % 2) res = (res % md * a % md) % md; a = (a % md * a % md) % md; n /= 2; } res %= md; return res; } long long power(long long a, long long n) { long long res = 1; while (n) { if (n % 2) res *= a; a = a * a; n /= 2; } return res; } long long abst(long long a) { return ((a < 0) ? (-1 * a) : (a)); } class cmp_set { public: bool operator()(long long a, long long b) { return a > b; } }; vector<string> vec_splitter(string s) { s += ','; vector<string> res; while (!s.empty()) { res.push_back(s.substr(0, s.find(','))); s = s.substr(s.find(',') + 1); } return res; } void debug_out(vector<string> __attribute__((unused)) args, __attribute__((unused)) long long idx, __attribute__((unused)) long long LINE_NUM) { cerr << endl; } template <typename Head, typename... Tail> void debug_out(vector<string> args, long long idx, long long LINE_NUM, Head H, Tail... T) { if (idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") "; stringstream ss; ss << H; cerr << args[idx] << " = " << ss.str(); debug_out(args, idx + 1, LINE_NUM, T...); } long long n, s; vector<pair<long long, long long> > v; long long fun(long long x) { long long req = 0; long long cnt = 0; for (long long i = v.size() - 1; i >= 0; i--) { if (cnt == ((n / 2) + 1)) break; if (v[i].second < x) continue; else { cnt++; req += max((long long)0, x - v[i].first); } } if (cnt < ((n / 2) + 1)) return 0; if (req > s) return 0; return 1; } int32_t main() { ios_base::sync_with_stdio(false), cin.tie(NULL); long long t; cin >> t; while (t--) { v.clear(); cin >> n >> s; long long sum = 0; for (long long i = 0; i < n; i++) { long long l, r; cin >> l >> r; v.push_back({l, r}); sum += l; } sort(v.begin(), v.end()); s -= sum; long long lo = v[n / 2].first, hi = 1e9, mid; while (lo < hi) { mid = lo + (hi - lo + 1) / 2; if (fun(mid)) lo = mid; else hi = mid - 1; } cout << lo << "\n"; } return 0; }
### Prompt Generate a cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long md = 1e9 + 7; const long long MX = 2e5 + 5; const long long INF = 1e18; const long double PI = 4 * atan((long double)1); long long power_md(long long a, long long n) { long long res = 1; while (n) { if (n % 2) res = (res % md * a % md) % md; a = (a % md * a % md) % md; n /= 2; } res %= md; return res; } long long power(long long a, long long n) { long long res = 1; while (n) { if (n % 2) res *= a; a = a * a; n /= 2; } return res; } long long abst(long long a) { return ((a < 0) ? (-1 * a) : (a)); } class cmp_set { public: bool operator()(long long a, long long b) { return a > b; } }; vector<string> vec_splitter(string s) { s += ','; vector<string> res; while (!s.empty()) { res.push_back(s.substr(0, s.find(','))); s = s.substr(s.find(',') + 1); } return res; } void debug_out(vector<string> __attribute__((unused)) args, __attribute__((unused)) long long idx, __attribute__((unused)) long long LINE_NUM) { cerr << endl; } template <typename Head, typename... Tail> void debug_out(vector<string> args, long long idx, long long LINE_NUM, Head H, Tail... T) { if (idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") "; stringstream ss; ss << H; cerr << args[idx] << " = " << ss.str(); debug_out(args, idx + 1, LINE_NUM, T...); } long long n, s; vector<pair<long long, long long> > v; long long fun(long long x) { long long req = 0; long long cnt = 0; for (long long i = v.size() - 1; i >= 0; i--) { if (cnt == ((n / 2) + 1)) break; if (v[i].second < x) continue; else { cnt++; req += max((long long)0, x - v[i].first); } } if (cnt < ((n / 2) + 1)) return 0; if (req > s) return 0; return 1; } int32_t main() { ios_base::sync_with_stdio(false), cin.tie(NULL); long long t; cin >> t; while (t--) { v.clear(); cin >> n >> s; long long sum = 0; for (long long i = 0; i < n; i++) { long long l, r; cin >> l >> r; v.push_back({l, r}); sum += l; } sort(v.begin(), v.end()); s -= sum; long long lo = v[n / 2].first, hi = 1e9, mid; while (lo < hi) { mid = lo + (hi - lo + 1) / 2; if (fun(mid)) lo = mid; else hi = mid - 1; } cout << lo << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long s; int n; vector<pair<int, int>> lims; int solve(int median) { int smallcnt = 0; int largecnt = 0; long long rem = s; for (int i = 0; i < n; i++) if (lims[i].second < median) { smallcnt++; rem -= lims[i].first; } else if (lims[i].first > median) { largecnt++; rem -= lims[i].first; } if (largecnt > n / 2) return -1; if (smallcnt > n / 2) return 1; int smallleft = n / 2 - smallcnt; for (int i = 0; i < n && smallleft > 0; i++) if (lims[i].first <= median && lims[i].second >= median) { rem -= lims[i].first; smallleft--; } int stillneed = n - n / 2 - largecnt; if (1LL * stillneed * median > rem) return 1; return 0; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { cin >> n >> s; lims.resize(n); for (int i = 0; i < n; i++) cin >> lims[i].first >> lims[i].second; sort(lims.begin(), lims.end()); int low = 1, high = 1000000000; while (low < high) { int mid = (low + high) >> 1; if (mid == low) mid++; int res = solve(mid); if (res == 1) high = mid - 1; else if (res == -1) low = mid + 1; else low = mid; } cout << low << "\n"; } return 0; }
### Prompt In Cpp, your task is to solve the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long s; int n; vector<pair<int, int>> lims; int solve(int median) { int smallcnt = 0; int largecnt = 0; long long rem = s; for (int i = 0; i < n; i++) if (lims[i].second < median) { smallcnt++; rem -= lims[i].first; } else if (lims[i].first > median) { largecnt++; rem -= lims[i].first; } if (largecnt > n / 2) return -1; if (smallcnt > n / 2) return 1; int smallleft = n / 2 - smallcnt; for (int i = 0; i < n && smallleft > 0; i++) if (lims[i].first <= median && lims[i].second >= median) { rem -= lims[i].first; smallleft--; } int stillneed = n - n / 2 - largecnt; if (1LL * stillneed * median > rem) return 1; return 0; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { cin >> n >> s; lims.resize(n); for (int i = 0; i < n; i++) cin >> lims[i].first >> lims[i].second; sort(lims.begin(), lims.end()); int low = 1, high = 1000000000; while (low < high) { int mid = (low + high) >> 1; if (mid == low) mid++; int res = solve(mid); if (res == 1) high = mid - 1; else if (res == -1) low = mid + 1; else low = mid; } cout << low << "\n"; } return 0; } ```
#include <bits/stdc++.h> const int INF = 1e9 + 7; const int MAXN = 3e5 + 20; const double eps = 1e-9; const long long inf = 1e18; const long double pi = acos(-1.0); using namespace std; int n; long long s; long long a[MAXN], b[MAXN]; long long cnt1, cnt2, pr, l, r, ans, m; long long sum; void solve() { cin >> n >> s; for (int i = 1; i <= n; i++) cin >> a[i] >> b[i]; l = 0; r = 1000000001; pr = n / 2; while (r - l > 1) { m = (l + r) / 2; cnt1 = 0; cnt2 = 0; sum = 0; vector<int> c; for (int i = 1; i <= n; i++) { if (b[i] < m) { cnt1++; sum += a[i]; } else if (a[i] > m) { cnt2++; sum += a[i]; } else c.push_back(a[i]); } if (cnt1 > pr) { r = m; continue; } if (cnt2 > pr) { l = m; continue; } sum += (pr - cnt2) * m; sum += m; if (sum > s) { r = m; continue; } sort(c.begin(), c.end()); for (int i = 0; i < pr - cnt1; i++) sum += c[i]; if (sum > s) { r = m; continue; } l = m; } cout << l << endl; } int main() { ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0); cout.setf(ios::fixed), cout.precision(20); int t; cin >> t; while (t > 0) { t--; solve(); } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> const int INF = 1e9 + 7; const int MAXN = 3e5 + 20; const double eps = 1e-9; const long long inf = 1e18; const long double pi = acos(-1.0); using namespace std; int n; long long s; long long a[MAXN], b[MAXN]; long long cnt1, cnt2, pr, l, r, ans, m; long long sum; void solve() { cin >> n >> s; for (int i = 1; i <= n; i++) cin >> a[i] >> b[i]; l = 0; r = 1000000001; pr = n / 2; while (r - l > 1) { m = (l + r) / 2; cnt1 = 0; cnt2 = 0; sum = 0; vector<int> c; for (int i = 1; i <= n; i++) { if (b[i] < m) { cnt1++; sum += a[i]; } else if (a[i] > m) { cnt2++; sum += a[i]; } else c.push_back(a[i]); } if (cnt1 > pr) { r = m; continue; } if (cnt2 > pr) { l = m; continue; } sum += (pr - cnt2) * m; sum += m; if (sum > s) { r = m; continue; } sort(c.begin(), c.end()); for (int i = 0; i < pr - cnt1; i++) sum += c[i]; if (sum > s) { r = m; continue; } l = m; } cout << l << endl; } int main() { ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0); cout.setf(ios::fixed), cout.precision(20); int t; cin >> t; while (t > 0) { t--; solve(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 3 * 1e5 + 10; struct node { long long l, r; } a[maxn]; int n; long long m; bool cmp(node a, node b) { if (a.l != b.l) return a.l > b.l; return a.r > b.r; } bool f(long long x) { int num = 0; long long sum = 0; for (int i = 0; i < n; i++) { if (a[i].r >= x && num < (n + 1) / 2) { if (a[i].l > x) sum += a[i].l; else sum += x; num++; } else { sum += a[i].l; } } if (num < ((n + 1) / 2)) return false; if (sum > m) return false; return true; } int main() { int t; scanf("%d", &t); while (t--) { scanf("%d", &n); scanf("%lld", &m); long long maxm = 0; for (int i = 0; i < n; i++) { scanf("%lld%lld", &a[i].l, &a[i].r); if (a[i].r > maxm) maxm = a[i].r; } sort(a, a + n, cmp); long long x = a[n / 2].l, y = maxm + 10; long long mid; long long maxx = a[n / 2].l; if (f(x)) { if (x > maxx) maxx = x; } if (f(y)) { if (y > maxx) maxx = y; } while (y - x > 1) { mid = x + (y - x) / 2; if (f(mid)) { if (mid > maxx) maxx = mid; x = mid; } else y = mid; } printf("%lld\n", maxx); } return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 3 * 1e5 + 10; struct node { long long l, r; } a[maxn]; int n; long long m; bool cmp(node a, node b) { if (a.l != b.l) return a.l > b.l; return a.r > b.r; } bool f(long long x) { int num = 0; long long sum = 0; for (int i = 0; i < n; i++) { if (a[i].r >= x && num < (n + 1) / 2) { if (a[i].l > x) sum += a[i].l; else sum += x; num++; } else { sum += a[i].l; } } if (num < ((n + 1) / 2)) return false; if (sum > m) return false; return true; } int main() { int t; scanf("%d", &t); while (t--) { scanf("%d", &n); scanf("%lld", &m); long long maxm = 0; for (int i = 0; i < n; i++) { scanf("%lld%lld", &a[i].l, &a[i].r); if (a[i].r > maxm) maxm = a[i].r; } sort(a, a + n, cmp); long long x = a[n / 2].l, y = maxm + 10; long long mid; long long maxx = a[n / 2].l; if (f(x)) { if (x > maxx) maxx = x; } if (f(y)) { if (y > maxx) maxx = y; } while (y - x > 1) { mid = x + (y - x) / 2; if (f(mid)) { if (mid > maxx) maxx = mid; x = mid; } else y = mid; } printf("%lld\n", maxx); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 5; long long tot; int n; pair<int, int> a[maxn]; bool judge(int mid) { long long sum = 0; int cnt = 0; vector<int> v; for (int i = 0; i < n; i++) { if (a[i].second < mid) sum += a[i].first; else if (a[i].first >= mid) { sum += a[i].first; cnt++; } else v.push_back(a[i].first); } sort(v.begin(), v.end()); cnt = max(0, (n + 1) / 2 - cnt); if (cnt > v.size()) return 0; for (int i = 0; i < v.size(); i++) { if (i < v.size() - cnt) sum += v[i]; else sum += mid; } return sum <= tot; } int main() { int t; scanf("%d", &t); while (t--) { scanf("%d%lld", &n, &tot); for (int i = 0; i < n; i++) { scanf("%d%d", &a[i].first, &a[i].second); } sort(a, a + n); int l = 1, r = 1e9 + 10; while (l < r) { int mid = (l + r + 1) >> 1; if (judge(mid)) { l = mid; } else { r = mid - 1; } } printf("%d\n", l); } return 0; }
### Prompt Please formulate a cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 5; long long tot; int n; pair<int, int> a[maxn]; bool judge(int mid) { long long sum = 0; int cnt = 0; vector<int> v; for (int i = 0; i < n; i++) { if (a[i].second < mid) sum += a[i].first; else if (a[i].first >= mid) { sum += a[i].first; cnt++; } else v.push_back(a[i].first); } sort(v.begin(), v.end()); cnt = max(0, (n + 1) / 2 - cnt); if (cnt > v.size()) return 0; for (int i = 0; i < v.size(); i++) { if (i < v.size() - cnt) sum += v[i]; else sum += mid; } return sum <= tot; } int main() { int t; scanf("%d", &t); while (t--) { scanf("%d%lld", &n, &tot); for (int i = 0; i < n; i++) { scanf("%d%d", &a[i].first, &a[i].second); } sort(a, a + n); int l = 1, r = 1e9 + 10; while (l < r) { int mid = (l + r + 1) >> 1; if (judge(mid)) { l = mid; } else { r = mid - 1; } } printf("%d\n", l); } return 0; } ```
#include <bits/stdc++.h> using namespace std; struct node { long long int l, r; } inp[200010]; long long n, s; bool cmp(node a, node b) { return a.l > b.l; } bool check(long long int m) { long long int sum = 0; int cnt = 0; for (int i = 0; i < n; i++) { sum += inp[i].l; } for (int i = 0; i < n; i++) { if (inp[i].r >= m) { sum += max(m - inp[i].l, 1ll * 0); cnt++; } if (cnt == (n + 1) / 2) { break; } } if (cnt == (n + 1) / 2 && sum <= s) { return true; } else { return false; } } int main() { int t; cin >> t; while (t--) { cin >> n >> s; for (int i = 0; i < n; i++) { cin >> inp[i].l >> inp[i].r; } long long int l = 0, r = s, ans; sort(inp, inp + n, cmp); while (l <= r) { long long int mid = (l + r) / 2; if (check(mid)) { ans = mid; l = mid + 1; } else { r = mid - 1; } } cout << ans << endl; } }
### Prompt Develop a solution in Cpp to the problem described below: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct node { long long int l, r; } inp[200010]; long long n, s; bool cmp(node a, node b) { return a.l > b.l; } bool check(long long int m) { long long int sum = 0; int cnt = 0; for (int i = 0; i < n; i++) { sum += inp[i].l; } for (int i = 0; i < n; i++) { if (inp[i].r >= m) { sum += max(m - inp[i].l, 1ll * 0); cnt++; } if (cnt == (n + 1) / 2) { break; } } if (cnt == (n + 1) / 2 && sum <= s) { return true; } else { return false; } } int main() { int t; cin >> t; while (t--) { cin >> n >> s; for (int i = 0; i < n; i++) { cin >> inp[i].l >> inp[i].r; } long long int l = 0, r = s, ans; sort(inp, inp + n, cmp); while (l <= r) { long long int mid = (l + r) / 2; if (check(mid)) { ans = mid; l = mid + 1; } else { r = mid - 1; } } cout << ans << endl; } } ```
#include <bits/stdc++.h> using namespace std; int q, n; long long s; class emp { public: long long l, r; emp() {} emp(long long l, long long r) : l(l), r(r) {} bool operator<(const emp& other) const { if (l != other.l) return l < other.l; return r < other.r; } }; emp E[200010]; bool f(long long med) { int l = 0, g = 0, e = 0; long long sum = 0; for (int i = 0; i < n; i++) { if (E[i].r < med) { l++; sum += E[i].l; } if (E[i].l > med) { g++; sum += E[i].l; } } if (l > n / 2 || g > n / 2 + 1) return false; for (int i = 0; i < n; i++) { if (E[i].l <= med && med <= E[i].r) { if (l < n / 2) sum += E[i].l, l++; else sum += med, g++; } } if (l != n / 2 || g != n / 2 + 1) return false; return sum <= s; } int main() { scanf("%d", &q); while (q--) { scanf("%d %lld", &n, &s); for (int i = 0; i < n; i++) scanf("%lld %lld", &E[i].l, &E[i].r); sort(E, E + n); long long lo = E[n / 2].l, hi = s, mid; while (lo <= hi) { mid = (lo + hi) / 2; if (f(mid)) lo = mid + 1; else hi = mid - 1; } printf("%lld\n", hi); } return 0; }
### Prompt Please create a solution in Cpp to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int q, n; long long s; class emp { public: long long l, r; emp() {} emp(long long l, long long r) : l(l), r(r) {} bool operator<(const emp& other) const { if (l != other.l) return l < other.l; return r < other.r; } }; emp E[200010]; bool f(long long med) { int l = 0, g = 0, e = 0; long long sum = 0; for (int i = 0; i < n; i++) { if (E[i].r < med) { l++; sum += E[i].l; } if (E[i].l > med) { g++; sum += E[i].l; } } if (l > n / 2 || g > n / 2 + 1) return false; for (int i = 0; i < n; i++) { if (E[i].l <= med && med <= E[i].r) { if (l < n / 2) sum += E[i].l, l++; else sum += med, g++; } } if (l != n / 2 || g != n / 2 + 1) return false; return sum <= s; } int main() { scanf("%d", &q); while (q--) { scanf("%d %lld", &n, &s); for (int i = 0; i < n; i++) scanf("%lld %lld", &E[i].l, &E[i].r); sort(E, E + n); long long lo = E[n / 2].l, hi = s, mid; while (lo <= hi) { mid = (lo + hi) / 2; if (f(mid)) lo = mid + 1; else hi = mid - 1; } printf("%lld\n", hi); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; struct node { int l; int r; } a[N]; bool cmp(node a, node b) { if (a.l == b.l) return a.r > b.r; return a.l > b.l; } long long n, s; int check(int x) { int k = n / 2, cnt = 0; long long sum = 0; for (int i = 0; i < n; i++) { if (a[i].r >= x && cnt <= k) { if (a[i].l <= x) { sum += x; } else { sum += a[i].l; } cnt++; } else { sum += a[i].l; } } return (sum <= s) && cnt == k + 1; } int main() { int t; scanf("%d", &t); while (t--) { scanf("%lld%lld", &n, &s); for (int i = 0; i < n; i++) { scanf("%d%d", &a[i].l, &a[i].r); } sort(a, a + n, cmp); int l = 1, r = 1e9; while (l < r) { int mid = (l + r + 1) >> 1; if (check(mid)) { l = mid; } else { r = mid - 1; } } printf("%d\n", l); } return 0; }
### Prompt Please formulate a cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; struct node { int l; int r; } a[N]; bool cmp(node a, node b) { if (a.l == b.l) return a.r > b.r; return a.l > b.l; } long long n, s; int check(int x) { int k = n / 2, cnt = 0; long long sum = 0; for (int i = 0; i < n; i++) { if (a[i].r >= x && cnt <= k) { if (a[i].l <= x) { sum += x; } else { sum += a[i].l; } cnt++; } else { sum += a[i].l; } } return (sum <= s) && cnt == k + 1; } int main() { int t; scanf("%d", &t); while (t--) { scanf("%lld%lld", &n, &s); for (int i = 0; i < n; i++) { scanf("%d%d", &a[i].l, &a[i].r); } sort(a, a + n, cmp); int l = 1, r = 1e9; while (l < r) { int mid = (l + r + 1) >> 1; if (check(mid)) { l = mid; } else { r = mid - 1; } } printf("%d\n", l); } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long t, n, S, s, up, down, mid, mx, mn, a, b, k, l, r, ans, raod; pair<long long, long long> p[300005]; int main() { cin >> t; while (t--) { cin >> n >> S; mn = 1e10; mx = 0; for (k = 1; k <= n; k++) { cin >> a >> b; p[k].first = a; mn = min(mn, a); p[k].second = b; mx = max(mx, b); } ans = 0; sort(p + 1, p + n + 1); l = mn; r = mx; while (l <= r) { long long mid = (l + r + 1) / 2; up = 0; down = 0; s = 0; for (k = 1; k <= n; k++) if (p[k].first > mid) up++; else if (p[k].second < mid) down++; if (up >= n / 2 + 1) { l = mid + 1; } else if (down >= n / 2 + 1) r = mid - 1; else { raod = 0; for (k = n; k >= 1; k--) { if (raod <= n / 2 & p[k].second >= mid) { raod++; s += max(mid, p[k].first); } else s += p[k].first; } if (s <= S) { ans = mid; l = mid + 1; } else r = mid - 1; } } cout << ans << endl; } }
### Prompt Please formulate a cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long t, n, S, s, up, down, mid, mx, mn, a, b, k, l, r, ans, raod; pair<long long, long long> p[300005]; int main() { cin >> t; while (t--) { cin >> n >> S; mn = 1e10; mx = 0; for (k = 1; k <= n; k++) { cin >> a >> b; p[k].first = a; mn = min(mn, a); p[k].second = b; mx = max(mx, b); } ans = 0; sort(p + 1, p + n + 1); l = mn; r = mx; while (l <= r) { long long mid = (l + r + 1) / 2; up = 0; down = 0; s = 0; for (k = 1; k <= n; k++) if (p[k].first > mid) up++; else if (p[k].second < mid) down++; if (up >= n / 2 + 1) { l = mid + 1; } else if (down >= n / 2 + 1) r = mid - 1; else { raod = 0; for (k = n; k >= 1; k--) { if (raod <= n / 2 & p[k].second >= mid) { raod++; s += max(mid, p[k].first); } else s += p[k].first; } if (s <= S) { ans = mid; l = mid + 1; } else r = mid - 1; } } cout << ans << endl; } } ```
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > v; bool check(long long mid, int cnt, long long total) { int i, c = 0, c1 = 0; long long sum = 0; vector<long long> tmp; for (i = 0; i < v.size(); i++) { if (v[i].second < mid) sum += v[i].first; else if (mid < v[i].first) { c++; sum += v[i].first; } else { c1++; tmp.push_back(v[i].first); } } int rem = max(0, cnt - c); if (c1 < rem) return 0; sort(tmp.begin(), tmp.end()); for (i = 0; i < c1; i++) { if (i < c1 - rem) sum += min(tmp[i], mid); else sum += mid; } if (total < sum) return 0; return 1; } void solve() { int i, t; long long total; cin >> t >> total; v.clear(); for (i = 0; i < t; i++) { int l, r; cin >> l >> r; v.push_back({l, r}); } int cnt = (t + 1) / 2; long long hi, lo, mid; lo = 1, hi = total; while (hi - lo >= 4) { mid = (hi + lo) / 2; if (check(mid, cnt, total)) lo = mid; else hi = mid; } for (i = hi; i >= lo; i--) { if (check(i, cnt, total)) { cout << i << "\n"; break; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int test; cin >> test; while (test--) solve(); return 0; }
### Prompt Please create a solution in Cpp to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<pair<int, int> > v; bool check(long long mid, int cnt, long long total) { int i, c = 0, c1 = 0; long long sum = 0; vector<long long> tmp; for (i = 0; i < v.size(); i++) { if (v[i].second < mid) sum += v[i].first; else if (mid < v[i].first) { c++; sum += v[i].first; } else { c1++; tmp.push_back(v[i].first); } } int rem = max(0, cnt - c); if (c1 < rem) return 0; sort(tmp.begin(), tmp.end()); for (i = 0; i < c1; i++) { if (i < c1 - rem) sum += min(tmp[i], mid); else sum += mid; } if (total < sum) return 0; return 1; } void solve() { int i, t; long long total; cin >> t >> total; v.clear(); for (i = 0; i < t; i++) { int l, r; cin >> l >> r; v.push_back({l, r}); } int cnt = (t + 1) / 2; long long hi, lo, mid; lo = 1, hi = total; while (hi - lo >= 4) { mid = (hi + lo) / 2; if (check(mid, cnt, total)) lo = mid; else hi = mid; } for (i = hi; i >= lo; i--) { if (check(i, cnt, total)) { cout << i << "\n"; break; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int test; cin >> test; while (test--) solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; int takemod(int a, int mod) { a %= mod; if (a < 0) a += mod; return a; } int fast_exp(int base, int expo, int mod) { int res = 1; while (expo > 0) { if (expo & 1) res = (res * base) % mod; base = (base * base) % mod; expo >>= 1; } return res; } int modinv(int a, int mod) { return takemod(fast_exp(takemod(a, mod), mod - 2, mod), mod); } const long long N = 1e15 + 5, A = 1e5 + 10, md = 1e9 + 7; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { long long n, s; cin >> n >> s; vector<pair<int, int> > a; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; a.push_back(make_pair(x, y)); s -= x; } int k = (n + 1) / 2; long long l = 1, r = N; while (l <= r) { long long m = (l + r) / 2; vector<long long> b; for (int i = 0; i < n; i++) { if (a[i].second >= m) { b.push_back(a[i].first); } } bool check = 0; if (b.size() >= k) { sort(b.begin(), b.end()); long long cost = 0; for (int i = 0; i < k; i++) { cost += max(0ll, m - b[b.size() - i - 1]); } if (cost <= s) { check = 1; } } if (check) { l = m + 1; } else { r = m - 1; } } cout << l - 1 << endl; } }
### Prompt In cpp, your task is to solve the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int takemod(int a, int mod) { a %= mod; if (a < 0) a += mod; return a; } int fast_exp(int base, int expo, int mod) { int res = 1; while (expo > 0) { if (expo & 1) res = (res * base) % mod; base = (base * base) % mod; expo >>= 1; } return res; } int modinv(int a, int mod) { return takemod(fast_exp(takemod(a, mod), mod - 2, mod), mod); } const long long N = 1e15 + 5, A = 1e5 + 10, md = 1e9 + 7; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { long long n, s; cin >> n >> s; vector<pair<int, int> > a; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; a.push_back(make_pair(x, y)); s -= x; } int k = (n + 1) / 2; long long l = 1, r = N; while (l <= r) { long long m = (l + r) / 2; vector<long long> b; for (int i = 0; i < n; i++) { if (a[i].second >= m) { b.push_back(a[i].first); } } bool check = 0; if (b.size() >= k) { sort(b.begin(), b.end()); long long cost = 0; for (int i = 0; i < k; i++) { cost += max(0ll, m - b[b.size() - i - 1]); } if (cost <= s) { check = 1; } } if (check) { l = m + 1; } else { r = m - 1; } } cout << l - 1 << endl; } } ```
#include <bits/stdc++.h> using namespace std; pair<long long, long long> ara[200005]; int n; long long s; bool cheak(long long m) { long long rem = s; for (int i = 0; i < n; i++) rem -= ara[i].first; int p = (n + 1) >> 1; for (int i = 0; i < n && p; i++) { if (ara[i].first >= m) p--; else if (ara[i].second >= m && (m - ara[i].first) <= rem) rem -= (m - ara[i].first), p--; } return (!p); } long long mn, mx; long long bs() { long long lo = mn, hi = mx, res; while (lo <= hi) { long long mid = (lo + hi) >> 1; if (cheak(mid)) res = mid, lo = mid + 1; else hi = mid - 1; } return res; } int main() { int ts; scanf("%d", &ts); while (ts--) { scanf("%d %lld", &n, &s); mn = 1000000009; mx = 0; for (int i = 0; i < n; i++) scanf("%lld %lld", &ara[i].first, &ara[i].second), mn = min(mn, ara[i].first), mx = max(mx, ara[i].second); sort(ara, ara + n, greater<pair<long long, long long> >()); printf("%lld\n", bs()); } return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; pair<long long, long long> ara[200005]; int n; long long s; bool cheak(long long m) { long long rem = s; for (int i = 0; i < n; i++) rem -= ara[i].first; int p = (n + 1) >> 1; for (int i = 0; i < n && p; i++) { if (ara[i].first >= m) p--; else if (ara[i].second >= m && (m - ara[i].first) <= rem) rem -= (m - ara[i].first), p--; } return (!p); } long long mn, mx; long long bs() { long long lo = mn, hi = mx, res; while (lo <= hi) { long long mid = (lo + hi) >> 1; if (cheak(mid)) res = mid, lo = mid + 1; else hi = mid - 1; } return res; } int main() { int ts; scanf("%d", &ts); while (ts--) { scanf("%d %lld", &n, &s); mn = 1000000009; mx = 0; for (int i = 0; i < n; i++) scanf("%lld %lld", &ara[i].first, &ara[i].second), mn = min(mn, ara[i].first), mx = max(mx, ara[i].second); sort(ara, ara + n, greater<pair<long long, long long> >()); printf("%lld\n", bs()); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int INF = 1001001001; const long long LINF = 1e17; const double pi = 3.1415926535897932; const string endstr = "\n"; template <typename T> T gcd(T a, T b) { return (a == 0) ? b : gcd(b % a, a); } template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; } bool p_comp_fs(const pair<long long, long long> p1, const pair<long long, long long> p2) { return p1.first < p2.first; }; bool p_comp_fg(const pair<long long, long long> p1, const pair<long long, long long> p2) { return p1.first > p2.first; }; bool p_comp_ss(const pair<long long, long long> p1, const pair<long long, long long> p2) { return p1.second < p2.second; }; bool p_comp_sg(const pair<long long, long long> p1, const pair<long long, long long> p2) { return p1.second > p2.second; }; template <typename T> vector<T> uniquen(vector<T> vec) { sort((vec).begin(), (vec).end()); vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; } inline long long popcnt(long long x) { return __builtin_popcountll((unsigned long long)x); }; template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } bool check(long long x, long long s, vector<pair<long long, long long> > &P) { long long N = P.size(); long long lcnt = 0, rcnt = 0, lsum = 0, rsum = 0; vector<pair<long long, long long> > midx; for (auto p : P) { if (p.second < x) { lcnt++; lsum += p.first; } else if (p.first > x) { rcnt++; rsum += p.first; } else { midx.push_back(p); } } if (lcnt > N / 2) return false; if (rcnt > N / 2) return true; long long sum = lsum + rsum; for (long long i = 0; i < N / 2 - lcnt; i++) sum += midx[i].first; long long rem = N - (lcnt + rcnt + (N / 2 - lcnt)); sum += x * rem; return sum <= s; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); long long T; cin >> T; for (long long _ = 0; _ < T; _++) { long long N, S; cin >> N >> S; vector<pair<long long, long long> > PS(N); for (long long i = 0; i < N; i++) cin >> PS[i].first >> PS[i].second; sort((PS).begin(), (PS).end()); long long ok = -1, ng = LINF; while (abs(ok - ng) > 1) { long long mid = (ok + ng) / 2; if (check(mid, S, PS)) ok = mid; else ng = mid; } cout << ok << endstr; } return 0; }
### Prompt Generate a CPP solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int INF = 1001001001; const long long LINF = 1e17; const double pi = 3.1415926535897932; const string endstr = "\n"; template <typename T> T gcd(T a, T b) { return (a == 0) ? b : gcd(b % a, a); } template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; } bool p_comp_fs(const pair<long long, long long> p1, const pair<long long, long long> p2) { return p1.first < p2.first; }; bool p_comp_fg(const pair<long long, long long> p1, const pair<long long, long long> p2) { return p1.first > p2.first; }; bool p_comp_ss(const pair<long long, long long> p1, const pair<long long, long long> p2) { return p1.second < p2.second; }; bool p_comp_sg(const pair<long long, long long> p1, const pair<long long, long long> p2) { return p1.second > p2.second; }; template <typename T> vector<T> uniquen(vector<T> vec) { sort((vec).begin(), (vec).end()); vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; } inline long long popcnt(long long x) { return __builtin_popcountll((unsigned long long)x); }; template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } bool check(long long x, long long s, vector<pair<long long, long long> > &P) { long long N = P.size(); long long lcnt = 0, rcnt = 0, lsum = 0, rsum = 0; vector<pair<long long, long long> > midx; for (auto p : P) { if (p.second < x) { lcnt++; lsum += p.first; } else if (p.first > x) { rcnt++; rsum += p.first; } else { midx.push_back(p); } } if (lcnt > N / 2) return false; if (rcnt > N / 2) return true; long long sum = lsum + rsum; for (long long i = 0; i < N / 2 - lcnt; i++) sum += midx[i].first; long long rem = N - (lcnt + rcnt + (N / 2 - lcnt)); sum += x * rem; return sum <= s; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); long long T; cin >> T; for (long long _ = 0; _ < T; _++) { long long N, S; cin >> N >> S; vector<pair<long long, long long> > PS(N); for (long long i = 0; i < N; i++) cin >> PS[i].first >> PS[i].second; sort((PS).begin(), (PS).end()); long long ok = -1, ng = LINF; while (abs(ok - ng) > 1) { long long mid = (ok + ng) / 2; if (check(mid, S, PS)) ok = mid; else ng = mid; } cout << ok << endstr; } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long int a[200010][2]; bool check(long long int m, long long int n, long long int s) { long long int aa = 0, bb = 0; long long int sal = 0; vector<pair<int, int>> v; for (int i = 0; i < n; i++) { if (a[i][1] < m) aa++, sal = sal + a[i][0]; else if (a[i][0] >= m) bb++, sal = sal + a[i][0]; else v.push_back({a[i][0], a[i][1]}); } long long int p = (n / 2) - aa; long long int q = (n / 2) + 1 - bb; if (bb >= ((n / 2) + 1)) { for (long long int i = 0; i < v.size(); i++) sal = sal + v[i].first; if (sal <= s) return true; else return false; } else if (p >= 0 && q >= 0) { sort(v.begin(), v.end()); for (int i = 0; i < p; i++) sal = sal + v[i].first; sal = sal + (q * m); if (sal <= s) return true; else return false; } else return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { long long int n, s; cin >> n >> s; long long int l = INT_MAX, r = 0; for (int i = 0; i < n; i++) { cin >> a[i][0] >> a[i][1]; r = max(r, a[i][1]); l = min(l, a[i][0]); } long long int ans = 0; while (l <= r) { long long int m = (l + r) / 2; if (check(m, n, s)) ans = m, l = m + 1; else r = m - 1; } cout << ans << endl; } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long int a[200010][2]; bool check(long long int m, long long int n, long long int s) { long long int aa = 0, bb = 0; long long int sal = 0; vector<pair<int, int>> v; for (int i = 0; i < n; i++) { if (a[i][1] < m) aa++, sal = sal + a[i][0]; else if (a[i][0] >= m) bb++, sal = sal + a[i][0]; else v.push_back({a[i][0], a[i][1]}); } long long int p = (n / 2) - aa; long long int q = (n / 2) + 1 - bb; if (bb >= ((n / 2) + 1)) { for (long long int i = 0; i < v.size(); i++) sal = sal + v[i].first; if (sal <= s) return true; else return false; } else if (p >= 0 && q >= 0) { sort(v.begin(), v.end()); for (int i = 0; i < p; i++) sal = sal + v[i].first; sal = sal + (q * m); if (sal <= s) return true; else return false; } else return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { long long int n, s; cin >> n >> s; long long int l = INT_MAX, r = 0; for (int i = 0; i < n; i++) { cin >> a[i][0] >> a[i][1]; r = max(r, a[i][1]); l = min(l, a[i][0]); } long long int ans = 0; while (l <= r) { long long int m = (l + r) / 2; if (check(m, n, s)) ans = m, l = m + 1; else r = m - 1; } cout << ans << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 233333; long long suml = 0, n, s; struct node { int l, r; } e[maxn]; bool cmp(node a, node b) { if (a.l == b.l) return a.r < b.r; return a.l < b.l; } int vis[maxn]; bool check(int x) { long long now = suml; int cnt = 0; for (int i = n; i >= 1; i--) { if (e[i].l >= x) cnt++; else if (e[i].r >= x) { cnt++; now += (x - e[i].l); } if (cnt == (n + 1) / 2) { if (now <= s) return true; else return false; } } return false; } int main() { int T; cin >> T; while (T--) { cin >> n >> s; int l, r = -999; suml = 0; for (int i = 1; i <= n; i++) { cin >> e[i].l >> e[i].r; r = max(r, e[i].r); suml += e[i].l; } sort(e + 1, e + 1 + n, cmp); int mid = (n + 1) / 2; l = e[mid].l; int ct = 1; while (l <= r) { mid = (l + r) >> 1; if (check(mid)) l = mid + 1; else r = mid - 1; } int ans = r; if (check(l)) ans = max(ans, l); if (check(l + 1)) ans = max(ans, l + 1); if (check(mid)) ans = max(ans, mid); if (check(mid + 1)) ans = max(ans, mid + 1); if (check(r + 1)) ans = max(ans, r + 1); cout << ans << endl; } return 0; }
### Prompt Create a solution in CPP for the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 233333; long long suml = 0, n, s; struct node { int l, r; } e[maxn]; bool cmp(node a, node b) { if (a.l == b.l) return a.r < b.r; return a.l < b.l; } int vis[maxn]; bool check(int x) { long long now = suml; int cnt = 0; for (int i = n; i >= 1; i--) { if (e[i].l >= x) cnt++; else if (e[i].r >= x) { cnt++; now += (x - e[i].l); } if (cnt == (n + 1) / 2) { if (now <= s) return true; else return false; } } return false; } int main() { int T; cin >> T; while (T--) { cin >> n >> s; int l, r = -999; suml = 0; for (int i = 1; i <= n; i++) { cin >> e[i].l >> e[i].r; r = max(r, e[i].r); suml += e[i].l; } sort(e + 1, e + 1 + n, cmp); int mid = (n + 1) / 2; l = e[mid].l; int ct = 1; while (l <= r) { mid = (l + r) >> 1; if (check(mid)) l = mid + 1; else r = mid - 1; } int ans = r; if (check(l)) ans = max(ans, l); if (check(l + 1)) ans = max(ans, l + 1); if (check(mid)) ans = max(ans, mid); if (check(mid + 1)) ans = max(ans, mid + 1); if (check(r + 1)) ans = max(ans, r + 1); cout << ans << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 5; struct Node { int L, R; bool operator<(const Node &other) const { return L < other.L; } }; int N, H; long long S; vector<Node> v; bool possible(int median) { long long sum = 0; for (Node &s : v) sum += s.L; int count = 0; for (int i = N - 1; i >= 0 && count < H; i--) if (v[i].R >= median) { sum += max(median - v[i].L, 0); count++; } return count == H && sum <= S; } int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int T; cin >> T; while (T--) { cin >> N >> S; H = (N + 1) / 2; v.resize(N); for (Node &s : v) cin >> s.L >> s.R; sort(v.begin(), v.end()); int l = 0, r = INF, ans; while (l <= r) { int mid = (l + r) / 2; if (possible(mid)) { ans = mid; l = mid + 1; } else r = mid - 1; } cout << ans << '\n'; } return 0; }
### Prompt Develop a solution in CPP to the problem described below: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 5; struct Node { int L, R; bool operator<(const Node &other) const { return L < other.L; } }; int N, H; long long S; vector<Node> v; bool possible(int median) { long long sum = 0; for (Node &s : v) sum += s.L; int count = 0; for (int i = N - 1; i >= 0 && count < H; i--) if (v[i].R >= median) { sum += max(median - v[i].L, 0); count++; } return count == H && sum <= S; } int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int T; cin >> T; while (T--) { cin >> N >> S; H = (N + 1) / 2; v.resize(N); for (Node &s : v) cin >> s.L >> s.R; sort(v.begin(), v.end()); int l = 0, r = INF, ans; while (l <= r) { int mid = (l + r) / 2; if (possible(mid)) { ans = mid; l = mid + 1; } else r = mid - 1; } cout << ans << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int N, N2; long long S; const int MAXN = 2e5 + 15; pair<long long, long long> A[MAXN]; long long I[MAXN]; bool check(long long m) { int c2 = 0, n = 0; long long sum = 0; for (int i = 0; i < N; ++i) if (A[i].second < m) { sum += A[i].first; } else if (A[i].first >= m) { ++c2; sum += A[i].first; } else { I[n++] = A[i].first; } int need = max(0, N2 - c2); if (need > n) return false; for (int i = 0; i < n; ++i) if (i < n - need) sum += I[i]; else sum += m; return sum <= S; } int main() { ios::sync_with_stdio(false); int T; cin >> T; while (T--) { cin >> N >> S; N2 = (N + 1) / 2; long long bb = 1, ee = 1e9 + 1; for (int i = 0; i < N; ++i) { long long a, b; cin >> a >> b; A[i] = make_pair(a, b); } sort(A, A + N); while (ee - bb > 1) { long long m = (bb + ee) / 2; if (check(m)) bb = m; else ee = m; } cout << bb << endl; } return 0; }
### Prompt Generate a cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int N, N2; long long S; const int MAXN = 2e5 + 15; pair<long long, long long> A[MAXN]; long long I[MAXN]; bool check(long long m) { int c2 = 0, n = 0; long long sum = 0; for (int i = 0; i < N; ++i) if (A[i].second < m) { sum += A[i].first; } else if (A[i].first >= m) { ++c2; sum += A[i].first; } else { I[n++] = A[i].first; } int need = max(0, N2 - c2); if (need > n) return false; for (int i = 0; i < n; ++i) if (i < n - need) sum += I[i]; else sum += m; return sum <= S; } int main() { ios::sync_with_stdio(false); int T; cin >> T; while (T--) { cin >> N >> S; N2 = (N + 1) / 2; long long bb = 1, ee = 1e9 + 1; for (int i = 0; i < N; ++i) { long long a, b; cin >> a >> b; A[i] = make_pair(a, b); } sort(A, A + N); while (ee - bb > 1) { long long m = (bb + ee) / 2; if (check(m)) bb = m; else ee = m; } cout << bb << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int q; scanf("%d", &q); while (q--) { int n; long long s; scanf("%d %I64d", &n, &s); long long deb = 0, fin = s; int l[n], r[n]; for (int i = 0; i < n; i++) { scanf("%d %d", &l[i], &r[i]); s -= l[i]; } while (deb < fin) { long long mid = (deb + fin + 1) / 2; priority_queue<int> suiv; for (int i = 0; i < n; i++) { if (r[i] >= mid) suiv.push(l[i]); } if ((int)suiv.size() < (n + 1) / 2) { fin = mid - 1; continue; } long long spend = 0; for (int i = 0; i < (n + 1) / 2; i++) { spend += max(0ll, mid - suiv.top()); suiv.pop(); } if (spend <= s) deb = mid; else fin = mid - 1; } printf("%I64d\n", deb); } return 0; }
### Prompt Your task is to create a CPP solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int q; scanf("%d", &q); while (q--) { int n; long long s; scanf("%d %I64d", &n, &s); long long deb = 0, fin = s; int l[n], r[n]; for (int i = 0; i < n; i++) { scanf("%d %d", &l[i], &r[i]); s -= l[i]; } while (deb < fin) { long long mid = (deb + fin + 1) / 2; priority_queue<int> suiv; for (int i = 0; i < n; i++) { if (r[i] >= mid) suiv.push(l[i]); } if ((int)suiv.size() < (n + 1) / 2) { fin = mid - 1; continue; } long long spend = 0; for (int i = 0; i < (n + 1) / 2; i++) { spend += max(0ll, mid - suiv.top()); suiv.pop(); } if (spend <= s) deb = mid; else fin = mid - 1; } printf("%I64d\n", deb); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9 + 200ll; bool can(vector<pair<int, int>> a, long long tar, int n, long long s) { int cnt = 0; long long sum = 0ll; vector<int> use; for (int i = 0; i < n; ++i) { if (a[i].second < tar) { sum += (long long)a[i].first; } else if (a[i].first >= tar) { sum += (long long)a[i].first; cnt++; } else { use.push_back(a[i].first); } } int want = max(0, (n + 1) / 2 - cnt); if (want > use.size()) return false; for (int i = 0; i < use.size(); ++i) { if (i < use.size() - want) { sum += (long long)use[i]; } else { sum += tar; } } return sum <= s; } int main() { ios::sync_with_stdio(0); int t; cin >> t; while (t--) { int n; long long s; cin >> n >> s; vector<pair<int, int>> a(n); for (int i = 0; i < n; ++i) { cin >> a[i].first >> a[i].second; } sort(a.begin(), a.end()); long long ans = 0ll; long long l = 0ll, r = INF; while (l <= r) { long long mid = (l + r) / 2ll; if (can(a, mid, n, s)) { l = mid + 1ll; ans = mid; } else { r = mid - 1ll; } } cout << ans << "\n"; } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long INF = 1e9 + 200ll; bool can(vector<pair<int, int>> a, long long tar, int n, long long s) { int cnt = 0; long long sum = 0ll; vector<int> use; for (int i = 0; i < n; ++i) { if (a[i].second < tar) { sum += (long long)a[i].first; } else if (a[i].first >= tar) { sum += (long long)a[i].first; cnt++; } else { use.push_back(a[i].first); } } int want = max(0, (n + 1) / 2 - cnt); if (want > use.size()) return false; for (int i = 0; i < use.size(); ++i) { if (i < use.size() - want) { sum += (long long)use[i]; } else { sum += tar; } } return sum <= s; } int main() { ios::sync_with_stdio(0); int t; cin >> t; while (t--) { int n; long long s; cin >> n >> s; vector<pair<int, int>> a(n); for (int i = 0; i < n; ++i) { cin >> a[i].first >> a[i].second; } sort(a.begin(), a.end()); long long ans = 0ll; long long l = 0ll, r = INF; while (l <= r) { long long mid = (l + r) / 2ll; if (can(a, mid, n, s)) { l = mid + 1ll; ans = mid; } else { r = mid - 1ll; } } cout << ans << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; pair<long long, long long> a[200010]; long long n, s; bool ok(long long x) { long long cnt = 0, left = s; for (long long i = 0; i < n; i++) left -= a[i].first; for (long long i = 0; i < n; i++) { if (a[i].first >= x) cnt++; else if (a[i].second >= x) { if (cnt <= n / 2) if (left - (x - a[i].first) >= 0) left -= (x - a[i].first), cnt++; } } return (cnt > n / 2); } signed main() { long long t; cin >> t; while (t--) { cin >> n >> s; for (long long i = 0; i < n; i++) cin >> a[i].first >> a[i].second; sort(a, a + n, greater<pair<long long, long long>>()); long long ub = 1000000001, lb = 0; while (ub - lb > 1) { long long mid = ub + lb >> 1; if (ok(mid)) lb = mid; else ub = mid; } cout << lb << endl; } }
### Prompt Please provide a CPP coded solution to the problem described below: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; pair<long long, long long> a[200010]; long long n, s; bool ok(long long x) { long long cnt = 0, left = s; for (long long i = 0; i < n; i++) left -= a[i].first; for (long long i = 0; i < n; i++) { if (a[i].first >= x) cnt++; else if (a[i].second >= x) { if (cnt <= n / 2) if (left - (x - a[i].first) >= 0) left -= (x - a[i].first), cnt++; } } return (cnt > n / 2); } signed main() { long long t; cin >> t; while (t--) { cin >> n >> s; for (long long i = 0; i < n; i++) cin >> a[i].first >> a[i].second; sort(a, a + n, greater<pair<long long, long long>>()); long long ub = 1000000001, lb = 0; while (ub - lb > 1) { long long mid = ub + lb >> 1; if (ok(mid)) lb = mid; else ub = mid; } cout << lb << endl; } } ```
#include <bits/stdc++.h> using namespace std; long long n, s; vector<pair<long long, long long> > arr; bool can(long long x) { long long ans = 0; vector<pair<long long, long long> > todo; for (long long i = 0; i < n; i++) { if (arr[i].second < x) todo.push_back(arr[i]); } for (long long i = 0; i < n; i++) { if (arr[i].second >= x) todo.push_back(arr[i]); } long long ct = 0; for (ct = 0; ct < n; ct++) { pair<long long, long long> aux = todo[ct]; if (ct < n / 2) { ans += aux.first; } if (ct >= n / 2) { if (aux.second < x) return false; ans += max(x, aux.first); } } return ans <= s; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long T; cin >> T; while (T--) { cin >> n >> s; arr.clear(); arr.resize(n); for (long long i = 0; i < n; i++) { cin >> arr[i].first >> arr[i].second; } sort(arr.begin(), arr.end()); long long low = 1, high = s, ans = 0, mid; while (low <= high) { mid = (low + high) / 2; if (can(mid)) { ans = mid; low = mid + 1; } else { high = mid - 1; } } cout << ans << "\n"; } return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, s; vector<pair<long long, long long> > arr; bool can(long long x) { long long ans = 0; vector<pair<long long, long long> > todo; for (long long i = 0; i < n; i++) { if (arr[i].second < x) todo.push_back(arr[i]); } for (long long i = 0; i < n; i++) { if (arr[i].second >= x) todo.push_back(arr[i]); } long long ct = 0; for (ct = 0; ct < n; ct++) { pair<long long, long long> aux = todo[ct]; if (ct < n / 2) { ans += aux.first; } if (ct >= n / 2) { if (aux.second < x) return false; ans += max(x, aux.first); } } return ans <= s; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long T; cin >> T; while (T--) { cin >> n >> s; arr.clear(); arr.resize(n); for (long long i = 0; i < n; i++) { cin >> arr[i].first >> arr[i].second; } sort(arr.begin(), arr.end()); long long low = 1, high = s, ans = 0, mid; while (low <= high) { mid = (low + high) / 2; if (can(mid)) { ans = mid; low = mid + 1; } else { high = mid - 1; } } cout << ans << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int N = 2e5 + 10; int n; long long s; struct node { int l, r; bool operator<(const node& a) const { return l < a.l; } } p[N]; vector<int> g; int judge(long long x) { g.clear(); long long sum = 0, cnt1 = 0, cnt2 = 0; for (int i = 1; i <= n; i++) { if (p[i].r < x) sum += p[i].l, cnt1++; else if (p[i].l > x) sum += p[i].l, cnt2++; else g.push_back(i); } if (cnt1 > n / 2) return 0; if (cnt2 > n / 2) return 1; for (int i = 0; i < n / 2 - cnt1; i++) { sum += p[g[i]].l; } sum += (n / 2 + 1 - cnt2) * x; if (sum <= s) return 1; return 0; } int main() { int t; scanf("%d", &t); while (t--) { scanf("%d%lld", &n, &s); for (int i = 1; i <= n; i++) scanf("%d%d", &p[i].l, &p[i].r); sort(p + 1, p + n + 1); long long left = 0, right = 1e9, mid, ans; while (right >= left) { mid = (left + right) >> 1; if (judge(mid)) ans = mid, left = mid + 1; else right = mid - 1; } printf("%lld\n", ans); } return 0; }
### Prompt In CPP, your task is to solve the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int N = 2e5 + 10; int n; long long s; struct node { int l, r; bool operator<(const node& a) const { return l < a.l; } } p[N]; vector<int> g; int judge(long long x) { g.clear(); long long sum = 0, cnt1 = 0, cnt2 = 0; for (int i = 1; i <= n; i++) { if (p[i].r < x) sum += p[i].l, cnt1++; else if (p[i].l > x) sum += p[i].l, cnt2++; else g.push_back(i); } if (cnt1 > n / 2) return 0; if (cnt2 > n / 2) return 1; for (int i = 0; i < n / 2 - cnt1; i++) { sum += p[g[i]].l; } sum += (n / 2 + 1 - cnt2) * x; if (sum <= s) return 1; return 0; } int main() { int t; scanf("%d", &t); while (t--) { scanf("%d%lld", &n, &s); for (int i = 1; i <= n; i++) scanf("%d%d", &p[i].l, &p[i].r); sort(p + 1, p + n + 1); long long left = 0, right = 1e9, mid, ans; while (right >= left) { mid = (left + right) >> 1; if (judge(mid)) ans = mid, left = mid + 1; else right = mid - 1; } printf("%lld\n", ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; struct node { int l, r; friend bool operator<(node a, node b) { return a.l < b.l; } } ee[200005]; int get(int n, int x, long long k) { long long sum = 0; int cnt = n / 2 + 1; for (int i = n; i >= 1; i--) { if (ee[i].l <= x && ee[i].r >= x && cnt) { sum += x; cnt--; } else { sum += ee[i].l; if (ee[i].l >= x) cnt--; } } if (sum > k || cnt > 0) return 0; else return 1; } int main() { int n, t; long long k; scanf("%d", &t); while (t--) { scanf("%d%lld", &n, &k); int maxs = 0; for (int i = 1; i <= n; i++) { scanf("%d%d", &ee[i].l, &ee[i].r); maxs = max(maxs, max(ee[i].l, ee[i].r)); } sort(ee + 1, ee + n + 1); int l = ee[n / 2 + 1].l, r = maxs, ans = l; while (l <= r) { int mid = (l + r) >> 1; if (get(n, mid, k)) { l = mid + 1; ans = mid; } else { r = mid - 1; } } printf("%d\n", ans); } return 0; }
### Prompt Generate a Cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct node { int l, r; friend bool operator<(node a, node b) { return a.l < b.l; } } ee[200005]; int get(int n, int x, long long k) { long long sum = 0; int cnt = n / 2 + 1; for (int i = n; i >= 1; i--) { if (ee[i].l <= x && ee[i].r >= x && cnt) { sum += x; cnt--; } else { sum += ee[i].l; if (ee[i].l >= x) cnt--; } } if (sum > k || cnt > 0) return 0; else return 1; } int main() { int n, t; long long k; scanf("%d", &t); while (t--) { scanf("%d%lld", &n, &k); int maxs = 0; for (int i = 1; i <= n; i++) { scanf("%d%d", &ee[i].l, &ee[i].r); maxs = max(maxs, max(ee[i].l, ee[i].r)); } sort(ee + 1, ee + n + 1); int l = ee[n / 2 + 1].l, r = maxs, ans = l; while (l <= r) { int mid = (l + r) >> 1; if (get(n, mid, k)) { l = mid + 1; ans = mid; } else { r = mid - 1; } } printf("%d\n", ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long MOD = (long long)(1e9 + 7); const long long N = 200007; vector<pair<long long, long long>> v; long long s; int n; int sure(long long median) { long long cnt1 = 0; long long cnt2 = 0; long long cnt3 = 0; long long sum = 0; vector<long long> tmp; for (auto ele : v) { long long l = ele.first; long long r = ele.second; if (r < median) { cnt1 += 1; sum += l; } else if (l > median) { cnt3 += 1; sum += l; } else { cnt2 += 1; tmp.push_back(l); } } long long mn = (n - 1) / 2; if (cnt1 > mn) { return 0; } else if (cnt3 > mn) { return 2; } int idx = 0; while (cnt1 < mn) { cnt1 += 1; sum += tmp[idx]; idx += 1; } for (int i = idx; i < tmp.size(); i++) { sum += median; } if (sum > s) { return 0; } else if (cnt2 != 0) { return 1; } else { return 2; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; int tt; cin >> tt; for (int t = 1; t <= tt; t++) { v.clear(); cin >> n >> s; for (int i = 0; i < n; i++) { long long x, y; cin >> x >> y; v.push_back({x, y}); } sort(v.begin(), v.end()); long long l = 1; long long r = MOD; long long ans = 0; while (l <= r) { long long mid = (l + r) / 2; if (sure(mid) == 1) { ans = mid; l = mid + 1; } else if (sure(mid) == 2) { l = mid + 1; } else { r = mid - 1; } } cout << ans << "\n"; } return 0; }
### Prompt Your task is to create a cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long MOD = (long long)(1e9 + 7); const long long N = 200007; vector<pair<long long, long long>> v; long long s; int n; int sure(long long median) { long long cnt1 = 0; long long cnt2 = 0; long long cnt3 = 0; long long sum = 0; vector<long long> tmp; for (auto ele : v) { long long l = ele.first; long long r = ele.second; if (r < median) { cnt1 += 1; sum += l; } else if (l > median) { cnt3 += 1; sum += l; } else { cnt2 += 1; tmp.push_back(l); } } long long mn = (n - 1) / 2; if (cnt1 > mn) { return 0; } else if (cnt3 > mn) { return 2; } int idx = 0; while (cnt1 < mn) { cnt1 += 1; sum += tmp[idx]; idx += 1; } for (int i = idx; i < tmp.size(); i++) { sum += median; } if (sum > s) { return 0; } else if (cnt2 != 0) { return 1; } else { return 2; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; int tt; cin >> tt; for (int t = 1; t <= tt; t++) { v.clear(); cin >> n >> s; for (int i = 0; i < n; i++) { long long x, y; cin >> x >> y; v.push_back({x, y}); } sort(v.begin(), v.end()); long long l = 1; long long r = MOD; long long ans = 0; while (l <= r) { long long mid = (l + r) / 2; if (sure(mid) == 1) { ans = mid; l = mid + 1; } else if (sure(mid) == 2) { l = mid + 1; } else { r = mid - 1; } } cout << ans << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { ll n, s; cin >> n >> s; vector<array<int, 2>> ranges(n); for (int i = 0; i < n; i++) { cin >> ranges[i][0] >> ranges[i][1]; } int l = 0, r = 1e9 + 1; auto check = [&](ll x) { int ok = 1; int rlessmid = 0, lgreatemid = 0; ll curs = s; vector<int> l; for (int i = 0; i < n; i++) { if (ranges[i][1] < x) rlessmid++, curs -= ranges[i][0]; else if (ranges[i][0] >= x) lgreatemid++, curs -= ranges[i][0]; else { l.push_back(ranges[i][0]); } } if (rlessmid > n / 2) return 0; if (lgreatemid > n / 2) return 1; sort(l.begin(), l.end()); int need = n / 2 - rlessmid; for (int j = 0; j < need; j++) { curs -= l[j]; } if (curs >= x * (l.size() - need)) return 1; return 0; }; while (r - l > 1) { int mid = l + (r - l) / 2; if (check(mid)) l = mid; else r = mid; } cout << l << endl; } return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { ll n, s; cin >> n >> s; vector<array<int, 2>> ranges(n); for (int i = 0; i < n; i++) { cin >> ranges[i][0] >> ranges[i][1]; } int l = 0, r = 1e9 + 1; auto check = [&](ll x) { int ok = 1; int rlessmid = 0, lgreatemid = 0; ll curs = s; vector<int> l; for (int i = 0; i < n; i++) { if (ranges[i][1] < x) rlessmid++, curs -= ranges[i][0]; else if (ranges[i][0] >= x) lgreatemid++, curs -= ranges[i][0]; else { l.push_back(ranges[i][0]); } } if (rlessmid > n / 2) return 0; if (lgreatemid > n / 2) return 1; sort(l.begin(), l.end()); int need = n / 2 - rlessmid; for (int j = 0; j < need; j++) { curs -= l[j]; } if (curs >= x * (l.size() - need)) return 1; return 0; }; while (r - l > 1) { int mid = l + (r - l) / 2; if (check(mid)) l = mid; else r = mid; } cout << l << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int64_t MOD = 1000000007; int64_t l[200001]; int64_t r[200001]; int64_t get(int64_t x, int64_t s, int64_t n) { vector<int64_t> arr; int64_t a = 0, b = 0; for (int64_t i = 1; i <= n; i++) { if (r[i] < x) s -= l[i], a++; else if (l[i] >= x) s -= l[i], b++; else arr.push_back(l[i]); } if (s < 0 || a > n / 2) return false; sort(arr.begin(), arr.end()); for (int64_t i = 0; i < arr.size() && a < (n / 2); i++) s -= arr[i], a++; s -= (n - a - b) * x; if (s < 0) return false; return true; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); int64_t t; cin >> t; while (t--) { int64_t n, s; cin >> n >> s; for (int64_t i = 1; i <= n; i++) cin >> l[i] >> r[i]; int64_t lo = 0, hi = s; while (lo <= hi) { int64_t mi = (lo + hi) >> 1; if (get(mi, s, n)) lo = mi + 1; else hi = mi - 1; } cout << hi << "\n"; } }
### Prompt Please create a solution in cpp to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int64_t MOD = 1000000007; int64_t l[200001]; int64_t r[200001]; int64_t get(int64_t x, int64_t s, int64_t n) { vector<int64_t> arr; int64_t a = 0, b = 0; for (int64_t i = 1; i <= n; i++) { if (r[i] < x) s -= l[i], a++; else if (l[i] >= x) s -= l[i], b++; else arr.push_back(l[i]); } if (s < 0 || a > n / 2) return false; sort(arr.begin(), arr.end()); for (int64_t i = 0; i < arr.size() && a < (n / 2); i++) s -= arr[i], a++; s -= (n - a - b) * x; if (s < 0) return false; return true; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); int64_t t; cin >> t; while (t--) { int64_t n, s; cin >> n >> s; for (int64_t i = 1; i <= n; i++) cin >> l[i] >> r[i]; int64_t lo = 0, hi = s; while (lo <= hi) { int64_t mi = (lo + hi) >> 1; if (get(mi, s, n)) lo = mi + 1; else hi = mi - 1; } cout << hi << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 5; const long long inf = 1e14 + 5; inline long long min(long long a, long long b) { return a < b ? a : b; } struct Node { int l, r; } a[MAXN]; inline bool cmp_l(const Node &p, const Node &q) { return p.l < q.l; } int n; long long s; bool chk(int mid) { int cntl = n >> 1, cntr = (n + 1) >> 1; long long res = 0; for (int i = 1; i <= n; ++i) { if (a[i].r < mid) { --cntl; if (cntl < 0) return 0; res += a[i].l; } else if (a[i].l > mid) { --cntr; if (cntr < 0) return 0; res += a[i].l; } if (res > s) return 0; } for (int i = 1; i <= n; ++i) if (a[i].l <= mid && mid <= a[i].r) { if (cntl) { --cntl; res += a[i].l; } else { --cntr; res += mid; } if (res > s) return 0; } return 1; } void solve(void) { cin >> n >> s; for (int i = 1; i <= n; ++i) cin >> a[i].l >> a[i].r; sort(a + 1, a + n + 1, cmp_l); int l = a[(n >> 1) + 1].l, r = 1e9; while (l < r) { int mid = (l + r + 1) >> 1; if (chk(mid)) l = mid; else r = mid - 1; } printf("%d\n", l); } int main(void) { int T; scanf("%d", &T); while (T--) solve(); return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 5; const long long inf = 1e14 + 5; inline long long min(long long a, long long b) { return a < b ? a : b; } struct Node { int l, r; } a[MAXN]; inline bool cmp_l(const Node &p, const Node &q) { return p.l < q.l; } int n; long long s; bool chk(int mid) { int cntl = n >> 1, cntr = (n + 1) >> 1; long long res = 0; for (int i = 1; i <= n; ++i) { if (a[i].r < mid) { --cntl; if (cntl < 0) return 0; res += a[i].l; } else if (a[i].l > mid) { --cntr; if (cntr < 0) return 0; res += a[i].l; } if (res > s) return 0; } for (int i = 1; i <= n; ++i) if (a[i].l <= mid && mid <= a[i].r) { if (cntl) { --cntl; res += a[i].l; } else { --cntr; res += mid; } if (res > s) return 0; } return 1; } void solve(void) { cin >> n >> s; for (int i = 1; i <= n; ++i) cin >> a[i].l >> a[i].r; sort(a + 1, a + n + 1, cmp_l); int l = a[(n >> 1) + 1].l, r = 1e9; while (l < r) { int mid = (l + r + 1) >> 1; if (chk(mid)) l = mid; else r = mid - 1; } printf("%d\n", l); } int main(void) { int T; scanf("%d", &T); while (T--) solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; bool prime[1000001]; long long spf[10000001]; long long f[300005]; long long pow1(long long x, long long y) { long long res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % mod; y = y >> 1; x = (x * x) % mod; } return res; } long long divide(long long n) { return pow1(n, mod - 2); } long long ncr(long long n, long long r) { if (n < r) return 0; return (f[n] * ((divide(f[r]) * divide(f[n - r])) % mod)) % mod; } void sieve() { memset(prime, true, sizeof(prime)); for (long long i = 2; i * i <= 1000000; i++) if (prime[i]) for (long long j = i * i; j <= 1000000; j += i) prime[j] = false; prime[0] = prime[1] = false; } void fastsieve() { spf[1] = 1; for (int i = 2; i <= 1e7; i++) spf[i] = i; for (int i = 4; i <= 1e7; i += 2) spf[i] = 2; for (int i = 3; i * i <= 1e7; i++) { if (spf[i] == i) { for (int j = i * i; j <= 1e7; j += i) if (spf[j] == j) spf[j] = i; } } } vector<long long> factorize(long long n) { long long count = 0; vector<long long> fac; while (!(n % 2)) { n >>= 1; count++; } if (count % 2) fac.push_back(2ll); for (long long i = 3; i <= sqrt(n); i += 2) { count = 0; while (n % i == 0) { count++; n = n / i; } if (count % 2) fac.push_back(i); } if (n > 2) fac.push_back(n); return fac; } vector<long long> fastfactorize(long long n) { vector<long long> v; long long prev = 0, cnt = 0; while (n != 1) { if (prev == spf[n]) cnt++; else { if (cnt % 2) v.push_back(prev); cnt = 1; prev = spf[n]; } n /= spf[n]; if (n == 1) { if (cnt % 2) v.push_back(prev); cnt = 1; prev = spf[n]; } } return v; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { long long n, s, i, ans = 0; cin >> n >> s; vector<pair<long long, long long>> v; for (i = 0; i < n; i++) { long long l, r; cin >> l >> r; v.push_back({l, r}); } sort(v.begin(), v.end()); long long l = 0, r = 1e9 + 1; while (l <= r) { long long mid = (l + r) / 2, cnt = 0, sum = 0; for (i = n - 1; i >= 0; i--) { if (mid <= v[i].second && cnt < (n + 1) / 2) { cnt++; sum += max(mid, v[i].first); } else sum += v[i].first; } if (cnt < (n + 1) / 2 || sum > s) r = mid - 1; else l = mid + 1, ans = mid; } cout << ans << endl; } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; bool prime[1000001]; long long spf[10000001]; long long f[300005]; long long pow1(long long x, long long y) { long long res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % mod; y = y >> 1; x = (x * x) % mod; } return res; } long long divide(long long n) { return pow1(n, mod - 2); } long long ncr(long long n, long long r) { if (n < r) return 0; return (f[n] * ((divide(f[r]) * divide(f[n - r])) % mod)) % mod; } void sieve() { memset(prime, true, sizeof(prime)); for (long long i = 2; i * i <= 1000000; i++) if (prime[i]) for (long long j = i * i; j <= 1000000; j += i) prime[j] = false; prime[0] = prime[1] = false; } void fastsieve() { spf[1] = 1; for (int i = 2; i <= 1e7; i++) spf[i] = i; for (int i = 4; i <= 1e7; i += 2) spf[i] = 2; for (int i = 3; i * i <= 1e7; i++) { if (spf[i] == i) { for (int j = i * i; j <= 1e7; j += i) if (spf[j] == j) spf[j] = i; } } } vector<long long> factorize(long long n) { long long count = 0; vector<long long> fac; while (!(n % 2)) { n >>= 1; count++; } if (count % 2) fac.push_back(2ll); for (long long i = 3; i <= sqrt(n); i += 2) { count = 0; while (n % i == 0) { count++; n = n / i; } if (count % 2) fac.push_back(i); } if (n > 2) fac.push_back(n); return fac; } vector<long long> fastfactorize(long long n) { vector<long long> v; long long prev = 0, cnt = 0; while (n != 1) { if (prev == spf[n]) cnt++; else { if (cnt % 2) v.push_back(prev); cnt = 1; prev = spf[n]; } n /= spf[n]; if (n == 1) { if (cnt % 2) v.push_back(prev); cnt = 1; prev = spf[n]; } } return v; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { long long n, s, i, ans = 0; cin >> n >> s; vector<pair<long long, long long>> v; for (i = 0; i < n; i++) { long long l, r; cin >> l >> r; v.push_back({l, r}); } sort(v.begin(), v.end()); long long l = 0, r = 1e9 + 1; while (l <= r) { long long mid = (l + r) / 2, cnt = 0, sum = 0; for (i = n - 1; i >= 0; i--) { if (mid <= v[i].second && cnt < (n + 1) / 2) { cnt++; sum += max(mid, v[i].first); } else sum += v[i].first; } if (cnt < (n + 1) / 2 || sum > s) r = mid - 1; else l = mid + 1, ans = mid; } cout << ans << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; bool isok(vector<pair<long long, long long> > &A, long long mid, long long s) { long long sum = 0, c = 0, left = 0, n = A.size(); vector<char> visited(n, 0); for (long long i = 0; i < (n); i++) { if (A[i].second < mid) { c++; sum += A[i].first; visited[i] = 1; } if (c > (n - 1) / 2) return false; } left = (n - 1) / 2 - c; for (long long i = 0; i < (n); i++) { if (A[i].second >= mid && A[i].first <= mid) { if (left) { sum += A[i].first; left--; } else sum += mid; } else if (A[i].second >= mid) sum += A[i].first; } if (sum > s) return false; return true; } int main() { long long(t); scanf("%lld", &(t)); ; while (t--) { long long(n); scanf("%lld", &(n)); ; long long(s); scanf("%lld", &(s)); ; vector<pair<long long, long long> > A(n); long long high = 1, low = 1e9; for (long long i = 0; i < (n); i++) { scanf("%lld%lld", &A[i].first, &A[i].second); high = max(high, A[i].second); low = min(low, A[i].first); } sort((A).begin(), (A).end()); while (low <= high) { long long mid = low + (high - low) / 2; if (isok(A, mid, s)) low = mid + 1; else high = mid - 1; } printf("%lld\n", high); } return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool isok(vector<pair<long long, long long> > &A, long long mid, long long s) { long long sum = 0, c = 0, left = 0, n = A.size(); vector<char> visited(n, 0); for (long long i = 0; i < (n); i++) { if (A[i].second < mid) { c++; sum += A[i].first; visited[i] = 1; } if (c > (n - 1) / 2) return false; } left = (n - 1) / 2 - c; for (long long i = 0; i < (n); i++) { if (A[i].second >= mid && A[i].first <= mid) { if (left) { sum += A[i].first; left--; } else sum += mid; } else if (A[i].second >= mid) sum += A[i].first; } if (sum > s) return false; return true; } int main() { long long(t); scanf("%lld", &(t)); ; while (t--) { long long(n); scanf("%lld", &(n)); ; long long(s); scanf("%lld", &(s)); ; vector<pair<long long, long long> > A(n); long long high = 1, low = 1e9; for (long long i = 0; i < (n); i++) { scanf("%lld%lld", &A[i].first, &A[i].second); high = max(high, A[i].second); low = min(low, A[i].first); } sort((A).begin(), (A).end()); while (low <= high) { long long mid = low + (high - low) / 2; if (isok(A, mid, s)) low = mid + 1; else high = mid - 1; } printf("%lld\n", high); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 2 * 1e5 + 1; struct Emp { int l, r; } emp[N]; bool search(int m, int n, long long s) { long long sum = 0, cnt = 0; for (int i = n - 1; i >= 0; i--) { if (emp[i].l >= m) cnt++; else if (emp[i].l <= m && emp[i].r >= m && cnt <= n / 2) { sum += m - emp[i].l; cnt++; } } return sum <= s && (cnt >= n / 2 + 1); } void solve() { int n; long long s; cin >> n >> s; for (int i = 0; i < n; i++) { int l, r; cin >> l >> r; emp[i].l = l; emp[i].r = r; s -= l; } sort(emp, emp + n, [](const Emp &x, const Emp &y) { return x.l < y.l; }); int l = 0, r = 1e9 + 1; while (l < r - 1) { int m = l + (r - l) / 2; bool f = search(m, n, s); if (f) l = m; else r = m; } cout << l << "\n"; } int t; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> t; while (t--) solve(); return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 2 * 1e5 + 1; struct Emp { int l, r; } emp[N]; bool search(int m, int n, long long s) { long long sum = 0, cnt = 0; for (int i = n - 1; i >= 0; i--) { if (emp[i].l >= m) cnt++; else if (emp[i].l <= m && emp[i].r >= m && cnt <= n / 2) { sum += m - emp[i].l; cnt++; } } return sum <= s && (cnt >= n / 2 + 1); } void solve() { int n; long long s; cin >> n >> s; for (int i = 0; i < n; i++) { int l, r; cin >> l >> r; emp[i].l = l; emp[i].r = r; s -= l; } sort(emp, emp + n, [](const Emp &x, const Emp &y) { return x.l < y.l; }); int l = 0, r = 1e9 + 1; while (l < r - 1) { int m = l + (r - l) / 2; bool f = search(m, n, s); if (f) l = m; else r = m; } cout << l << "\n"; } int t; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> t; while (t--) solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; long long n, s, curs; const long long maxn = 2e5 + 10; pair<long long, long long> vec[maxn]; long long v[maxn]; bool check(long long mid) { if (mid == s + 1) return false; long long sz = 0; for (long long i = 0; i < n; i++) { if (vec[i].second >= mid) v[sz++] = vec[i].first; } if (sz <= n / 2) return false; long long ans = 0; for (long long i = 1; i <= n / 2 + 1; i++) { if (v[sz - i] < mid) { ans += mid - v[sz - i]; } } return ans <= curs; } int main() { long long t; scanf("%lld", &t); while (t--) { scanf("%lld%lld", &n, &s); curs = s; for (long long i = 0; i < n; i++) { long long a, b; scanf("%lld%lld", &a, &b); vec[i] = make_pair(a, b); curs -= vec[i].first; } sort(vec, vec + n); long long l = vec[n / 2].first, r = s + 1; while (r - l > 1) { long long mid = (r + l) >> 1; if (check(mid)) l = mid; else r = mid; } cout << l << endl; } }
### Prompt In Cpp, your task is to solve the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, s, curs; const long long maxn = 2e5 + 10; pair<long long, long long> vec[maxn]; long long v[maxn]; bool check(long long mid) { if (mid == s + 1) return false; long long sz = 0; for (long long i = 0; i < n; i++) { if (vec[i].second >= mid) v[sz++] = vec[i].first; } if (sz <= n / 2) return false; long long ans = 0; for (long long i = 1; i <= n / 2 + 1; i++) { if (v[sz - i] < mid) { ans += mid - v[sz - i]; } } return ans <= curs; } int main() { long long t; scanf("%lld", &t); while (t--) { scanf("%lld%lld", &n, &s); curs = s; for (long long i = 0; i < n; i++) { long long a, b; scanf("%lld%lld", &a, &b); vec[i] = make_pair(a, b); curs -= vec[i].first; } sort(vec, vec + n); long long l = vec[n / 2].first, r = s + 1; while (r - l > 1) { long long mid = (r + l) >> 1; if (check(mid)) l = mid; else r = mid; } cout << l << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { long long n, k; cin >> n >> k; vector<pair<int, int> > v; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; v.push_back({a, b}); } sort(v.begin(), v.end()); long long second = 0, e = 2e14; long long mid; while (e - second > 1) { long long cnt = 0, flag = 0, sum = 0; mid = second + (e - second) / 2; vector<int> a; for (int i = 0; i < n; i++) { if (v[i].second < mid) sum += v[i].first; else if (v[i].first >= mid) { sum += v[i].first; cnt++; } else a.push_back(v[i].first); } long long req = max(0LL, (n + 1) / 2 - cnt); if (req > a.size()) flag = 1; else for (int i = 0; i < a.size(); i++) { if (i < a.size() - req) sum += a[i]; else sum += mid; } if (sum <= k && flag == 0) second = mid; else e = mid; } cout << second << endl; } }
### Prompt Please formulate a Cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { long long n, k; cin >> n >> k; vector<pair<int, int> > v; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; v.push_back({a, b}); } sort(v.begin(), v.end()); long long second = 0, e = 2e14; long long mid; while (e - second > 1) { long long cnt = 0, flag = 0, sum = 0; mid = second + (e - second) / 2; vector<int> a; for (int i = 0; i < n; i++) { if (v[i].second < mid) sum += v[i].first; else if (v[i].first >= mid) { sum += v[i].first; cnt++; } else a.push_back(v[i].first); } long long req = max(0LL, (n + 1) / 2 - cnt); if (req > a.size()) flag = 1; else for (int i = 0; i < a.size(); i++) { if (i < a.size() - req) sum += a[i]; else sum += mid; } if (sum <= k && flag == 0) second = mid; else e = mid; } cout << second << endl; } } ```
#include <bits/stdc++.h> using namespace std; long long l[300100], r[300100]; long long n, sal; signed main() { long long t; scanf("%lld", &t); while (t--) { scanf("%lld %lld", &n, &sal); for (long long i = 1; i <= n; i++) scanf("%lld %lld", &l[i], &r[i]); long long bg = 1, nd = 1e9; while (bg < nd) { long long mid = (bg + nd + 1) >> 1ll; multiset<pair<long long, long long>> s, b; for (long long i = 1; i <= n; i++) if (mid <= r[i]) s.emplace(l[i], r[i]); else if (l[i] < mid) b.emplace(l[i], r[i]); long long cnt = (n + 1) / 2; while (s.size() > cnt) { if (s.begin()->first < mid) b.emplace(*s.begin()); s.erase(s.find(*s.begin())); } if (s.size() < (long long)cnt) { nd = mid - 1ll; continue; } long long aux = 0; cnt = (n - 1) / 2; for (auto u : b) { if (cnt == 0) break; aux += 0ll + u.first; cnt--; } for (auto u : s) aux += max(u.first, mid); if (0ll + aux <= 0ll + sal) bg = mid; else nd = mid - 1; } printf("%lld\n", bg); } }
### Prompt Your challenge is to write a CPP solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long l[300100], r[300100]; long long n, sal; signed main() { long long t; scanf("%lld", &t); while (t--) { scanf("%lld %lld", &n, &sal); for (long long i = 1; i <= n; i++) scanf("%lld %lld", &l[i], &r[i]); long long bg = 1, nd = 1e9; while (bg < nd) { long long mid = (bg + nd + 1) >> 1ll; multiset<pair<long long, long long>> s, b; for (long long i = 1; i <= n; i++) if (mid <= r[i]) s.emplace(l[i], r[i]); else if (l[i] < mid) b.emplace(l[i], r[i]); long long cnt = (n + 1) / 2; while (s.size() > cnt) { if (s.begin()->first < mid) b.emplace(*s.begin()); s.erase(s.find(*s.begin())); } if (s.size() < (long long)cnt) { nd = mid - 1ll; continue; } long long aux = 0; cnt = (n - 1) / 2; for (auto u : b) { if (cnt == 0) break; aux += 0ll + u.first; cnt--; } for (auto u : s) aux += max(u.first, mid); if (0ll + aux <= 0ll + sal) bg = mid; else nd = mid - 1; } printf("%lld\n", bg); } } ```
#include <bits/stdc++.h> using namespace std; long long n; struct node { long long l, r; } a1[200005], a2[200005], e[200005]; inline long long read() { long long x = 0, f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -1; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; return x * f; } inline bool cmp1(node x, node y) { return x.r < y.r; } inline bool cmp2(node x, node y) { return x.l < y.l; } inline long long calc(long long x) { long long sum = 0, n1 = 0, n2 = 0; for (long long i = 1; i <= n; i++) if (e[i].r < x) sum += e[i].l, n1++; for (long long i = 1; i <= n; i++) if (e[i].l > x) sum += e[i].l, n2++; if (n1 > n / 2) return (1ll << 60); if (n2 > n / 2) return 0; long long tp = n / 2 - n1; for (long long i = 1; i <= n; i++) if (a1[i].l <= x && a1[i].r >= x) { if (tp) sum += a1[i].l, tp--; else sum += x; } return sum; } int main() { long long T = read(); while (T--) { n = read(); long long s = read(); for (long long i = 1; i <= n; i++) { e[i].l = a1[i].l = a2[i].l = read(); e[i].r = a1[i].r = a2[i].r = read(); } sort(a1 + 1, a1 + n + 1, cmp2); if (n == 1) { cout << min(s, e[1].r) << endl; continue; } long long l = 1, r = (long long)(1e9), ans = 0; while (l <= r) { long long mid = l + r >> 1; if (calc(mid) <= s) ans = mid, l = mid + 1; else r = mid - 1; } printf("%I64d\n", ans); } return 0; }
### Prompt Your task is to create a cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n; struct node { long long l, r; } a1[200005], a2[200005], e[200005]; inline long long read() { long long x = 0, f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -1; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; return x * f; } inline bool cmp1(node x, node y) { return x.r < y.r; } inline bool cmp2(node x, node y) { return x.l < y.l; } inline long long calc(long long x) { long long sum = 0, n1 = 0, n2 = 0; for (long long i = 1; i <= n; i++) if (e[i].r < x) sum += e[i].l, n1++; for (long long i = 1; i <= n; i++) if (e[i].l > x) sum += e[i].l, n2++; if (n1 > n / 2) return (1ll << 60); if (n2 > n / 2) return 0; long long tp = n / 2 - n1; for (long long i = 1; i <= n; i++) if (a1[i].l <= x && a1[i].r >= x) { if (tp) sum += a1[i].l, tp--; else sum += x; } return sum; } int main() { long long T = read(); while (T--) { n = read(); long long s = read(); for (long long i = 1; i <= n; i++) { e[i].l = a1[i].l = a2[i].l = read(); e[i].r = a1[i].r = a2[i].r = read(); } sort(a1 + 1, a1 + n + 1, cmp2); if (n == 1) { cout << min(s, e[1].r) << endl; continue; } long long l = 1, r = (long long)(1e9), ans = 0; while (l <= r) { long long mid = l + r >> 1; if (calc(mid) <= s) ans = mid, l = mid + 1; else r = mid - 1; } printf("%I64d\n", ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; struct node { int l, r; } a[200009]; int n; long long k; bool cmp(node aa, node bb) { if (aa.l == bb.l) return aa.r < bb.r; else return aa.l < bb.l; } int check(int x) { long long ans = 0; int w = 0; for (int i = n; i >= 1; i--) { if (w <= n / 2) { if (a[i].r < x) ans += a[i].l; else { ans += max(a[i].l, x); w++; } } else { ans += a[i].l; } } if (w != (n / 2 + 1)) return 0; if (ans <= k) return 1; else return 0; } int main() { int t; scanf("%d", &t); while (t--) { scanf("%d%lld", &n, &k); for (int i = 1; i <= n; i++) scanf("%d%d", &a[i].l, &a[i].r); sort(a + 1, a + 1 + n, cmp); int l = 0, r = 1e9; while (l < r) { int mid = (l + r + 1) / 2; if (check(mid) == 1) l = mid; else r = mid - 1; } printf("%d\n", l); } return 0; }
### Prompt Develop a solution in Cpp to the problem described below: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct node { int l, r; } a[200009]; int n; long long k; bool cmp(node aa, node bb) { if (aa.l == bb.l) return aa.r < bb.r; else return aa.l < bb.l; } int check(int x) { long long ans = 0; int w = 0; for (int i = n; i >= 1; i--) { if (w <= n / 2) { if (a[i].r < x) ans += a[i].l; else { ans += max(a[i].l, x); w++; } } else { ans += a[i].l; } } if (w != (n / 2 + 1)) return 0; if (ans <= k) return 1; else return 0; } int main() { int t; scanf("%d", &t); while (t--) { scanf("%d%lld", &n, &k); for (int i = 1; i <= n; i++) scanf("%d%d", &a[i].l, &a[i].r); sort(a + 1, a + 1 + n, cmp); int l = 0, r = 1e9; while (l < r) { int mid = (l + r + 1) / 2; if (check(mid) == 1) l = mid; else r = mid - 1; } printf("%d\n", l); } return 0; } ```
#include <bits/stdc++.h> const double PI = 3.141592653589793238460; using namespace std; pair<long long int, long long int> ar[200001]; bool isPossible(long long int mid, long long int s, long long int n) { long long int sum = 0; int cnt = 0; vector<int> va, vb, vc; for (int i = 1; i <= n; i++) { if (ar[i].second < mid) va.push_back(i), sum += ar[i].first; else if (ar[i].first > mid) vb.push_back(i), sum += ar[i].first, cnt++; else vc.push_back(i); } int rem = max((long long int)0, n / 2 + 1 - cnt); if (rem > vc.size()) return false; while (vc.size() > 0) { if (rem) sum += mid, rem--; else sum += ar[vc.back()].first; vc.pop_back(); } return sum <= s; } long long int getAns(long long int n, long long int s) { long long int L = ar[1].first; long long int H = 1000000000; long long int res = 1; while (L <= H) { long long int mid = (L + H) / 2; if (isPossible(mid, s, n)) res = max(res, mid), L = mid + 1; else H = mid - 1; } return res; } bool comp(pair<long long int, long long int> a, pair<long long int, long long int> b) { return a.first < b.first; } int main() { long long int t, n, s; cin >> t; while (t--) { cin >> n >> s; for (int i = 1; i <= n; i++) cin >> ar[i].first >> ar[i].second; sort(ar + 1, ar + n + 1, comp); cout << getAns(n, s) << '\n'; } }
### Prompt Create a solution in Cpp for the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> const double PI = 3.141592653589793238460; using namespace std; pair<long long int, long long int> ar[200001]; bool isPossible(long long int mid, long long int s, long long int n) { long long int sum = 0; int cnt = 0; vector<int> va, vb, vc; for (int i = 1; i <= n; i++) { if (ar[i].second < mid) va.push_back(i), sum += ar[i].first; else if (ar[i].first > mid) vb.push_back(i), sum += ar[i].first, cnt++; else vc.push_back(i); } int rem = max((long long int)0, n / 2 + 1 - cnt); if (rem > vc.size()) return false; while (vc.size() > 0) { if (rem) sum += mid, rem--; else sum += ar[vc.back()].first; vc.pop_back(); } return sum <= s; } long long int getAns(long long int n, long long int s) { long long int L = ar[1].first; long long int H = 1000000000; long long int res = 1; while (L <= H) { long long int mid = (L + H) / 2; if (isPossible(mid, s, n)) res = max(res, mid), L = mid + 1; else H = mid - 1; } return res; } bool comp(pair<long long int, long long int> a, pair<long long int, long long int> b) { return a.first < b.first; } int main() { long long int t, n, s; cin >> t; while (t--) { cin >> n >> s; for (int i = 1; i <= n; i++) cin >> ar[i].first >> ar[i].second; sort(ar + 1, ar + n + 1, comp); cout << getAns(n, s) << '\n'; } } ```
#include <bits/stdc++.h> using namespace std; const long long N = 2e5 + 7; const long long INF = 1e9; const long long MOD = INF + 7; long long n, s; vector<pair<long long, long long> > a; bool solve(long long m) { long long idx = (long long)n / 2; long long sm = 0; for (long long i = 0; i < n; i++) { sm += a[i].first; } long long cnt = 0; for (long long i = n - 1; i >= 0 && cnt <= idx; i--) { if (m <= a[i].second) { sm += max(0LL, m - a[i].first); cnt++; } } return cnt == idx + 1 && sm <= s; } void solve() { cin >> n >> s; a.clear(); long long sm = 0; for (long long i = 0, x, y; i < n; i++) { cin >> x >> y; a.push_back({x, y}); sm += x; } sort((a).begin(), (a).end()); long long lo = 0, hi = INF * INF; while (lo < hi) { long long now = sm; long long m = (lo + hi + 1) / 2; if (solve(m)) lo = m; else hi = m - 1; } cout << lo << '\n'; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t = 1; cin >> t; while (t--) { solve(); } }
### Prompt Develop a solution in Cpp to the problem described below: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long N = 2e5 + 7; const long long INF = 1e9; const long long MOD = INF + 7; long long n, s; vector<pair<long long, long long> > a; bool solve(long long m) { long long idx = (long long)n / 2; long long sm = 0; for (long long i = 0; i < n; i++) { sm += a[i].first; } long long cnt = 0; for (long long i = n - 1; i >= 0 && cnt <= idx; i--) { if (m <= a[i].second) { sm += max(0LL, m - a[i].first); cnt++; } } return cnt == idx + 1 && sm <= s; } void solve() { cin >> n >> s; a.clear(); long long sm = 0; for (long long i = 0, x, y; i < n; i++) { cin >> x >> y; a.push_back({x, y}); sm += x; } sort((a).begin(), (a).end()); long long lo = 0, hi = INF * INF; while (lo < hi) { long long now = sm; long long m = (lo + hi + 1) / 2; if (solve(m)) lo = m; else hi = m - 1; } cout << lo << '\n'; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t = 1; cin >> t; while (t--) { solve(); } } ```
#include <bits/stdc++.h> using namespace std; int dirx[] = {1, -1, 0, 0}, diry[] = {0, 0, 1, -1}; long long bigmod(long long x, long long p) { long long res = 1; while (p) { if (p & 1) res = (res * x) % 998244353; x = (x * x) % 998244353; p >>= 1; } return res; } vector<pair<int, int> > inp; int n; long long fun(int mid) { long long sum = mid; multiset<int> both; int low = 0, high = 0; for (int i = 0; i < n; i++) { if (inp[i].second > mid) { sum += inp[i].second; high++; } else if (inp[i].first < mid) { sum += inp[i].second; low++; } else both.insert(inp[i].second); } for (int i = 0; i < n / 2 - low; i++) { sum += *both.begin(); both.erase(both.begin()); } sum += 1LL * mid * (n / 2 - high); return sum; } bool cmp(pair<int, int>& a, pair<int, int>& b) { return a.second < b.second; } int32_t main() { ios_base::sync_with_stdio(false); int t; cin >> t; while (t--) { inp.clear(); cin >> n; long long taka; cin >> taka; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; inp.push_back({y, x}); } sort(inp.begin(), inp.end()); int h = inp[n / 2].first, l, mid, ans; sort(inp.begin(), inp.end(), cmp); l = inp[n / 2].second; while (l <= h) { int mid = (l + h) / 2; if (fun(mid) <= taka) { ans = mid; l = mid + 1; } else h = mid - 1; } cout << ans << "\n"; } return 0; }
### Prompt Create a solution in CPP for the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int dirx[] = {1, -1, 0, 0}, diry[] = {0, 0, 1, -1}; long long bigmod(long long x, long long p) { long long res = 1; while (p) { if (p & 1) res = (res * x) % 998244353; x = (x * x) % 998244353; p >>= 1; } return res; } vector<pair<int, int> > inp; int n; long long fun(int mid) { long long sum = mid; multiset<int> both; int low = 0, high = 0; for (int i = 0; i < n; i++) { if (inp[i].second > mid) { sum += inp[i].second; high++; } else if (inp[i].first < mid) { sum += inp[i].second; low++; } else both.insert(inp[i].second); } for (int i = 0; i < n / 2 - low; i++) { sum += *both.begin(); both.erase(both.begin()); } sum += 1LL * mid * (n / 2 - high); return sum; } bool cmp(pair<int, int>& a, pair<int, int>& b) { return a.second < b.second; } int32_t main() { ios_base::sync_with_stdio(false); int t; cin >> t; while (t--) { inp.clear(); cin >> n; long long taka; cin >> taka; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; inp.push_back({y, x}); } sort(inp.begin(), inp.end()); int h = inp[n / 2].first, l, mid, ans; sort(inp.begin(), inp.end(), cmp); l = inp[n / 2].second; while (l <= h) { int mid = (l + h) / 2; if (fun(mid) <= taka) { ans = mid; l = mid + 1; } else h = mid - 1; } cout << ans << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int t, n; long long s; struct uzi { int l, r; bool operator<(const uzi& t) const { return l > t.l; } } p[N]; int vis[N]; int main() { ios::sync_with_stdio(false); for (cin >> t; t; t--) { cin >> n >> s; long long S = 0; for (int i = 1; i <= n; i++) cin >> p[i].l >> p[i].r, S += p[i].l; sort(p + 1, p + 1 + n); int l = p[n / 2 + 1].l, r = 1e9, ans = 0; while (l <= r) { int mid = l + r >> 1; long long sum = 0; int sz = 0; long long tmp = S; for (int i = 1; i <= n; i++) { if (p[i].l > mid) { sz++; sum += p[i].l; vis[i] = 1; tmp -= p[i].l; } } int tot = 0; for (int i = 1; i <= n; i++) { if (p[i].l <= mid && p[i].r >= mid && sz <= n / 2) { vis[i]++; sz++; tot++; sum += mid; tmp -= p[i].l; } } if (sz <= n / 2) r = mid - 1; else { sum += tmp; if (sum <= s) ans = mid, l = mid + 1; else r = mid - 1; } } cout << ans << '\n'; } return 0; }
### Prompt Generate a CPP solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int t, n; long long s; struct uzi { int l, r; bool operator<(const uzi& t) const { return l > t.l; } } p[N]; int vis[N]; int main() { ios::sync_with_stdio(false); for (cin >> t; t; t--) { cin >> n >> s; long long S = 0; for (int i = 1; i <= n; i++) cin >> p[i].l >> p[i].r, S += p[i].l; sort(p + 1, p + 1 + n); int l = p[n / 2 + 1].l, r = 1e9, ans = 0; while (l <= r) { int mid = l + r >> 1; long long sum = 0; int sz = 0; long long tmp = S; for (int i = 1; i <= n; i++) { if (p[i].l > mid) { sz++; sum += p[i].l; vis[i] = 1; tmp -= p[i].l; } } int tot = 0; for (int i = 1; i <= n; i++) { if (p[i].l <= mid && p[i].r >= mid && sz <= n / 2) { vis[i]++; sz++; tot++; sum += mid; tmp -= p[i].l; } } if (sz <= n / 2) r = mid - 1; else { sum += tmp; if (sum <= s) ans = mid, l = mid + 1; else r = mid - 1; } } cout << ans << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; std::mt19937 rng( (int)std::chrono::steady_clock::now().time_since_epoch().count()); const double EPS = 1e-9; const double PI = acos(-1); const int MOD = 1000000007; vector<pair<long long, long long> > v; int n; long long s; bool test(long long x) { long long sum = 0; for (int i = 0; i < n; i++) sum += v[i].first; int cont = 0; for (int i = n - 1; i >= 0 and cont < (n + 1) / 2; i--) { if (x <= v[i].second) { sum += max(0LL, x - v[i].first); cont++; } } return sum <= s and cont == (n + 1) / 2; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { cin >> n >> s; v.resize(n); for (int i = 0; i < n; i++) { cin >> v[i].first >> v[i].second; } sort(v.begin(), v.end()); long long lo = 0, hi = MOD, ans = -1LL; while (lo <= hi) { long long mi = (lo + hi) / 2LL; if (test(mi)) { ans = mi; lo = mi + 1; } else { hi = mi - 1; } } cout << ans << '\n'; } return 0; }
### Prompt Generate a CPP solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; std::mt19937 rng( (int)std::chrono::steady_clock::now().time_since_epoch().count()); const double EPS = 1e-9; const double PI = acos(-1); const int MOD = 1000000007; vector<pair<long long, long long> > v; int n; long long s; bool test(long long x) { long long sum = 0; for (int i = 0; i < n; i++) sum += v[i].first; int cont = 0; for (int i = n - 1; i >= 0 and cont < (n + 1) / 2; i--) { if (x <= v[i].second) { sum += max(0LL, x - v[i].first); cont++; } } return sum <= s and cont == (n + 1) / 2; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { cin >> n >> s; v.resize(n); for (int i = 0; i < n; i++) { cin >> v[i].first >> v[i].second; } sort(v.begin(), v.end()); long long lo = 0, hi = MOD, ans = -1LL; while (lo <= hi) { long long mi = (lo + hi) / 2LL; if (test(mi)) { ans = mi; lo = mi + 1; } else { hi = mi - 1; } } cout << ans << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; long long s, sum; int T, n; pair<int, int> L[N]; bool judge(long long val) { long long v = s; int num = (n + 1) / 2; for (int i = 1; i <= n; i++) { v -= L[i].first; if (L[i].first >= val) num--; } vector<int> cost; for (int i = 1; i <= n; i++) { if (L[i].first < val and L[i].second >= val) { cost.push_back(val - L[i].first); } } sort(cost.begin(), cost.end()); if (cost.size() < num) return false; for (int i = 0; i < num; i++) v -= cost[i]; return v >= 0; } int main() { cin >> T; while (T--) { cin >> n >> s; sum = 0; for (int i = 1; i <= n; i++) cin >> L[i].first >> L[i].second, sum += L[i].first; sort(L + 1, L + 1 + n); int mid = (n + 1) / 2; long long l = L[mid].first, r = 1e10; long long ans = l; while (l <= r) { long long m = (l + r) / 2; if (judge(m)) { ans = max(ans, m); l = m + 1; } else { r = m - 1; } } cout << ans << endl; } }
### Prompt Your task is to create a cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; long long s, sum; int T, n; pair<int, int> L[N]; bool judge(long long val) { long long v = s; int num = (n + 1) / 2; for (int i = 1; i <= n; i++) { v -= L[i].first; if (L[i].first >= val) num--; } vector<int> cost; for (int i = 1; i <= n; i++) { if (L[i].first < val and L[i].second >= val) { cost.push_back(val - L[i].first); } } sort(cost.begin(), cost.end()); if (cost.size() < num) return false; for (int i = 0; i < num; i++) v -= cost[i]; return v >= 0; } int main() { cin >> T; while (T--) { cin >> n >> s; sum = 0; for (int i = 1; i <= n; i++) cin >> L[i].first >> L[i].second, sum += L[i].first; sort(L + 1, L + 1 + n); int mid = (n + 1) / 2; long long l = L[mid].first, r = 1e10; long long ans = l; while (l <= r) { long long m = (l + r) / 2; if (judge(m)) { ans = max(ans, m); l = m + 1; } else { r = m - 1; } } cout << ans << endl; } } ```
#include <bits/stdc++.h> using namespace std; bool isValid(const long long& median, const vector<pair<long long, long long>>& ranges, const long long& s) { long long select_count = 0; long long sum = 0; for (int i = 0; i < ranges.size(); i++) { if (select_count < (ranges.size() + 1) / 2 && ranges[i].second >= median) { sum += max(ranges[i].first, median); select_count++; } else { sum += ranges[i].first; } } if (select_count < (ranges.size() + 1) / 2) { return false; } else { return (sum <= s); } } int main() { int t; cin >> t; for (int tc = 0; tc < t; tc++) { long long n, s; cin >> n >> s; vector<pair<long long, long long>> ranges(n); long long min_low = s; long long max_high = 0; for (int i = 0; i < n; i++) { cin >> ranges[i].first >> ranges[i].second; min_low = min(ranges[i].first, min_low); max_high = max(ranges[i].second, max_high); } sort(ranges.begin(), ranges.end(), greater<pair<long long, long long>>()); long long low = min_low; long long high = max_high; long long middle; while (low < high) { middle = (low + high + 1) / 2; if (isValid(middle, ranges, s)) { low = middle; } else { high = middle - 1; } } cout << low << endl; } return 0; }
### Prompt Create a solution in CPP for the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool isValid(const long long& median, const vector<pair<long long, long long>>& ranges, const long long& s) { long long select_count = 0; long long sum = 0; for (int i = 0; i < ranges.size(); i++) { if (select_count < (ranges.size() + 1) / 2 && ranges[i].second >= median) { sum += max(ranges[i].first, median); select_count++; } else { sum += ranges[i].first; } } if (select_count < (ranges.size() + 1) / 2) { return false; } else { return (sum <= s); } } int main() { int t; cin >> t; for (int tc = 0; tc < t; tc++) { long long n, s; cin >> n >> s; vector<pair<long long, long long>> ranges(n); long long min_low = s; long long max_high = 0; for (int i = 0; i < n; i++) { cin >> ranges[i].first >> ranges[i].second; min_low = min(ranges[i].first, min_low); max_high = max(ranges[i].second, max_high); } sort(ranges.begin(), ranges.end(), greater<pair<long long, long long>>()); long long low = min_low; long long high = max_high; long long middle; while (low < high) { middle = (low + high + 1) / 2; if (isValid(middle, ranges, s)) { low = middle; } else { high = middle - 1; } } cout << low << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; template <class T> bool umin(T &a, T b) { return a > b ? (a = b, true) : false; } template <class T> bool umax(T &a, T b) { return a < b ? (a = b, true) : false; } long long POW(long long a, long long p, long long M) { if (!p) return 1LL; long long T = POW(a, p / 2, M); T = T * T % M; if (p & 1) T = T * (a % M) % M; return T; } long long SQRT(long long a) { long long b = (long long)sqrtl(((double)a) + 0.5); while (b * b < a) ++b; while (b * b > a) --b; return (b * b == a) ? b : -1; } const long long MOD = 1e9 + 7; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.precision(10); cout << fixed; int q; cin >> q; while (q--) { int n; cin >> n; long long s; cin >> s; vector<pair<long long, long long>> v(n); for (int i = 0; i < n; i++) cin >> v[i].first >> v[i].second; sort((v).begin(), (v).end()); long long L = v[n / 2].first; long long R = 1e15; long long ff = L; while (L <= R) { long long mid = L + R; mid /= 2; long long f = 0; int ct = 0; bool ps = false; vector<long long> tt; for (auto i : v) { if (i.first > mid) { f += i.first; ct++; } else if (i.second < mid) f += i.first; else tt.push_back(i.first); } if (tt.size()) { ct++; ps = true; f += mid; tt.pop_back(); } while (ct < (n + 1) / 2 && tt.size()) { ct++; f += mid; tt.pop_back(); } for (auto i : tt) f += i; if (f <= s && ps && ct == (n + 1) / 2) { L = mid + 1; ff = mid; } else R = mid - 1; } cout << ff << endl; } cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s\n"; return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <class T> bool umin(T &a, T b) { return a > b ? (a = b, true) : false; } template <class T> bool umax(T &a, T b) { return a < b ? (a = b, true) : false; } long long POW(long long a, long long p, long long M) { if (!p) return 1LL; long long T = POW(a, p / 2, M); T = T * T % M; if (p & 1) T = T * (a % M) % M; return T; } long long SQRT(long long a) { long long b = (long long)sqrtl(((double)a) + 0.5); while (b * b < a) ++b; while (b * b > a) --b; return (b * b == a) ? b : -1; } const long long MOD = 1e9 + 7; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.precision(10); cout << fixed; int q; cin >> q; while (q--) { int n; cin >> n; long long s; cin >> s; vector<pair<long long, long long>> v(n); for (int i = 0; i < n; i++) cin >> v[i].first >> v[i].second; sort((v).begin(), (v).end()); long long L = v[n / 2].first; long long R = 1e15; long long ff = L; while (L <= R) { long long mid = L + R; mid /= 2; long long f = 0; int ct = 0; bool ps = false; vector<long long> tt; for (auto i : v) { if (i.first > mid) { f += i.first; ct++; } else if (i.second < mid) f += i.first; else tt.push_back(i.first); } if (tt.size()) { ct++; ps = true; f += mid; tt.pop_back(); } while (ct < (n + 1) / 2 && tt.size()) { ct++; f += mid; tt.pop_back(); } for (auto i : tt) f += i; if (f <= s && ps && ct == (n + 1) / 2) { L = mid + 1; ff = mid; } else R = mid - 1; } cout << ff << endl; } cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s\n"; return 0; } ```
#include <bits/stdc++.h> using namespace std; const double eps = 1e-10; const int INF = 0x3f3f3f3f, N = 1000005, M = 2000005, MOD = 2017; int sgn(double x) { return x < -eps ? -1 : (x < eps ? 0 : 1); } int Rand(int x) { return rand() * rand() % x + 1; } struct node { int l, r; bool operator<(const node &x) const { return l > x.l; } }; int n; long long s; vector<node> a; int check(int mid) { long long now = 0; int count = 0; for (auto p : a) now += p.l; for (auto p : a) { if (p.r >= mid && count < (n + 1) / 2) { count++; now += max(0, mid - p.l); } } return count == (n + 1) / 2 && now <= s; } int Solve() { cin >> n >> s; int maxn = 0; for (int i = 1; i <= n; i++) { int l, r; cin >> l >> r; maxn = max(maxn, r); a.push_back({l, r}); } sort(a.begin(), a.end()); int l = 0, r = maxn, mid, ans = -1; while (l <= r) { mid = l + r >> 1; if (check(mid)) ans = mid, l = mid + 1; else r = mid - 1; } cout << ans << endl; a.clear(); return 0; } void Pre() {} int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); Pre(); int cas; cin >> cas; while (cas--) Solve(); return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const double eps = 1e-10; const int INF = 0x3f3f3f3f, N = 1000005, M = 2000005, MOD = 2017; int sgn(double x) { return x < -eps ? -1 : (x < eps ? 0 : 1); } int Rand(int x) { return rand() * rand() % x + 1; } struct node { int l, r; bool operator<(const node &x) const { return l > x.l; } }; int n; long long s; vector<node> a; int check(int mid) { long long now = 0; int count = 0; for (auto p : a) now += p.l; for (auto p : a) { if (p.r >= mid && count < (n + 1) / 2) { count++; now += max(0, mid - p.l); } } return count == (n + 1) / 2 && now <= s; } int Solve() { cin >> n >> s; int maxn = 0; for (int i = 1; i <= n; i++) { int l, r; cin >> l >> r; maxn = max(maxn, r); a.push_back({l, r}); } sort(a.begin(), a.end()); int l = 0, r = maxn, mid, ans = -1; while (l <= r) { mid = l + r >> 1; if (check(mid)) ans = mid, l = mid + 1; else r = mid - 1; } cout << ans << endl; a.clear(); return 0; } void Pre() {} int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); Pre(); int cas; cin >> cas; while (cas--) Solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long MAXN = 2e5 + 5; pair<long long, long long> a[MAXN]; long long s; long long n; bool check(long long x) { long long sum = 0, cnt = 0; vector<long long> nums; for (long long i = 0; i < n; i++) { if (a[i].first >= x) { sum += a[i].first; cnt++; } else { if (a[i].second < x) { sum += a[i].first; } else { nums.push_back(a[i].first); } } } long long left = max((long long)0, (n + 1) / 2 - cnt); if (left > nums.size()) return 0; for (long long i = 0; i < nums.size() - left; i++) { sum += nums[i]; } for (long long i = nums.size() - left; i < nums.size(); i++) { sum += x; } return (sum <= s); } int main() { long long q; cin >> q; while (q--) { cin >> n >> s; for (long long i = 0; i < n; i++) { cin >> a[i].first >> a[i].second; } sort(a, a + n); long long l = 1, r = 1e18; while (l + 1 < r) { long long m = (l + r) >> 1; if (check(m)) { l = m; } else { r = m; } } cout << l << "\n"; } return 0; }
### Prompt Please create a solution in CPP to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long MAXN = 2e5 + 5; pair<long long, long long> a[MAXN]; long long s; long long n; bool check(long long x) { long long sum = 0, cnt = 0; vector<long long> nums; for (long long i = 0; i < n; i++) { if (a[i].first >= x) { sum += a[i].first; cnt++; } else { if (a[i].second < x) { sum += a[i].first; } else { nums.push_back(a[i].first); } } } long long left = max((long long)0, (n + 1) / 2 - cnt); if (left > nums.size()) return 0; for (long long i = 0; i < nums.size() - left; i++) { sum += nums[i]; } for (long long i = nums.size() - left; i < nums.size(); i++) { sum += x; } return (sum <= s); } int main() { long long q; cin >> q; while (q--) { cin >> n >> s; for (long long i = 0; i < n; i++) { cin >> a[i].first >> a[i].second; } sort(a, a + n); long long l = 1, r = 1e18; while (l + 1 < r) { long long m = (l + r) >> 1; if (check(m)) { l = m; } else { r = m; } } cout << l << "\n"; } return 0; } ```
#include <bits/stdc++.h> using pp = std::pair<int, int>; int main() { std::ios::sync_with_stdio(false); int t = 0; std::cin >> t; for (int k = 0; k < t; ++k) { int n = 0; long long s = 0, sum = 0; std::cin >> n >> s; pp p[n]; for (int i = 0; i < n; ++i) { std::cin >> p[i].first >> p[i].second; sum += p[i].first; } if (n == 1) { std::cout << std::min(s, (long long)p[0].second) << "\n"; continue; } std::sort(p, p + n, [](pp const& a, pp const& b) { return a.second < b.second; }); int m = n / 2; long long border = s - sum; int a = 0, b = p[m].second; for (; a < b;) { int c = (a + b + 1) / 2; int x = std::lower_bound(p, p + n, (pp){c, c}, [](pp const& a, pp const& b) { return a.second < b.second; }) - p; if (x > m) { b = c - 1; continue; } pp rem[n]; for (int i = x; i < n; ++i) rem[i] = p[i]; std::sort(rem + x, rem + n, [](pp const& a, pp const& b) { return a.first < b.first; }); long long ds = 0; for (int i = m; i < n && rem[i].first < c; ++i) { ds += c - rem[i].first; } if (ds <= border) { a = c; } else { b = c - 1; } } std::cout << a << "\n"; } }
### Prompt Construct a Cpp code solution to the problem outlined: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using pp = std::pair<int, int>; int main() { std::ios::sync_with_stdio(false); int t = 0; std::cin >> t; for (int k = 0; k < t; ++k) { int n = 0; long long s = 0, sum = 0; std::cin >> n >> s; pp p[n]; for (int i = 0; i < n; ++i) { std::cin >> p[i].first >> p[i].second; sum += p[i].first; } if (n == 1) { std::cout << std::min(s, (long long)p[0].second) << "\n"; continue; } std::sort(p, p + n, [](pp const& a, pp const& b) { return a.second < b.second; }); int m = n / 2; long long border = s - sum; int a = 0, b = p[m].second; for (; a < b;) { int c = (a + b + 1) / 2; int x = std::lower_bound(p, p + n, (pp){c, c}, [](pp const& a, pp const& b) { return a.second < b.second; }) - p; if (x > m) { b = c - 1; continue; } pp rem[n]; for (int i = x; i < n; ++i) rem[i] = p[i]; std::sort(rem + x, rem + n, [](pp const& a, pp const& b) { return a.first < b.first; }); long long ds = 0; for (int i = m; i < n && rem[i].first < c; ++i) { ds += c - rem[i].first; } if (ds <= border) { a = c; } else { b = c - 1; } } std::cout << a << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; const long double PI = 2 * acos(0.0); void yes() { cout << "YES\n"; } void no() { cout << "NO\n"; cout << -1 << "\n"; exit(0); } long long m(long long a) { return (a + 1000000007) % 1000000007; } long long cel(long long x1, long long y1) { ; if ((x1 % y1) == 0) return x1 / y1; return x1 / y1 + 1; } long long power(long long a, long long b) { if (b == 0) return 1; long long d = power(a, b / 2); d = m(d * d); if (b & 1) d = m(d * a); return d; } bool check(long long mid, long long s, vector<long long> &l, vector<long long> &r) { long long n = l.size(); vector<pair<long long, long long>> vec; for (long long i = 0; i < n; i++) { s -= l[i]; if (l[i] < mid) { vec.push_back({l[i], r[i]}); } } if (vec.size() <= (n / 2)) return true; sort(vec.begin(), vec.end()); reverse(vec.begin(), vec.end()); long long l1 = vec.size() - (n / 2); long long c = 0; for (long long i = 0; i < vec.size(); i++) { if (c == l1) break; if (vec[i].second >= mid) { s -= (mid - vec[i].first); c++; } } if (s >= 0 && c == l1) return true; return false; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t; cin >> t; while (t--) { long long n, s; cin >> n >> s; vector<long long> l(n), r(n); for (long long i = 0; i < n; i++) { cin >> l[i] >> r[i]; } long long l1 = 0, f = s; long long ans = 0; while (l1 <= f) { long long mid = (l1 + f) / 2; if (check(mid, s, l, r)) { ans = mid; l1 = mid + 1; } else f = mid - 1; } cout << ans << "\n"; } }
### Prompt Your challenge is to write a Cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long double PI = 2 * acos(0.0); void yes() { cout << "YES\n"; } void no() { cout << "NO\n"; cout << -1 << "\n"; exit(0); } long long m(long long a) { return (a + 1000000007) % 1000000007; } long long cel(long long x1, long long y1) { ; if ((x1 % y1) == 0) return x1 / y1; return x1 / y1 + 1; } long long power(long long a, long long b) { if (b == 0) return 1; long long d = power(a, b / 2); d = m(d * d); if (b & 1) d = m(d * a); return d; } bool check(long long mid, long long s, vector<long long> &l, vector<long long> &r) { long long n = l.size(); vector<pair<long long, long long>> vec; for (long long i = 0; i < n; i++) { s -= l[i]; if (l[i] < mid) { vec.push_back({l[i], r[i]}); } } if (vec.size() <= (n / 2)) return true; sort(vec.begin(), vec.end()); reverse(vec.begin(), vec.end()); long long l1 = vec.size() - (n / 2); long long c = 0; for (long long i = 0; i < vec.size(); i++) { if (c == l1) break; if (vec[i].second >= mid) { s -= (mid - vec[i].first); c++; } } if (s >= 0 && c == l1) return true; return false; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t; cin >> t; while (t--) { long long n, s; cin >> n >> s; vector<long long> l(n), r(n); for (long long i = 0; i < n; i++) { cin >> l[i] >> r[i]; } long long l1 = 0, f = s; long long ans = 0; while (l1 <= f) { long long mid = (l1 + f) / 2; if (check(mid, s, l, r)) { ans = mid; l1 = mid + 1; } else f = mid - 1; } cout << ans << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; long long t, n, m; struct p { long long l, r; bool operator<(const p& tmp) const { return l < tmp.l; } } a[maxn]; bool col(p a, p b) { return a.l < b.l; } bool cor(p a, p b) { return a.r > b.r; } bool isok(long long mid) { priority_queue<p> q; long long temp = 0, k = n / 2 + 1; for (int i = n; i >= 1; i--) { if (a[i].r >= mid && a[i].l <= mid) q.push(a[i]); else if (a[i].l > mid) temp += a[i].l, k--; else temp += a[i].l; } if (k < 0) return false; while (!q.empty()) { p u = q.top(); q.pop(); if (k) k--, temp += mid; else temp += u.l; } if (temp > m || k != 0) return false; else return true; } int main() { cin >> t; while (t--) { long long l = 1e9, r = 0, ans = -1; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i].l >> a[i].r; sort(a + 1, a + 1 + n, col); l = a[n / 2 + 1].l; sort(a + 1, a + 1 + n, cor); r = a[n / 2 + 1].r; while (r >= l) { long long mid = l + r >> 1; if (isok(mid)) l = mid + 1, ans = mid; else r = mid - 1; } cout << ans << endl; } }
### Prompt Please create a solution in CPP to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; long long t, n, m; struct p { long long l, r; bool operator<(const p& tmp) const { return l < tmp.l; } } a[maxn]; bool col(p a, p b) { return a.l < b.l; } bool cor(p a, p b) { return a.r > b.r; } bool isok(long long mid) { priority_queue<p> q; long long temp = 0, k = n / 2 + 1; for (int i = n; i >= 1; i--) { if (a[i].r >= mid && a[i].l <= mid) q.push(a[i]); else if (a[i].l > mid) temp += a[i].l, k--; else temp += a[i].l; } if (k < 0) return false; while (!q.empty()) { p u = q.top(); q.pop(); if (k) k--, temp += mid; else temp += u.l; } if (temp > m || k != 0) return false; else return true; } int main() { cin >> t; while (t--) { long long l = 1e9, r = 0, ans = -1; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i].l >> a[i].r; sort(a + 1, a + 1 + n, col); l = a[n / 2 + 1].l; sort(a + 1, a + 1 + n, cor); r = a[n / 2 + 1].r; while (r >= l) { long long mid = l + r >> 1; if (isok(mid)) l = mid + 1, ans = mid; else r = mid - 1; } cout << ans << endl; } } ```
#include <bits/stdc++.h> using namespace std; const long long inf = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; const int maxn = 2e6 + 4; const int N = 5e3 + 5; const double eps = 1e-6; const double pi = acos(-1); long long qpow(long long x, long long y) { long long ans = 1; x %= mod; while (y) { if (y & 1) ans = ans * x % mod; x = x * x % mod; y >>= 1; } return ans; } struct node { int l, r; } a[maxn]; int _, n; long long s; bool cmp(node a, node b) { return a.l > b.l; } bool ok(long long m) { long long cnt = 0, res = 0; for (int i = 1; i <= n; i++) { if (a[i].l <= m && m <= a[i].r) { if (cnt < n / 2 + 1) res += m, cnt++; else res += a[i].l; } else { res += a[i].l; if (a[i].l >= m) cnt++; } } if (cnt < n / 2 + 1) return 0; return res <= s; } int main() { for (scanf("%d", &_); _; _--) { scanf("%d%lld", &n, &s); for (int i = 1; i <= n; i++) { scanf("%d%d", &a[i].l, &a[i].r); } sort(a + 1, a + 1 + n, cmp); long long l = 0, r = 1e9; long long ans = l; while (l <= r) { long long mid = l + r >> 1; if (ok(mid)) l = mid + 1, ans = mid; else r = mid - 1; } printf("%lld\n", ans); } }
### Prompt In CPP, your task is to solve the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long inf = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; const int maxn = 2e6 + 4; const int N = 5e3 + 5; const double eps = 1e-6; const double pi = acos(-1); long long qpow(long long x, long long y) { long long ans = 1; x %= mod; while (y) { if (y & 1) ans = ans * x % mod; x = x * x % mod; y >>= 1; } return ans; } struct node { int l, r; } a[maxn]; int _, n; long long s; bool cmp(node a, node b) { return a.l > b.l; } bool ok(long long m) { long long cnt = 0, res = 0; for (int i = 1; i <= n; i++) { if (a[i].l <= m && m <= a[i].r) { if (cnt < n / 2 + 1) res += m, cnt++; else res += a[i].l; } else { res += a[i].l; if (a[i].l >= m) cnt++; } } if (cnt < n / 2 + 1) return 0; return res <= s; } int main() { for (scanf("%d", &_); _; _--) { scanf("%d%lld", &n, &s); for (int i = 1; i <= n; i++) { scanf("%d%d", &a[i].l, &a[i].r); } sort(a + 1, a + 1 + n, cmp); long long l = 0, r = 1e9; long long ans = l; while (l <= r) { long long mid = l + r >> 1; if (ok(mid)) l = mid + 1, ans = mid; else r = mid - 1; } printf("%lld\n", ans); } } ```
#include <bits/stdc++.h> using namespace std; const long long int infinity = 9e18; pair<bool, long long int> isPossible( vector<pair<long long int, long long int> > &vp, long long int k, long long int s) { long long int n = vp.size(); vector<long long int> v, v1, v2; long long int a = 0, b = 0, c = 0; long long int sum = 0; for (auto i : vp) { if (i.first <= k && k <= i.second) { v.push_back(i.first); } else if (i.second < k) { a++; sum += i.first; } else if (i.first > k) { b++; sum += i.first; } } long long int x = (n + 1) / 2; if (a >= x) { return {1, -1}; } if (b >= x) { return {1, 1}; } long long int y = ((x - 1) - a); sort(v.begin(), v.end()); for (int i = 0; i < y; i++) { sum += v[i]; } sum += k * (v.size() - y); return {sum <= s, 0}; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; for (int idx = 0; idx < t; idx++) { long long int n, s; cin >> n >> s; vector<pair<long long int, long long int> > vp(n); for (int i = 0; i < n; i++) { cin >> vp[i].first >> vp[i].second; } long long int st = 1, end = s; long long int ans = 0; while (st <= end) { long long int mid = (st + end) / 2; pair<bool, long long int> p = isPossible(vp, mid, s); if (p.first) { if (p.second == -1) { end = mid - 1; } else if (p.second == 0) { ans = mid; st = mid + 1; } else if (p.second == 1) { st = mid + 1; } } else { end = mid - 1; } } cout << ans << "\n"; } return 0; }
### Prompt Please formulate a cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long int infinity = 9e18; pair<bool, long long int> isPossible( vector<pair<long long int, long long int> > &vp, long long int k, long long int s) { long long int n = vp.size(); vector<long long int> v, v1, v2; long long int a = 0, b = 0, c = 0; long long int sum = 0; for (auto i : vp) { if (i.first <= k && k <= i.second) { v.push_back(i.first); } else if (i.second < k) { a++; sum += i.first; } else if (i.first > k) { b++; sum += i.first; } } long long int x = (n + 1) / 2; if (a >= x) { return {1, -1}; } if (b >= x) { return {1, 1}; } long long int y = ((x - 1) - a); sort(v.begin(), v.end()); for (int i = 0; i < y; i++) { sum += v[i]; } sum += k * (v.size() - y); return {sum <= s, 0}; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; for (int idx = 0; idx < t; idx++) { long long int n, s; cin >> n >> s; vector<pair<long long int, long long int> > vp(n); for (int i = 0; i < n; i++) { cin >> vp[i].first >> vp[i].second; } long long int st = 1, end = s; long long int ans = 0; while (st <= end) { long long int mid = (st + end) / 2; pair<bool, long long int> p = isPossible(vp, mid, s); if (p.first) { if (p.second == -1) { end = mid - 1; } else if (p.second == 0) { ans = mid; st = mid + 1; } else if (p.second == 1) { st = mid + 1; } } else { end = mid - 1; } } cout << ans << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; bool canDoAtLeast(long long m, long long s, vector<pair<long long, long long> > &E) { long long good = 0; vector<long long> P; for (auto [l, r] : E) { s -= l; if (l >= m) good++; else if (m <= r) { P.push_back(m - l); } } sort(P.begin(), P.end()); reverse(P.begin(), P.end()); while (good <= (int)E.size() / 2) { if (P.empty()) return false; good++; s -= P.back(); P.pop_back(); } return s >= 0; } void solve() { long long n, s; cin >> n >> s; vector<pair<long long, long long> > E(n); for (int i = 0; i < n; i++) { cin >> E[i].first >> E[i].second; } long long st = 1; long long fn = s; while (st <= fn) { long long mid = st + (fn - st) / 2; if (canDoAtLeast(mid, s, E)) { st = mid + 1; } else { fn = mid - 1; } } cout << fn << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t; cin >> t; for (int i = 0; i < t; i++) solve(); }
### Prompt Please provide a Cpp coded solution to the problem described below: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool canDoAtLeast(long long m, long long s, vector<pair<long long, long long> > &E) { long long good = 0; vector<long long> P; for (auto [l, r] : E) { s -= l; if (l >= m) good++; else if (m <= r) { P.push_back(m - l); } } sort(P.begin(), P.end()); reverse(P.begin(), P.end()); while (good <= (int)E.size() / 2) { if (P.empty()) return false; good++; s -= P.back(); P.pop_back(); } return s >= 0; } void solve() { long long n, s; cin >> n >> s; vector<pair<long long, long long> > E(n); for (int i = 0; i < n; i++) { cin >> E[i].first >> E[i].second; } long long st = 1; long long fn = s; while (st <= fn) { long long mid = st + (fn - st) / 2; if (canDoAtLeast(mid, s, E)) { st = mid + 1; } else { fn = mid - 1; } } cout << fn << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t; cin >> t; for (int i = 0; i < t; i++) solve(); } ```
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; using vi = vector<int>; using ll = long long; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n; ll s; cin >> n >> s; vi l(n), r(n); for (int i = int(0); i < int(n); i++) cin >> l[i] >> r[i]; for (int i = int(0); i < int(n); i++) s -= l[i]; vi ordered_r = r; sort(ordered_r.begin(), ordered_r.end()); int max_median = ordered_r[n / 2]; vi inds_by_l(n); iota(inds_by_l.begin(), inds_by_l.end(), 0); sort(inds_by_l.begin(), inds_by_l.end(), [&](int i, int j) { return l[i] > l[j]; }); auto can = [&](int median) { ll left_s = s; int cant = 0; for (int i : inds_by_l) if (l[i] >= median) cant++; else if (r[i] >= median) { int dif = median - l[i]; if (left_s >= dif) left_s -= dif, cant++; else break; } return cant > n / 2; }; int lo = 0, hi = max_median; if (can(hi)) { cout << hi << '\n'; continue; } while (hi - lo > 1) { int mid = (lo + hi) / 2; if (can(mid)) lo = mid; else hi = mid; } cout << lo << '\n'; } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; using vi = vector<int>; using ll = long long; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n; ll s; cin >> n >> s; vi l(n), r(n); for (int i = int(0); i < int(n); i++) cin >> l[i] >> r[i]; for (int i = int(0); i < int(n); i++) s -= l[i]; vi ordered_r = r; sort(ordered_r.begin(), ordered_r.end()); int max_median = ordered_r[n / 2]; vi inds_by_l(n); iota(inds_by_l.begin(), inds_by_l.end(), 0); sort(inds_by_l.begin(), inds_by_l.end(), [&](int i, int j) { return l[i] > l[j]; }); auto can = [&](int median) { ll left_s = s; int cant = 0; for (int i : inds_by_l) if (l[i] >= median) cant++; else if (r[i] >= median) { int dif = median - l[i]; if (left_s >= dif) left_s -= dif, cant++; else break; } return cant > n / 2; }; int lo = 0, hi = max_median; if (can(hi)) { cout << hi << '\n'; continue; } while (hi - lo > 1) { int mid = (lo + hi) / 2; if (can(mid)) lo = mid; else hi = mid; } cout << lo << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; #pragma GCC optimization("Ofast") #pragma GCC optimization("unroll-loops") #pragma GCC target("avx2,avx,fma") int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int i, j, t; cin >> t; for (i = 0; i < t; i++) { int n; long long int s; cin >> n >> s; vector<pair<long long int, long long int> > v; long long int l, r; for (j = 0; j < n; j++) { cin >> l >> r; v.push_back(make_pair(l, r)); } sort(v.rbegin(), v.rend()); l = 1; r = s; while (l <= r) { long long int mid = (l + r) / 2; long long int sum = 0; int k = 0; for (j = 0; j < n; j++) { if (v[j].second >= mid && k < n / 2 + 1) { sum += max(v[j].first, mid); k++; } else sum += v[j].first; } if (k < (n / 2 + 1) || sum > s) r = mid - 1; else l = mid + 1; } cout << r << "\n"; } return 0; }
### Prompt Please formulate a cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; #pragma GCC optimization("Ofast") #pragma GCC optimization("unroll-loops") #pragma GCC target("avx2,avx,fma") int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int i, j, t; cin >> t; for (i = 0; i < t; i++) { int n; long long int s; cin >> n >> s; vector<pair<long long int, long long int> > v; long long int l, r; for (j = 0; j < n; j++) { cin >> l >> r; v.push_back(make_pair(l, r)); } sort(v.rbegin(), v.rend()); l = 1; r = s; while (l <= r) { long long int mid = (l + r) / 2; long long int sum = 0; int k = 0; for (j = 0; j < n; j++) { if (v[j].second >= mid && k < n / 2 + 1) { sum += max(v[j].first, mid); k++; } else sum += v[j].first; } if (k < (n / 2 + 1) || sum > s) r = mid - 1; else l = mid + 1; } cout << r << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; struct num { long long l, r; } a[200005]; bool cmp(num x, num y) { if (x.l == y.l) return x.r > y.r; return x.l > y.l; } long long n, s; long long check(long long m) { long long br = n / 2 + 1, sum = 0; for (int i = 0; i < n; i++) { if (a[i].l <= m && m <= a[i].r && br) { br--; sum += m; } else { sum += a[i].l; if (a[i].l > m) br--; } } if (br) return s + 1; return sum; } int main() { int q; cin >> q; while (q--) { cin >> n >> s; for (long long i = 0; i < n; i++) { cin >> a[i].l >> a[i].r; } sort(a, a + n, cmp); long long l = a[n / 2].l + 1, r = s; while (l <= r) { long long m = (l + r) >> 1; if (check(m) <= s) l = m + 1; else r = m - 1; } cout << l - 1 << endl; } }
### Prompt In cpp, your task is to solve the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct num { long long l, r; } a[200005]; bool cmp(num x, num y) { if (x.l == y.l) return x.r > y.r; return x.l > y.l; } long long n, s; long long check(long long m) { long long br = n / 2 + 1, sum = 0; for (int i = 0; i < n; i++) { if (a[i].l <= m && m <= a[i].r && br) { br--; sum += m; } else { sum += a[i].l; if (a[i].l > m) br--; } } if (br) return s + 1; return sum; } int main() { int q; cin >> q; while (q--) { cin >> n >> s; for (long long i = 0; i < n; i++) { cin >> a[i].l >> a[i].r; } sort(a, a + n, cmp); long long l = a[n / 2].l + 1, r = s; while (l <= r) { long long m = (l + r) >> 1; if (check(m) <= s) l = m + 1; else r = m - 1; } cout << l - 1 << endl; } } ```
#include <bits/stdc++.h> using namespace std; long long n, s, INI; pair<long long, long long> a[200010]; bool op(long long meta) { long long quant = 0, disponivel = s; for (long long i = n; i >= 1; i--) { long long vez = meta - a[i].first; if (a[i].first >= meta) { quant++; continue; } if (a[i].second < meta) continue; if (vez > disponivel) continue; disponivel -= vez; quant++; } long long precisa = n / 2; precisa++; if (quant >= precisa) return true; else return false; } void solve() { cin >> n >> INI; for (long long i = 1; i <= n; i++) cin >> a[i].first >> a[i].second; sort(a + 1, a + n + 1); s = INI; for (long long i = 1; i <= n; i++) s -= a[i].first; long long ini = 1, fim = 200000000000000; while (ini <= fim) { long long meio = (ini + fim) / 2; if (op(meio)) { bool prox = op(meio + 1); if (!prox) { cout << meio << "\n"; return; } ini = meio + 1; } else { bool ant = op(meio - 1); if (ant) { cout << meio - 1 << "\n"; return; } fim = meio - 1; } } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); long long t; cin >> t; for (long long i = 1; i <= t; i++) solve(); }
### Prompt In CPP, your task is to solve the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, s, INI; pair<long long, long long> a[200010]; bool op(long long meta) { long long quant = 0, disponivel = s; for (long long i = n; i >= 1; i--) { long long vez = meta - a[i].first; if (a[i].first >= meta) { quant++; continue; } if (a[i].second < meta) continue; if (vez > disponivel) continue; disponivel -= vez; quant++; } long long precisa = n / 2; precisa++; if (quant >= precisa) return true; else return false; } void solve() { cin >> n >> INI; for (long long i = 1; i <= n; i++) cin >> a[i].first >> a[i].second; sort(a + 1, a + n + 1); s = INI; for (long long i = 1; i <= n; i++) s -= a[i].first; long long ini = 1, fim = 200000000000000; while (ini <= fim) { long long meio = (ini + fim) / 2; if (op(meio)) { bool prox = op(meio + 1); if (!prox) { cout << meio << "\n"; return; } ini = meio + 1; } else { bool ant = op(meio - 1); if (ant) { cout << meio - 1 << "\n"; return; } fim = meio - 1; } } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); long long t; cin >> t; for (long long i = 1; i <= t; i++) solve(); } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 200020; inline long long read() { char ch = getchar(); long long x = 0, f = 0; while (ch < '0' || ch > '9') f |= ch == '-', ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return f ? -x : x; } struct seg { int l, r; bool operator<(const seg &s) const { return l < s.l; } } s[maxn]; int t, n, tmp[maxn], tl; long long sum; bool check(int x) { long long tot = 0; int rem = (n + 1) / 2; tl = 0; for (int i = (1); i <= (n); i++) { if (s[i].r < x) tot += s[i].l; if (s[i].l >= x) tot += s[i].l, rem--; if (s[i].r >= x && s[i].l < x) tmp[++tl] = s[i].l; } rem = max(rem, 0); if (tl < rem) return false; sort(tmp + 1, tmp + tl + 1); for (int i = (1); i <= (tl - rem); i++) tot += tmp[i]; for (int i = (tl - rem + 1); i <= (tl); i++) tot += x; return tot <= sum; } int main() { t = read(); while (t--) { n = read(); sum = read(); for (int i = (1); i <= (n); i++) s[i].l = read(), s[i].r = read(); int l = 1, r = 1e9; while (l < r) { int mid = (l + r + 1) >> 1; if (check(mid)) l = mid; else r = mid - 1; } printf("%d\n", l); } }
### Prompt Please provide a CPP coded solution to the problem described below: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 200020; inline long long read() { char ch = getchar(); long long x = 0, f = 0; while (ch < '0' || ch > '9') f |= ch == '-', ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return f ? -x : x; } struct seg { int l, r; bool operator<(const seg &s) const { return l < s.l; } } s[maxn]; int t, n, tmp[maxn], tl; long long sum; bool check(int x) { long long tot = 0; int rem = (n + 1) / 2; tl = 0; for (int i = (1); i <= (n); i++) { if (s[i].r < x) tot += s[i].l; if (s[i].l >= x) tot += s[i].l, rem--; if (s[i].r >= x && s[i].l < x) tmp[++tl] = s[i].l; } rem = max(rem, 0); if (tl < rem) return false; sort(tmp + 1, tmp + tl + 1); for (int i = (1); i <= (tl - rem); i++) tot += tmp[i]; for (int i = (tl - rem + 1); i <= (tl); i++) tot += x; return tot <= sum; } int main() { t = read(); while (t--) { n = read(); sum = read(); for (int i = (1); i <= (n); i++) s[i].l = read(), s[i].r = read(); int l = 1, r = 1e9; while (l < r) { int mid = (l + r + 1) >> 1; if (check(mid)) l = mid; else r = mid - 1; } printf("%d\n", l); } } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 1; const int M = 1e9 + 7; vector<pair<int, int> > seg; bool check(long long n, long long s, long long median) { long long less = 0, more = 0; long long cur = 0; vector<int> v; for (int i = 0; i < n; i++) { if (seg[i].second < median) { cur += seg[i].first; } else if (median <= seg[i].first) { cur += seg[i].first; more++; } else { v.push_back(seg[i].first); } } sort(v.begin(), v.end(), greater<int>()); int rem = max(0LL, (n + 1) / 2 - more); if ((int)v.size() < rem) return false; for (auto val : v) { if (rem > 0) { cur += median; rem--; } else { cur += val; } } return cur <= s; } void solve() { long long n, s; cin >> n >> s; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; seg.push_back({x, y}); } sort(seg.begin(), seg.end()); long long lo = seg[n / 2].first, hi = s + 1; while (hi - lo > 1) { long long mid = lo + hi >> 1; if (check(n, s, mid)) lo = mid; else hi = mid; } cout << lo << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { solve(); seg.clear(); } return 0; }
### Prompt In CPP, your task is to solve the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 1; const int M = 1e9 + 7; vector<pair<int, int> > seg; bool check(long long n, long long s, long long median) { long long less = 0, more = 0; long long cur = 0; vector<int> v; for (int i = 0; i < n; i++) { if (seg[i].second < median) { cur += seg[i].first; } else if (median <= seg[i].first) { cur += seg[i].first; more++; } else { v.push_back(seg[i].first); } } sort(v.begin(), v.end(), greater<int>()); int rem = max(0LL, (n + 1) / 2 - more); if ((int)v.size() < rem) return false; for (auto val : v) { if (rem > 0) { cur += median; rem--; } else { cur += val; } } return cur <= s; } void solve() { long long n, s; cin >> n >> s; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; seg.push_back({x, y}); } sort(seg.begin(), seg.end()); long long lo = seg[n / 2].first, hi = s + 1; while (hi - lo > 1) { long long mid = lo + hi >> 1; if (check(n, s, mid)) lo = mid; else hi = mid; } cout << lo << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { solve(); seg.clear(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const double eps = 1e-6; const int mod = 1e9 + 7; const int maxn = 2e6 + 100; const int maxm = 2e6 + 100; const int inf = 0x3f3f3f3f; const double pi = acos(-1.0); int t; pair<long long, long long> a[maxn]; long long s; int n; int vis[maxn]; bool cmp2(pair<long long, long long> a, pair<long long, long long> b) { return a.second < b.second; } vector<long long> mm, rr, lll; int ck(long long x) { int L = 0, R = 0, M = 0; lll.clear(); rr.clear(); mm.clear(); for (int i = 1; i <= n; i++) { if (a[i].first <= x && a[i].second >= x) { mm.push_back(a[i].first); M++; continue; } if (a[i].first > x) { R++; rr.push_back(a[i].first); } if (a[i].second < x) { L++; lll.push_back(a[i].first); } } if (L > n / 2) return false; long long res = 0; sort(mm.begin(), mm.end()); if (L < n / 2) { int to = M; for (int i = 0; i < to; i++) { lll.push_back(mm[i]); L++; M--; if (L == n / 2) break; } } if (R < n / 2 + 1) { if (R + M != n / 2 + 1) return false; res += 1ll * M * x; } for (int i = 0; i < (int)lll.size(); i++) { res += lll[i]; } for (int i = 0; i < (int)rr.size(); i++) { res += max(rr[i], x); } return res <= s; } int main() { scanf("%d", &t); while (t--) { long long mx = 0; scanf("%d %lld", &n, &s); for (int i = 1; i <= n; i++) { scanf("%lld %lld", &a[i].first, &a[i].second); mx = max(mx, a[i].second); } long long ans = 0; sort(a + 1, a + 1 + n); long long l = a[n / 2 + 1].first; sort(a + 1, a + 1 + n, cmp2); long long r = a[n / 2 + 1].second; while (l <= r) { long long mid = l + r >> 1; if (ck(mid)) { l = mid + 1; ans = mid; } else r = mid - 1; } printf("%lld\n", ans); } return 0; }
### Prompt Create a solution in cpp for the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const double eps = 1e-6; const int mod = 1e9 + 7; const int maxn = 2e6 + 100; const int maxm = 2e6 + 100; const int inf = 0x3f3f3f3f; const double pi = acos(-1.0); int t; pair<long long, long long> a[maxn]; long long s; int n; int vis[maxn]; bool cmp2(pair<long long, long long> a, pair<long long, long long> b) { return a.second < b.second; } vector<long long> mm, rr, lll; int ck(long long x) { int L = 0, R = 0, M = 0; lll.clear(); rr.clear(); mm.clear(); for (int i = 1; i <= n; i++) { if (a[i].first <= x && a[i].second >= x) { mm.push_back(a[i].first); M++; continue; } if (a[i].first > x) { R++; rr.push_back(a[i].first); } if (a[i].second < x) { L++; lll.push_back(a[i].first); } } if (L > n / 2) return false; long long res = 0; sort(mm.begin(), mm.end()); if (L < n / 2) { int to = M; for (int i = 0; i < to; i++) { lll.push_back(mm[i]); L++; M--; if (L == n / 2) break; } } if (R < n / 2 + 1) { if (R + M != n / 2 + 1) return false; res += 1ll * M * x; } for (int i = 0; i < (int)lll.size(); i++) { res += lll[i]; } for (int i = 0; i < (int)rr.size(); i++) { res += max(rr[i], x); } return res <= s; } int main() { scanf("%d", &t); while (t--) { long long mx = 0; scanf("%d %lld", &n, &s); for (int i = 1; i <= n; i++) { scanf("%lld %lld", &a[i].first, &a[i].second); mx = max(mx, a[i].second); } long long ans = 0; sort(a + 1, a + 1 + n); long long l = a[n / 2 + 1].first; sort(a + 1, a + 1 + n, cmp2); long long r = a[n / 2 + 1].second; while (l <= r) { long long mid = l + r >> 1; if (ck(mid)) { l = mid + 1; ans = mid; } else r = mid - 1; } printf("%lld\n", ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int n; long long s; pair<int, int> a[200010]; bool take[200010]; bool good(int x) { int cnt = n / 2; long long tot = 0; for (int i = 1; i <= n; i++) { if (a[i].second < x) { tot += a[i].first; --cnt; take[i] = true; } else take[i] = false; } if (cnt < 0) return false; for (int i = 1; i <= n; i++) { if (cnt == 0) break; if (take[i]) continue; tot += a[i].first; --cnt; take[i] = true; } for (int i = 1; i <= n; i++) { if (!take[i]) { tot += max(x, a[i].first); } } return tot <= s; } int search(int b, int e) { if (b == e) return b; int m = (b + e + 1) >> 1; if (good(m)) return search(m, e); else return search(b, m - 1); } void solve() { scanf("%d %lld", &n, &s); for (int i = 1; i <= n; i++) { scanf("%d %d", &a[i].first, &a[i].second); } sort(a + 1, a + n + 1); printf("%d\n", search(1, 1000000000)); } int main(int argc, char const *argv[]) { int t; cin >> t; while (t--) { solve(); } return 0; }
### Prompt Generate a cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n; long long s; pair<int, int> a[200010]; bool take[200010]; bool good(int x) { int cnt = n / 2; long long tot = 0; for (int i = 1; i <= n; i++) { if (a[i].second < x) { tot += a[i].first; --cnt; take[i] = true; } else take[i] = false; } if (cnt < 0) return false; for (int i = 1; i <= n; i++) { if (cnt == 0) break; if (take[i]) continue; tot += a[i].first; --cnt; take[i] = true; } for (int i = 1; i <= n; i++) { if (!take[i]) { tot += max(x, a[i].first); } } return tot <= s; } int search(int b, int e) { if (b == e) return b; int m = (b + e + 1) >> 1; if (good(m)) return search(m, e); else return search(b, m - 1); } void solve() { scanf("%d %lld", &n, &s); for (int i = 1; i <= n; i++) { scanf("%d %d", &a[i].first, &a[i].second); } sort(a + 1, a + n + 1); printf("%d\n", search(1, 1000000000)); } int main(int argc, char const *argv[]) { int t; cin >> t; while (t--) { solve(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; struct node { long long l, r; }; bool cmp(node a, node b) { if (a.l != b.l) return a.l < b.l; return a.r < b.r; } long long T, N, S; node a[200005]; bool can(long long x) { long long cnt = 0; long long money = 0; for (long long i = N; i >= 1; i--) { if (a[i].r < x) money += a[i].l; else if (cnt * 2 > N) money += a[i].l; else { long long pay = max(a[i].l, x); money += pay; cnt++; } } if (money <= S && cnt * 2 > N) return 1; return 0; } int main() { cin.tie(0); ios_base::sync_with_stdio(0); cin >> T; while (T--) { cin >> N >> S; for (long long i = 1; i <= N; i++) cin >> a[i].l >> a[i].r; sort(a + 1, a + N + 1, cmp); long long ini = 0; long long fin = 1e9; while (ini < fin) { long long mid = (ini + fin + 1) / 2; if (can(mid)) ini = mid; else fin = mid - 1; } cout << ini << "\n"; } return 0; }
### Prompt Please create a solution in Cpp to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct node { long long l, r; }; bool cmp(node a, node b) { if (a.l != b.l) return a.l < b.l; return a.r < b.r; } long long T, N, S; node a[200005]; bool can(long long x) { long long cnt = 0; long long money = 0; for (long long i = N; i >= 1; i--) { if (a[i].r < x) money += a[i].l; else if (cnt * 2 > N) money += a[i].l; else { long long pay = max(a[i].l, x); money += pay; cnt++; } } if (money <= S && cnt * 2 > N) return 1; return 0; } int main() { cin.tie(0); ios_base::sync_with_stdio(0); cin >> T; while (T--) { cin >> N >> S; for (long long i = 1; i <= N; i++) cin >> a[i].l >> a[i].r; sort(a + 1, a + N + 1, cmp); long long ini = 0; long long fin = 1e9; while (ini < fin) { long long mid = (ini + fin + 1) / 2; if (can(mid)) ini = mid; else fin = mid - 1; } cout << ini << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; const double PI = acos(-1); long long mod = 1e9 + 7; struct segment { long long l, r; bool operator<(const segment& s) const { return l < s.l; } }; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { long long n, s; cin >> n >> s; vector<segment> vs(n); for (long long i = 0; i < n; i++) cin >> vs[i].l >> vs[i].r; long long low = 1, high = s + 1; while (low < high) { long long mid = (low + high + 1) / 2; vector<segment> tmp; long long cost = 0, left = 0, right = 0; for (long long i = 0; i < n; i++) { if (vs[i].r < mid) { left++; cost += vs[i].l; } else if (vs[i].l > mid) { right++; cost += vs[i].l; } else tmp.push_back(vs[i]); } sort(tmp.begin(), tmp.end()); long long rem = n / 2 - left; for (long long i = 0; i < tmp.size(); i++) { if (rem > 0) { rem--; cost += tmp[i].l; left++; } else { cost += mid; right++; } } if (cost > s || left >= right) high = mid - 1; else low = mid; } cout << low << "\n"; } return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long INF = 1e18; const double PI = acos(-1); long long mod = 1e9 + 7; struct segment { long long l, r; bool operator<(const segment& s) const { return l < s.l; } }; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { long long n, s; cin >> n >> s; vector<segment> vs(n); for (long long i = 0; i < n; i++) cin >> vs[i].l >> vs[i].r; long long low = 1, high = s + 1; while (low < high) { long long mid = (low + high + 1) / 2; vector<segment> tmp; long long cost = 0, left = 0, right = 0; for (long long i = 0; i < n; i++) { if (vs[i].r < mid) { left++; cost += vs[i].l; } else if (vs[i].l > mid) { right++; cost += vs[i].l; } else tmp.push_back(vs[i]); } sort(tmp.begin(), tmp.end()); long long rem = n / 2 - left; for (long long i = 0; i < tmp.size(); i++) { if (rem > 0) { rem--; cost += tmp[i].l; left++; } else { cost += mid; right++; } } if (cost > s || left >= right) high = mid - 1; else low = mid; } cout << low << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long n, s, t, cnt, sum; const int maxn = 2e5 + 7; pair<long long, long long> a[maxn]; int main() { cin >> t; while (t--) { cin >> n >> s; for (int i = 1; i <= n; i++) { cin >> a[i].first >> a[i].second; } sort(a + 1, a + n + 1); long long l = 1; long long r = 1000000000; while (l <= r) { long long mid = (l + r) / 2; cnt = (n + 1) / 2; sum = 0; for (int i = n; i >= 1; i--) { if (a[i].second >= mid && cnt) { sum += max(a[i].first, mid); cnt--; } else sum += a[i].first; } if (sum <= s && cnt == 0) { l = mid + 1; } else r = mid - 1; } cout << l - 1 << endl; } }
### Prompt In Cpp, your task is to solve the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, s, t, cnt, sum; const int maxn = 2e5 + 7; pair<long long, long long> a[maxn]; int main() { cin >> t; while (t--) { cin >> n >> s; for (int i = 1; i <= n; i++) { cin >> a[i].first >> a[i].second; } sort(a + 1, a + n + 1); long long l = 1; long long r = 1000000000; while (l <= r) { long long mid = (l + r) / 2; cnt = (n + 1) / 2; sum = 0; for (int i = n; i >= 1; i--) { if (a[i].second >= mid && cnt) { sum += max(a[i].first, mid); cnt--; } else sum += a[i].first; } if (sum <= s && cnt == 0) { l = mid + 1; } else r = mid - 1; } cout << l - 1 << endl; } } ```
#include <bits/stdc++.h> using namespace std; int const N = 200000, inf = 1e9; pair<int, int> x[N]; int n; vector<int> y; bool ok(int m, long long s) { y.clear(); for (int i = 0; i < (int)(n); ++i) { s -= x[i].first; if (m <= x[i].second) { if (m >= x[i].first) y.push_back(m - x[i].first); else y.push_back(0); } } if (y.size() < n + 1 >> 1) return false; sort(y.begin(), y.end()); for (int i = 0; i < (int)(n + 1 >> 1); ++i) s -= y[i]; return s >= 0; } void solve() { long long s; scanf("%d%lld", &n, &s); for (int i = 0; i < (int)(n); ++i) scanf("%d%d", &x[i].first, &x[i].second); int l = 1, r = inf; while (r > l) { int m = l + r + 1 >> 1; if (ok(m, s)) l = m; else r = m - 1; } printf("%d\n", l); } int main() { int t; scanf("%d", &t); while (t--) solve(); }
### Prompt Develop a solution in CPP to the problem described below: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int const N = 200000, inf = 1e9; pair<int, int> x[N]; int n; vector<int> y; bool ok(int m, long long s) { y.clear(); for (int i = 0; i < (int)(n); ++i) { s -= x[i].first; if (m <= x[i].second) { if (m >= x[i].first) y.push_back(m - x[i].first); else y.push_back(0); } } if (y.size() < n + 1 >> 1) return false; sort(y.begin(), y.end()); for (int i = 0; i < (int)(n + 1 >> 1); ++i) s -= y[i]; return s >= 0; } void solve() { long long s; scanf("%d%lld", &n, &s); for (int i = 0; i < (int)(n); ++i) scanf("%d%d", &x[i].first, &x[i].second); int l = 1, r = inf; while (r > l) { int m = l + r + 1 >> 1; if (ok(m, s)) l = m; else r = m - 1; } printf("%d\n", l); } int main() { int t; scanf("%d", &t); while (t--) solve(); } ```
#include <bits/stdc++.h> using namespace std; vector<pair<long long, long long>> f; long long n, s; bool can(long long mid) { long long sum = 0, cnt = 0; vector<long long> v; for (long long i = 0; i < n; i++) { if (f[i].second < mid) sum += f[i].first; else if (f[i].first >= mid) { sum += f[i].first; cnt++; } else { v.push_back(f[i].first); } } long long rem = max(0LL, (n + 1) / 2 - cnt); if (v.size() < rem) return false; sort(v.rbegin(), v.rend()); for (long long i = 0; i < v.size(); ++i) { if (rem > 0) { sum += mid; rem--; } else { sum += v[i]; } } return sum <= s; } signed main() { long long t; cin >> t; while (t--) { cin >> n >> s; f.resize(n); for (long long i = 0; i < n; i++) { cin >> f[i].first >> f[i].second; } long long left = 0, right = 1e9 + 7; while (right - left > 1) { long long mid = right + left >> 1; if (can(mid)) left = mid; else right = mid; } cout << left << '\n'; } }
### Prompt Your task is to create a CPP solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<pair<long long, long long>> f; long long n, s; bool can(long long mid) { long long sum = 0, cnt = 0; vector<long long> v; for (long long i = 0; i < n; i++) { if (f[i].second < mid) sum += f[i].first; else if (f[i].first >= mid) { sum += f[i].first; cnt++; } else { v.push_back(f[i].first); } } long long rem = max(0LL, (n + 1) / 2 - cnt); if (v.size() < rem) return false; sort(v.rbegin(), v.rend()); for (long long i = 0; i < v.size(); ++i) { if (rem > 0) { sum += mid; rem--; } else { sum += v[i]; } } return sum <= s; } signed main() { long long t; cin >> t; while (t--) { cin >> n >> s; f.resize(n); for (long long i = 0; i < n; i++) { cin >> f[i].first >> f[i].second; } long long left = 0, right = 1e9 + 7; while (right - left > 1) { long long mid = right + left >> 1; if (can(mid)) left = mid; else right = mid; } cout << left << '\n'; } } ```
#include <bits/stdc++.h> using namespace std; struct range { int l, r, id; } rng[200010]; int vis[200010]; int n; long long s; bool find(int m) { for (int i = 1; i <= n; i++) vis[i] = 0; int lcnt(0), rcnt(0); long long sum(0); for (int i = 1; i <= n; i++) { if (rng[i].r < m) { lcnt++; sum += rng[i].l; vis[i] = 1; } else if (rng[i].l > m) { rcnt++; sum += rng[i].l; vis[i] = 1; } if (sum > s || lcnt > n / 2 || rcnt > n / 2) return false; } for (int i = 1; i <= n; i++) { if (vis[i]) continue; if (lcnt < n / 2) { if (rng[i].l > m) { return false; } sum += rng[i].l; lcnt++; vis[i] = 1; } else if (rcnt <= n / 2) { sum += m; vis[i] = 1; rcnt++; } if (sum > s) return false; } return true; } int main() { int t; scanf("%d", &t); while (t--) { scanf("%d %I64d", &n, &s); for (int i = 1; i <= n; i++) { scanf("%d %d", &rng[i].l, &rng[i].r); rng[i].id = i; } sort(rng + 1, rng + 1 + n, [](const range& lhs, const range& rhs) { return lhs.r < rhs.r; }); int r = rng[n / 2 + 1].r + 1; sort(rng + 1, rng + 1 + n, [](const range& lhs, const range& rhs) { return lhs.l < rhs.l; }); int l = rng[n / 2 + 1].l; while (r - l > 1) { int m = (l + r) >> 1; if (find(m)) { l = m; } else { r = m; } } printf("%d\n", l); } }
### Prompt Please provide a Cpp coded solution to the problem described below: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct range { int l, r, id; } rng[200010]; int vis[200010]; int n; long long s; bool find(int m) { for (int i = 1; i <= n; i++) vis[i] = 0; int lcnt(0), rcnt(0); long long sum(0); for (int i = 1; i <= n; i++) { if (rng[i].r < m) { lcnt++; sum += rng[i].l; vis[i] = 1; } else if (rng[i].l > m) { rcnt++; sum += rng[i].l; vis[i] = 1; } if (sum > s || lcnt > n / 2 || rcnt > n / 2) return false; } for (int i = 1; i <= n; i++) { if (vis[i]) continue; if (lcnt < n / 2) { if (rng[i].l > m) { return false; } sum += rng[i].l; lcnt++; vis[i] = 1; } else if (rcnt <= n / 2) { sum += m; vis[i] = 1; rcnt++; } if (sum > s) return false; } return true; } int main() { int t; scanf("%d", &t); while (t--) { scanf("%d %I64d", &n, &s); for (int i = 1; i <= n; i++) { scanf("%d %d", &rng[i].l, &rng[i].r); rng[i].id = i; } sort(rng + 1, rng + 1 + n, [](const range& lhs, const range& rhs) { return lhs.r < rhs.r; }); int r = rng[n / 2 + 1].r + 1; sort(rng + 1, rng + 1 + n, [](const range& lhs, const range& rhs) { return lhs.l < rhs.l; }); int l = rng[n / 2 + 1].l; while (r - l > 1) { int m = (l + r) >> 1; if (find(m)) { l = m; } else { r = m; } } printf("%d\n", l); } } ```