Codeforces Global Round 17 C. Keshi Is Throwing a Party 二分查找

server/2024/10/8 18:21:04/

Keshi Is Throwing a Party

题目描述

Keshi is throwing a party and he wants everybody in the party to be happy.

He has n n n friends. His i i i-th friend has i i i dollars.

If you invite the i i i-th friend to the party, he will be happy only if at most a i a_i ai people in the party are strictly richer than him and at most b i b_i bi people are strictly poorer than him.

Keshi wants to invite as many people as possible. Find the maximum number of people he can invite to the party so that every invited person would be happy.

输入描述

The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 ) (1\le t\le 10^4) (1t104) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 ) (1\le n\le 2 \cdot 10^5) (1n2105) — the number of Keshi’s friends.

The i i i-th of the following n n n lines contains two integers a i a_i ai and b i b_i bi ( 0 ≤ a i , b i < n ) (0 \le a_i, b_i < n) (0ai,bi<n).

It is guaranteed that the sum of n n n over all test cases doesn’t exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

输出描述

For each test case print the maximum number of people Keshi can invite.

样例输入 #1

3
3
1 2
2 1
1 1
2
0 0
0 1
2
1 0
0 1

样例输出 #1

2
1
2

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;void solve()
{int n;cin >> n;vector<int> a(n), b(n);for (int i = 0; i < n; i++)cin >> a[i] >> b[i];auto check = [&](int x) -> bool{int cnt = 0;for (int i = 0; i < n; i++){if (cnt <= b[i] && x - cnt - 1 <= a[i]) // 贪心策略,每找到一个满足条件的朋友,邀请他cnt++;if (cnt >= x) // 邀请人数已足够,返回1return 1;}return 0;};// 二分查找,确定上下界为1和nint l = 1, r = n;while (l < r){int mid = (l + r + 1) / 2;if (check(mid))l = mid;elser = mid - 1;}cout << l << '\n';return;
}int main()
{ios::sync_with_stdio(0);cin.tie(0);int t;cin >> t;while (t--)solve();return 0;
}

http://www.ppmy.cn/server/44677.html

相关文章

Redis常见基本类型(5)-List, Set

List 命令小结 命令及解释时间复杂度lpush/rpush key value[key value...](向右/左端插入元素)O(k), k是元素个数linsert key before | after pivot value(在某个坐标之前/右插入元素)O(n), n是pivot距离头尾的距离lrange start end(获取从start到end部分的元素)O(s n): s是…

手写电纸书天花板,阅读办公新体验 | 汉王手写电纸本 N10 2024 版使用评测

手写电纸书天花板&#xff0c;阅读办公新体验 | 汉王手写电纸本 N10 2024 版使用评测 请问如果说到电纸书&#xff0c;你的认知还只是Kindle吗&#xff1f;然而遗憾的是&#xff0c;Kindle亦是过去&#xff0c;智能才是未来。 哈喽小伙伴们好&#xff0c;我是Stark-C~&#x…

赶紧收藏!2024 年最常见 20道 Redis面试题(七)

上一篇地址&#xff1a;赶紧收藏&#xff01;2024 年最常见 20道 Redis面试题&#xff08;六&#xff09;-CSDN博客 十三、Redis如何做内存优化&#xff1f; Redis是一个内存中的数据存储系统&#xff0c;因此内存优化对于提高性能和降低成本至关重要。以下是一些Redis内存优…

研二学妹面试字节,竟倒在了ThreadLocal上,这是不要应届生还是不要女生啊?

一、写在开头 今天和一个之前研二的学妹聊天&#xff0c;聊及她上周面试字节的情况&#xff0c;着实感受到了Java后端现在找工作的压力啊&#xff0c;记得在18&#xff0c;19年的时候&#xff0c;研究生计算机专业的学生&#xff0c;背背八股文找个Java开发工作毫无问题&#x…

【经验分享】可视化的项目管理,轻松解决资源冲突和协作困难

在数字化时代&#xff0c;高效协同逐步成为提升组织效能的重要着力点&#xff0c;同时也是企业保持竞争力、实现持续发展的关键要素。一方面可以打破部门壁垒&#xff0c;促进信息流通&#xff0c;从而提升整体工作效率&#xff1b;另一方面还能帮助企业优化资源配置和管理流程…

【CALayer-CALayer的基本属性 Objective-C语言】

一、接下来,我们来说这个Layer啊, 1.首先,Layer能接触到的,就是我们之前说截图啊,就是我们self.view里面,有一个layer属性, [self.view.layer renderInContext:(CGContextRef t)]; 那个里面,有一个layer属性,然后呢,是CALayer类型的, 接下来,我们就来学习一…

5月28日,每日信息差

第一、微信推出青少年模式&#xff0c;包含时长限制、宵禁能力、监护人授权功能、消费限额与访问限制、青少年内容分级等 4 项特色功能&#xff0c;旨在更好地保护青少年用户的使用安全与健康&#xff0c;助力青少年网络环境的优化 第二、百度贴吧存在公开买卖孩子的行为&…

基于朴素贝叶斯算法的微博舆情监控系统,flask后端,可视化丰富

背景&#xff1a; 微博作为中国最大的社交媒体平台之一&#xff0c;汇聚了海量用户生成的文本数据&#xff0c;承载着丰富的社会信息和舆论动向。随着互联网的快速发展&#xff0c;人们对于利用这些数据进行舆情分析和预测的需求日益增加。在这种情况下&#xff0c;以Python为…