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
), vi
Output
For each test case, output a single integer.
Sample Input
1
3 1
1 2
1 2
2 1 1
Sample Output
5
Hint
题意
t组数据
n个班 每个班m个人
从不同的两个班里找两人要求他们的iq和大于k
求满足上述条件的种数
题解:
暴力肯定行不通
先不考虑班级不同的条件 发现可以用二分简单求到
在考虑只是一个班的条件 同样可以用二分实现
利用从小到大排序后下标,成绩与满足条件的人数 采用二分法
代码
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 1e5+10;
int a[1500][150];
int num[N];
int st[1500];
int main()
{
int t;
scanf("%d",&t);
while (t--){
int n;
int k;
scanf("%d%d",&n,&k);
int iq;
int cnt = 0;
for (int i = 0;i < n; ++i){
scanf("%d",&st[i]);
for (int j = 0; j < st[i]; ++j){
scanf("%d",&a[i][j]);
num[cnt++] = a[i][j];
}
sort(a[i],a[i]+st[i]);
}
sort(num,num+cnt);
LL ans = 0;
/*二分查找大于k-num[i]的所有同学的个数 相当于和num[i]组队*/
for (int i = 0; i < cnt; ++i){
ans += num+cnt-upper_bound(num+i+1,num+cnt,k-num[i]);
}
/*二分查找每个班内部同学组队的个数 这个是需要被减去的*/
for (int i = 0; i< n; ++i){
for (int j= 0; j < st[i]; ++j){
ans -= a[i]+st[i]-upper_bound(a[i]+j+1,a[i]+st[i],k-a[i][j]);
}
}
printf("%lld\n",ans);
}
return 0;
}