hdu 5101 Select(Bestcoder Round #17)

Select

                                                   Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 285    Accepted Submission(s): 85

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

    一个人要选两名队员,两名队员的IQ之和要大于他的,另两名队员不能在一个班级。

   这题的二分思路还是非常easy想出来的,总人数1000*100,时限为2s,复杂度肯定为nlog(n),又由于是找比一个数大的。非常符合二分的性质。

     官方题解:

  答案=从全部数中选择的两个加和大于k的数的方案数-在同一个集合中选择的两个加和大于k的数的方案数
而对于同一个集合中选择的两个加和大于k的方案数是能够直接排序然后利用单调性高速统计出来的。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn=100000+100;
int a[maxn];
int map[1010][110];
int v[1010];
int cur;
int finda(int x)//在大的区间二分查找
{
    int l=0;
    int r=cur-1;
    if(x>=a[r])
    return cur;
    while(l<r)
    {
        int m=(l+r)>>1;
        if(a[m]<=x)
        l=m+1;
        else
        r=m;
    }
    return l;
}
int find(int p,int x)//在小区间二分查找
{
    if(x>=map[p][v[p]-1])
    return v[p];
    int l=0;
    int r=v[p]-1;
    while(l<r)
    {
        int m=(l+r)>>1;
        if(map[p][m]>x)
        r=m;
        else
        l=m+1;
    }
    return l;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,k,m;
        scanf("%d%d",&n,&k);
        cur=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&v[i]);
            m=v[i];
            for(int j=0;j<m;j++)
            {
                scanf("%d",&map[i][j]);
                a[cur++]=map[i][j];
            }
            sort(map[i],map[i]+m);
        }
        sort(a,a+cur);
        long long ans=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<v[i];j++)
            {
                int to=k-map[i][j];
                int temp=cur-finda(to);
                int temp2=v[i]-find(i,to);
                ans+=(temp-temp2);
            }
        }
        printf("%I64d\n",ans/2);
    }
    return 0;
}

时间: 2024-10-12 20:19:25

hdu 5101 Select(Bestcoder Round #17)的相关文章

HDU 5101 Select(不同集合的两个数大于k的方案数)

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

HDU 5904 - LCIS (BestCoder Round #87)

HDU 5904 - LCIS [ DP ]    BestCoder Round #87 题意: 给定两个序列,求它们的最长公共递增子序列的长度, 并且这个子序列的值是连续的 分析: 状态转移方程式: dp[a[i]] = max(dp[a[i]], dp[a[i]-1] + 1); 发现其实可以简化为 dp[a[i]] = dp[a[i]-1] + 1:因为计算过程中dp[a[i]]不会降低 对两个序列都求一遍,然后取两者最小值的最大值 1 #include <cstdio> 2 #inc

hdu 5748 Bellovin(BestCoder Round #84——最长递增子序列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5748 Bellovin Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 929    Accepted Submission(s): 421 Problem Description Peter has a sequence a1,a2,

HDU 5666 Segment——BestCoder Round #80

Segment Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description Silen August does not like to talk with others.She like to find some interesting probl

HDU 5101 Select(BestCoder Round #17)

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

hdu 5101 Select

http://acm.hdu.edu.cn/showproblem.php?pid=5101 在比赛的时候没有想出怎么做,自己真水. 题意:给定一些集合,选择两个来自不同集合的数,加和大于k,问有多少种选择方案. 思路:答案=从所有数中选择的两个加和大于k的数的方案数-在同一个集合中选择的两个加和大于k的数的方案数而对于同一个集合中选择的两个加和大于k的方案数是可以直接排序然后利用单调性快速统计出来的.利用vector把所有的数存到q[0]里面.对q[0]排序,也对q[i]排序. 然后在q[0]

hdu 4859 海岸线 Bestcoder Round 1

http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格线满足两侧分别是海洋和陆地 这道题很神 首先考虑一下,什么情况下能够对答案做出贡献 就是相邻的两块不一样的时候 这样我们可以建立最小割模型,可是都说是最小割了 无法求出最大的不相同的东西 所以我们考虑转化,用总的配对数目 - 最小的相同的对数 至于最小的相同的对数怎么算呢? 我们考虑这样的构造方法:

hdu 5101 Select(树状数组)

题目链接:hdu5101 Select 题目大意:N和K,给定若干组数,要从从不同组中选出连个数和大于K,问说有多少种组成方案. 解题思路:树状数组维护,将所有的数离散化掉对应成树状数组的下标,每次先计算一组,然后再将该组的元素插入到 树状数组中. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int m

hdu 5083 Instruction(Bestcoder Round #15)

Instruction                                                               Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 327    Accepted Submission(s): 94 Problem Description Nowadays, Jim Gre