Select
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 701 Accepted Submission(s): 204
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.
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 )
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
题意:
先给你两个数n、k。n表示有n个班,k表示 TJ 的IQ值。现在 TJ 要选两个人,这两个人的IQ和要比 TJ 大,并且不能是一个班的。接下来给你n个班的情况,每行第一个数m代表这个班有m人,接着m个数指班上每个人的IQ。问你有多少种符合条件的方法。
分析:
只要求所有人IQ值加和比k大的方法数再减去每个班的人IQ值加和比k大的方法数的总和,利用二分即可。复杂度O(n*m*log(n*m));
lower_bound(v.begin(),v.end(),x) : v数组内的数一定要是排过序的。在v数组中从v.begin()开始到v.end()找到第一个不小于x的数,并返回其所在位置的迭代器。
吐槽:自己写的二分怎么都过不了,于是就学了下lower_bound()这个函数==。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
__int64 v[100010], u[110];
int main()
{
int T, n, k, m;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n,&k);
__int64 sum = 0;
int cnt = 0;
for(int i=0; i<n; i++)
{
scanf("%d", &m);
for(int j=0; j<m; j++)
{
scanf("%d", u+j);
v[cnt++] = u[j];
}
sort(u,u+m);
for(int j=0; j<m-1; j++)
sum -= m-(lower_bound(u+j+1,u+m,k-u[j]+1)-u);
}
sort(v,v+cnt);
for(int i=0; i<cnt-1; i++)
sum += cnt-(lower_bound(v+i+1,v+cnt,k-v[i]+1)-v);
printf("%I64d\n", sum);
}
return 0;
}