hdu 5101

Select

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

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

BestCoder Round #17

思路:对于每一行,先询问,后插入树状数组。

手残写错一个变量竟然过了初判,也是醉了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<map>
#include<ctime>
#include<bitset>
#define LL long long
#define ll long long
#define INF 0x3f3f3f3f
#define maxn 100010
#define eps 1e-6
using namespace std;

int cnt[1010] ,len ;
LL b[maxn] ;
int mat[1010][110],xt[maxn] ;

void add(int x)
{
    while(x< maxn)
    {
        xt[x]++;
        x += (x&-x) ;
    }
}
int sum1(int x)
{
    int ans=0;
    while(x>0)
    {
        ans += xt[x] ;
        x -= (x&-x) ;
    }
    return ans;
}
int main()
{
    int n ,i ,j ,k ,m ;
    int T,sum;
    LL ans;
    cin >> T ;
    while(T--)
    {
        scanf("%d%d",&n,&sum) ;
        len=0;
        for( i = 1 ; i <= n ;i++)
        {
            scanf("%d",&cnt[i]) ;
            for( j = 1 ; j <= cnt[i];j++)
            {
                scanf("%d",&mat[i][j]) ;
                b[len++] =mat[i][j] ;
            }
        }
        b[len++]=7897878787888LL;
        sort(b,b+len) ;
        len=unique(b,b+len)-b;
        memset(xt,0,sizeof(xt)) ;
        int hehe=0;
        for( i = 1 ; i <= cnt[1];i++)
        {
            k = lower_bound(b,b+len,mat[1][i])-b;
            k++;
            hehe++;
            add(k) ;
        }
        ans=0;
        for( i = 2 ; i <= n;i++)
        {
            for( j = 1 ; j <= cnt[i];j++)
            {
                m = sum-mat[i][j] ;
                k = lower_bound(b,b+len,m)-b;
                if(k==len-1) continue;
                if(m!=b[k])k--;
                k++;
                //cout<<k<<" "<<sum1(k)<<endl;
                if(k>0)
                  ans += hehe-sum1(k) ;
                else ans += hehe;
            }
            for( j = 1 ; j <= cnt[i];j++)
            {
                k = lower_bound(b,b+len,mat[i][j])-b;
                k++;
                hehe++;
                add(k) ;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}
/*
1
3 3
1 2
1 2
2 1 1
*/

时间: 2024-10-18 20:52:25

hdu 5101的相关文章

BestCoder17 1002.Select(hdu 5101) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5101 题目意思:给出 n 个 classes 和 Dudu 的 IQ(为k),每个classes 都有相应的人,每个人又有相应的IQ.现在要求从这些classes挑选两个人,满足IQ之和 > k,不过要满足这两个人不能来自同一个class的. 解题思路不难想出,直接所有人两两之和 > k - 同一个class 两两之和 > k  就是答案了. 不过很容易超时!!!! 用二分就可以过了.二分有

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 5101 Select(树状数组)

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

HDU 5101 Select --离散化+树状数组

题意:n 组,每组有一些值,求 在不同的两组中每组选一个使值的和大于k的方法数. 解法:n * Cnt[n] <= 1000*100 = 100000, 即最多10^5个人,所以枚举每个值x,求他的后面那些组中有多少数大于 k-x即可, 求有多少数大于k-x可以先求有多少数小于等于k-x,然后总数减一下即可. 可以用树状数组求. 先把所有数离散地存入一个树状数组中,然后每次枚举完一组的数后,将这组的数去掉. 代码: #include <iostream> #include <cst

hdu 5101 Select (二分+单调)

题意: 多多有一个智商值K. 有n个班级,第i个班级有mi个人.智商分别是v1,v2,.....vm. 多多要从这些人中选出两人.要求两人智商和大于K,并且两人不同班.问总共有多少种方案. 数据范围: n ( 0≤n≤1000 ), k( 0≤k<2^31 ) m( 0≤m≤100 ), v[i]( 0≤v[i]<2^31 ) 思路: 对于一个单调长的序列,可以很容易地统计两数之和大于K的对数(枚举第一个数,二分找到满足条件的最小的数的位置,然后它和它后面的数都是满足条件的) 故对于每个班内,

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 bo

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(不同集合的两个数大于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 5101(思路题)

Select Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1599    Accepted Submission(s): 443 Problem Description One day, Dudu, the most clever boy, heard of ACM/ICPC, which is a very interesting