Select
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1422 Accepted Submission(s): 395
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
题目大意:给出n个集合,每个集合有m个数,不同的集合,m的值不一定相同,问从不同的集合中取两个数,使得这两个数的和大于k的取法有多少种。
解题思路:
对于一组有m个数,如果要取两个数a和b,使得这两个数的和大于k,那么可以将这组数由小到大排序,然后枚举a,查找满足条件的数b的个数,在查找数b的个数时,可以利用lower_bound函数(二分查找)。比如:我们找到第一个满足a+b>k的b是第i个数,则第i+1,i+2,……个数都满足。枚举a的过程记数并加和,得到的结果就是满足条件的(a,b)对的2倍。
题目要求不同的集合,可以利用总集合中满足条件的数对 - 相同集合满足条件的集合数对。
(注意:单个集合的数最多有100,集合数目最多有1000,则总集合的数最多有100000)
代码如下:
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #include <iostream> #include <algorithm> #include <string> #include <vector> #include <deque> #include <list> #include <set> #include <map> #include <stack> #include <queue> #include <numeric> #include <iomanip> #include <bitset> #include <sstream> #include <fstream> #include <limits.h> #define debug "output for debug\n" #define pi (acos(-1.0)) #define eps (1e-6) #define inf (1<<28) #define sqr(x) (x) * (x) #define mod 1000000007 using namespace std; typedef long long ll; typedef unsigned long long ULL; struct IQ { ll m; ll v[100005]; }a[1005]; int main() { ll i,j,l,m,n,v,k,t; scanf("%I64d",&t); while(t--) { scanf("%I64d%I64d",&n,&k); a[0].m=0; for(i=1,l=0;i<=n;i++) { scanf("%I64d",&m); a[i].m=m; a[0].m+=m; for(j=0;j<m;j++) { scanf("%I64d",&v); a[i].v[j]=v; a[0].v[l++]=v; } sort(a[i].v,a[i].v+m); } sort(a[0].v,a[0].v+a[0].m); ll ans=0; for(i=1;i<=n;i++) { for(j=0;j<a[i].m;j++) { v=a[i].v[j]; ll x=lower_bound(a[0].v,a[0].v+a[0].m,k-v+1)-a[0].v; ll n1=a[0].m-x; //printf("i=%I64d j=%I64d x=%I64d n1=%I64d ",i,j,x,n1); ll y=lower_bound(a[i].v,a[i].v+a[i].m,k-v+1)-a[i].v; ll n2=a[i].m-y; //printf("i=%I64d j=%I64d y=%I64d n2=%I64d\n",i,j,y,n2); ans+=n1-n2; } } printf("%I64d\n",ans/2); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。