Select
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1044 Accepted Submission(s): 296
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
Recommend
题意: 一个人选择两名队友,要求两名队友处于不同的班级 并且智商之和高于自己,求选择情况有多少种
题解: 二分查找,枚举其中一名队友,二分另外一名队友,用全集减去相同班级的情况=不同班级的种数
代码:
#include<stdio.h> #include<algorithm> #include<iostream> using namespace std; struct dian { int classs[105]; int count; }cun[1005]; int a[100015]; int erfen(int b[],int l,int r,int key) { int mid,n=r; if(key>=b[n]) return 0; while(l<r) { mid=(l+r)/2; if(b[mid]<=key) l=mid+1; else r=mid; } return n+1-l; } int main() { int T; int n,k; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&k); int num=1; for(int i=1;i<=n;i++) { scanf("%I64d",&cun[i].count); for(int j=1;j<=cun[i].count;j++) { scanf("%I64d",&cun[i].classs[j]); a[num++]=cun[i].classs[j]; } } sort(a+1,a+num); num--; __int64 kk=0; for(int i=1;i<num;i++) kk+=erfen(a,i+1,num,k-a[i]); // printf("%I64d\n",kk); for(int i=1;i<=n;i++) { sort(cun[i].classs+1,cun[i].classs+1+cun[i].count); for(int j=1;j<cun[i].count;j++) { kk-=erfen(cun[i].classs,j+1,cun[i].count,k-cun[i].classs[j]); } } printf("%I64d\n",kk); } }