HDOJ 5101 Select(upper_bound的运用)

来源:互联网 发布:hebe掰弯selina知乎 编辑:程序博客网 时间:2024/06/05 04:08


Select

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1887    Accepted Submission(s): 539


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. (T20)
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 (
0n1000 ), k( 0k<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(
0m100 ), v[i]( 0v[i]<231 )

 


Output

For each test case, output a single integer.

 


Sample Input

13 11 21 22 1 1

 


Sample Output

5


先贴出lower_bound和upper_bound的用法

(1)lower_bound (a,a+n,t)   返回非递减序列a[0]~a[n-1]第一个大于等于值t的位置

(2)upper_bound(a,a+n,t)   返回非递减序列a[0]~a[n-1]第一个大于值t的位置

注:没找到则返回最后一个位置

例如a[5]={1,3,7,7,9};
lower_bound(a,a+5,7) - a的值为 2
upper_bound(a,a+5,7) - a的值为 4


#include <bits/stdc++.h>using namespace std;#define mst(a,b) memset((a),(b),sizeof(a))#define f(i,a,b) for(int i=(a);i<(b);++i)const int maxn = 1005;const int mod = 1000000007;const int INF = 1e9;#define ll long long#define rush() int T;scanf("%d",&T);while(T--)int a[maxn][105];int b[maxn*100];int num[maxn];int n,k,m;void init(){    mst(a,0);    mst(b,0);    mst(num,0);}int main(){    rush()    {        init();        scanf("%d%d",&n,&k);        int cnt=0;        f(i,0,n)        {            scanf("%d",&num[i]);            f(j,0,num[i])            {                scanf("%d",&a[i][j]);                b[cnt++]=a[i][j];            }            sort(a[i],a[i]+num[i]);        }        sort(b,b+cnt);        ll ans=0;        f(i,0,n)        {            f(j,0,num[i])            {                int t=k-a[i][j];                int x1=upper_bound(b,b+cnt,t)-b; //所有人里找IQ>t的                ans+=(cnt-x1);                if(t<a[i][num[i]-1])                {                    int x2=upper_bound(a[i],a[i]+num[i],t)-a[i]; //某个班级里IQ>t的                    ans-=(num[i]-x2);                }            }        }        printf("%I64d\n",ans/2);  (x,y),(y,x)如此重复算了两遍    }    return 0;}





0 0
原创粉丝点击