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
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

qinsanma and Code

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

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

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

打赏作者

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

抵扣说明:

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

余额充值