HDU - 5101 Select (二分)

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≤20T≤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≤10000 ), k( 0≤k≤2^{31} ). 
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≤1000≤m≤100 ), v[i]( 0≤v[i]<2^{31} )

 

Output

For each test case, output a single integer.

 

Sample Input

1
3 1
1 2
1 2
2 1 1

 

Sample Output

5

题目大意:从n个班的学生中选取两个学生,这两个学生需满足:不在同一个班,两个人的IQ和大于K,输出总共的方案数

解题思路: 想明白了这道题就好做了,首先我们可以求出每一个班的学生中满足条件的方案数,然后把所有的学生放在一起再求满足条件的方案数,后者减去前者即为答案,再求有多少种方案时,先sort排下序,从第一个学生开始,找到满足条件的第二个学生的位置l(这个学生是符合条件时IQ的最小值,因为排过序,后面的学生都满足,与第一个学生配备的方案数就是m-l)

 

刚开始使用cin输入TL了,然后用std::ios::sync_with_stdio(false)来取消cin与stdin的同步,加快cin的输入效率依然TL,最后用scna输入才过,看来即使取消cin与stdin的同步cin的输入所需时间还是比scanf用的多。

 

AC代码:

#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=1100;
ll a[maxn],b[maxn*maxn];
int main() 
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		ll n,m;
		ll cnt=0;//总人数 
		ll k,sum=0;
		scanf("%lld%lld",&n,&k);
		for(ll i=0;i<n;i++)
		{
			scanf("%lld",&m);
			for(ll j=0;j<m;j++)
			{
				scanf("%lld",&a[j]);
				b[cnt++]=a[j];
			}
			sort(a,a+m);
			for(ll j=0;j<m;j++)//同一班中从第一个学生开始一次找符合条件的方案数 
			{
				ll l=j+1,r=m-1;
				while(l<=r)
				{
					ll mid=(l+r)/2;
					if(a[j]+a[mid]>k)
					{
						r=mid-1;	
					}
					else
						l=mid+1;
				}
				sum-=m-l;
			}	
		}
		sort(b,b+cnt);
		for(ll j=0;j<cnt;j++)//所有学生中满足条件的方案数 
		{
			ll l=j+1,r=cnt-1;
			while(l<=r)
			{
				ll mid=(l+r)/2;
				if(b[j]+b[mid]>k)
				{
					r=mid-1;
				}
				else
					l=mid+1;
			}
			sum+=cnt-l;
		}
		printf("%lld\n",sum);
	}
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值