hdu 5101 Select【二分查找】

Select

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1812    Accepted Submission(s): 507

Problem Description

One day, Dudu, the most clever boy, heard of ACM/ICPC, which is a very interesting game. He wants to take part in the game. But as we all know, you can't get good result without teammates.
So, he needs to select two classmates as his teammates. 
In this game, the IQ is very important, if you have low IQ you will WanTuo. Dudu's IQ is a given number k. We use an integer v[i] to represent the IQ of the ith classmate. 
The sum of new two teammates' IQ must more than Dudu's IQ.
For some reason, Dudu don't want the two teammates comes from the same class.
Now, give you the status of classes, can you tell Dudu how many ways there are.

Input

There is a number T shows there are T test cases below. (T≤20)
For each test case , the first line contains two integers, n and k, which means the number of class and the IQ of Dudu. n ( 0≤n≤1000 ), k( 0≤k<231 ).
Then, there are n classes below, for each class, the first line contains an integer m, which means the number of the classmates in this class, and for next m lines, each line contains an integer v[i], which means there is a person whose iq is v[i] in this class. m( 0≤m≤100 ), v[i]( 0≤v[i]<231 )

Output

For each test case, output a single integer.

Sample Input

1

3 1

1 2

1 2

2 1 1

Sample Output

5

Source

BestCoder Round #17

 

题目大意: 

给你n组数字 ,让你从两组数字中各找一个数字,组成一个大于K的二元组,问一共有多少个。

分析样例:

可行方案:(2,2)(2,1)(2,1)(2,1)(2,1);


思路:


1、首先把全部数字排成一排,然后排序,二分查找。求得此时情况的可行解个数tmp,时间复杂度O(n*m*Log(n*m));


2、然后将每组中的数字排序,同样做二分查找,求得各个组的可行解个数tmpp,时间复杂度O(n*m*Logm);


3、排序之后二分查找:枚举每一个当前数字a【i】,然后二分查找距离其最远的那个数字,使得a【i】+a【mid】>k;记录答案mid-i;


Ac代码:


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long int
ll b[1000*1000];
ll a[1500];
int main()
{
    ll t;
    scanf("%I64d",&t);
    while(t--)
    {
        ll sum=0;
        ll cont=0;
        ll n,k;
        scanf("%I64d%I64d",&n,&k);
        for(ll i=0;i<n;i++)
        {
            ll m;
            scanf("%I64d",&m);
            for(ll j=0;j<m;j++)
            {
                scanf("%I64d",&a[j]);
                b[cont++]=a[j];
            }
            sort(a,a+m);
            reverse(a,a+m);
            for(ll j=0;j<m;j++)
            {
                ll ans=-1;
                ll mid=0;
                ll l=j;
                ll r=m-1;
                while(r-l>=0)
                {
                    mid=(l+r)/2;
                    if(a[mid]+a[j]>k)
                    {
                        l=mid+1;
                        ans=mid;
                    }
                    else r=mid-1;
                }
                if(ans==-1)continue;
                sum-=ans-j;
            }
        }
        sort(b,b+cont);
        reverse(b,b+cont);
        for(ll j=0;j<cont;j++)
        {
            ll tmpp=-1;
            ll mid;
            ll l=j;
            ll r=cont-1;
            while(r-l>=0)
            {
                mid=(l+r)/2;
                if(b[mid]+b[j]>k)
                {
                    l=mid+1;
                    tmpp=mid;
                }
                else
                {
                    r=mid-1;
                }
            }
            if(tmpp==-1)continue;
            sum+=tmpp-j;
        }
        printf("%I64d\n",sum);
    }
}
/*
2 1
5 1 1 1 1 1
2 1 1
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值