hdu 5101 Select(思路,容器,二分)

6 篇文章 0 订阅

Select

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


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
  
  
1 3 1 1 2 1 2 2 1 1
 

Sample Output
  
  
5

题意:有n个班级,每个班级有m个人,求从两个不同班级里找出两个人和大于嘟嘟的IQ k的个数

思路:

ans=所有人里找出两个人智商之和大于k-同一个班级里两个人智商之和大于k

我们可以对每一个班级的人的智商进行排序,还有单独保存所有人的智商,然后对所有人的智商进行排序

那么我们可以枚举每一个人,然后在所有人里面二分去找出另一个人,两人智商之和满足条件的个数-

在当前班级里二分去找出另一个人,两人智商之和满足条件的个数

这里可以用vector来保存这个二维表,也方便提取首尾地址,用lower_bound去找出序列中大于等于V的第一个数的地址

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
#define N 1100
int main()
{
    int T;
    int n,k,t,l;
    scanf("%d",&T);
    while(T--)
    {
        vector<int>q[N];
        scanf("%d %d",&n,&k);
        for(int i=1;i<=n;i++)
            {
                scanf("%d",&t);
                while(t--)
                {
                    scanf("%d",&l);
                    q[0].push_back(l);
                    q[i].push_back(l);
                }
                sort(q[i].begin(),q[i].end());
            }
            sort(q[0].begin(),q[0].end());
            int sum,sum1;
            long long ans=0;
            for(int i=1;i<=n;i++)
            {
                for(int j=0;j<q[i].size();j++)
                {
                    int v=q[i][j];
                    sum=q[0].end()-lower_bound(q[0].begin(),q[0].end(),k-v+1);
                    sum1=q[i].end()-lower_bound(q[i].begin(),q[i].end(),k-v+1);
                    ans+=sum-sum1;
                }
            }
        printf("%lld\n",ans/2);
    }
    return 0;
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值