C. Keshi Is Throwing a Party- Codeforces Global Round 17

12 篇文章 1 订阅
2 篇文章 0 订阅

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<n)(0≤ai,bi<n).

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],那么就可以选该物品。

#include <iostream>
# include<algorithm>
# include<cstring>

using namespace std;

typedef long long int ll;

ll a[200000+10],b[200000+10];
int n;
bool check(ll mid)
{
    int now=0;
    for(int i=1;i<=n;i++)
    {
        if(mid-now-1<=a[i]&&now<=b[i])
            now++;

        if(now>=mid)
            return 1;
    }

    return  0;
}
int main()
{


    int t;

    cin>>t;

    while(t--)
    {


        cin>>n;

        for(int i=1;i<=n;i++)
        {
            cin>>a[i]>>b[i];
        }

        int left=0,right=n;

        while(left<=right)
        {
            int mid=(left+right)>>1;

            if(check(mid))
                left=mid+1;
            else
                right=mid-1;
        }

        cout<<right<<endl;
    }

    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
JAVA课程设计报告-完整版全文共8页,当前为第1页。JAVA课程设计报告-完整版全文共8页,当前为第1页。JAVA课程设计报告_完整版 JAVA课程设计报告-完整版全文共8页,当前为第1页。 JAVA课程设计报告-完整版全文共8页,当前为第1页。   if(((j).getXueHao ).equals(xuehao111)   "" (j).getXingMing .equals(xingming111))  {     cha1=j;  }  }    if(cha1 == null) //无此学生信息则录入信息  {   (new   Student(xingming111,(xuehao111),riqi111,   (keshi111),kemu111,leixing111));  (\录入信息成功!\  (null);  (null);  (null);  (null);  (null);  (null);  }  else  {   (\学生已有信息存在,有需要请修改!\  (null);  (null);  (null);  (null);  (null);  (null);  }  }     if( ==chaxun2) //修改功能监听 {    if( == 0)   (\暂无任何学生信息,请先录入!\  else  {   for(int j = 0;j student;   JLabel xingming1,xuehao1,riqi1,keshi1,kemu1,leixing1;//录入功能组件定义  JTextField xingming11,xuehao11,riqi11,keshi11,kemu11,leixing11;  JAVA课程设计报告-完整版全文共8页,当前为第2页。JAVA课程设计报告-完整版全文共8页,当前为第2页。String xingming111,xuehao111,riqi111,keshi111,kemu111,leixing111; JPanel Input,panel11,panel12,panel13,panel14,panel15,panel16;   JLabel xingming2,xuehao2,riqi2,keshi2,kemu2,leixing2;//修改功能组件的定义  JTextField xingming22,xuehao22,riqi22,keshi22,kemu22,leixing22;  String xingming222,xuehao222,riqi222,keshi222,kemu222,leixing222;   JPanel Modify,panel21,panel22,panel23,panel24,panel25,panel26,panel27;   JLabel xingming3,xuehao3,riqi3,keshi3,kemu3,leixing3;//查找功能组件的定义  JTextField xingming33,xuehao33,riqi33,keshi33,kemu33,leixing33;  String xingming333,xuehao333,riqi333,keshi333,kemu333,leixing333; JPanel JAVA课程设计报告-完整版全文共8页,当前为第3页。JAVA课程设计报告-完整版全文共8页,当前为第3页。Find,panel31,panel32,panel33,panel34,panel35,panel36;     JButton tijiao1,xiugai2,chaxun2,chaxun3;//各个按钮的定义  JTabbedPane tb;   Integer cha1 = null,cha2 = null; public kaoqin {   background1 = new JLabel(\请录入学生信息!\//提示信息的初始化   36   background2 = new JLabel(\请使用学号和姓名查询!\ background3 = new JLabel(\请使用学号和姓名查询!\   student = new ArrayList ;   xingming1=new JLabel(\学生姓名:\//录入界面各个组件的安排  xuehao1=new JLabel(\学生学号:\ riqi1=new JLabel(\缺勤日期:\ keshi1=new JLabel(\缺勤课时:\ kemu1=new JLabel(\缺勤科目:\ leixing1=new JLabel(\缺勤类型:\     xingming11=new JTextField(

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秦三码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值