码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • C. Keshi Is Throwing a Party- Codeforces Global Round 17


    C. Keshi Is Throwing a Party

    time limit per test

    1 second

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

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

    He has nn friends. His ii-th friend has ii dollars.

    If you invite the ii-th friend to the party, he will be happy only if at most aiai people in the party are strictly richer than him and at most bibi 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.

    Input

    The first line contains a single integer tt (1≤t≤104)(1≤t≤104) — the number of test cases. The description of the test cases follows.

    The first line of each test case contains a single integer nn (1≤n≤2⋅105)(1≤n≤2⋅105) — the number of Keshi's friends.

    The ii-th of the following nn lines contains two integers aiai and bibi (0≤ai,bi

    It is guaranteed that the sum of nn over all test cases doesn't exceed 2⋅1052⋅105.

    Output

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

    Example

    input

    Copy

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

    output

    Copy

    2
    1
    2
    

    Note

    In the first test case, he invites the first and the second person. If he invites all of them, the third person won't be happy because there will be more than 11 person poorer than him.

    =========================================================================

    二分和贪心,二分枚举选的个数,判定函数里面运用贪心思想,能选就选。选一个的话,如果剩下的k-cnt-1个少于a[i],并且已经被选的 cnt<=b[i],那么就可以选该物品。

    1. #include
    2. # include
    3. # include
    4. using namespace std;
    5. typedef long long int ll;
    6. ll a[200000+10],b[200000+10];
    7. int n;
    8. bool check(ll mid)
    9. {
    10. int now=0;
    11. for(int i=1;i<=n;i++)
    12. {
    13. if(mid-now-1<=a[i]&&now<=b[i])
    14. now++;
    15. if(now>=mid)
    16. return 1;
    17. }
    18. return 0;
    19. }
    20. int main()
    21. {
    22. int t;
    23. cin>>t;
    24. while(t--)
    25. {
    26. cin>>n;
    27. for(int i=1;i<=n;i++)
    28. {
    29. cin>>a[i]>>b[i];
    30. }
    31. int left=0,right=n;
    32. while(left<=right)
    33. {
    34. int mid=(left+right)>>1;
    35. if(check(mid))
    36. left=mid+1;
    37. else
    38. right=mid-1;
    39. }
    40. cout<
    41. }
    42. return 0;
    43. }

  • 相关阅读:
    系统架构设计师教程 第3章 信息系统基础知识-3.1 信息系统概述
    Vue生命周期
    整合SSM(Mybatis-Spring-SpringMVC层)
    Qt 处理图像数据的类区别(QPixmap、QImage、QPicture)
    stm32f407探索者开发板(一)——资源介绍(顺便说下无人机的进度状况)
    fatal: Unable to create ‘D:/git/2_wechat/.git/index.lock‘: File exists.
    项目从SVN修改成git
    力扣——盛最多水的容器
    傻白入门芯片设计,RDL/Interposer/EMIB/TSV(三)
    淘宝店铺订单解密接口/淘宝店铺订单插旗接口/淘宝店铺订单交易接口/淘宝店铺商品上传接口/淘宝店铺订单明文接口/代码对接分享
  • 原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126128724
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号