【hdu 5101】Select 【二分 +容斥思想】

来源:互联网 发布:延边大学知乎 编辑:程序博客网 时间:2024/05/29 10:31

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 vii 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≤n≤1000 ), k( 0≤k<2310≤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 vii, which means there is a person whose iq is vii in this class. m( 0≤m≤1000≤m≤100 ), vii( 0≤v[i]<2310≤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

分析,有点容斥的感觉,正向做,时间TLE,那么我们反向求 所有学生两两配对(iq和大于k)的个数,减去每个班中两两配对(iq和大于k)的个数。
代码

#include<bits/stdc++.h>using namespace std;const int MAXN =1000+10;const int MAXM = 1e6 ;int a[MAXN][110];int s[MAXM];int main(){     int  t;cin>>t;     while(t--){        int n,k;cin>>n>>k;int top=0;int ge=0;        for(int i=0;i<n;i++){            int num;scanf("%d",&num);            top+=num;a[i][0]=num;            for(int j=1;j<=num;j++){                scanf("%d",&a[i][j]);                s[ge++]=a[i][j];            }               sort(a[i]+1,a[i]+1+num);        }        sort(s,s+ge);long long  ans=0;        for(int i=0;i<n;i++){            for(int j=1;j<=a[i][0];j++){                 int n1=s+ge-upper_bound(s,s+ge,k-a[i][j]);//所有人中                 int n2=a[i]+1+a[i][0]-upper_bound(a[i]+1,a[i]+1+a[i][0],k-a[i][j]);//每个班的                 ans+=n1-n2;            }        }        printf("%lld\n",ans/2);     }    return 0;}
原创粉丝点击