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;
}

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

  1. hdu 5636 搜索 BestCoder Round #74 (div.2)

    Shortest Path  Accepts: 40  Submissions: 610  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit: ...

  2. hdu 5101 Select

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5101 Select Description One day, Dudu, the most cleve ...

  3. HDU 5904 - LCIS (BestCoder Round #87)

    HDU 5904 - LCIS [ DP ]    BestCoder Round #87 题意: 给定两个序列,求它们的最长公共递增子序列的长度, 并且这个子序列的值是连续的 分析: 状态转移方程式 ...

  4. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

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

    题意:n 组,每组有一些值,求 在不同的两组中每组选一个使值的和大于k的方法数. 解法:n * Cnt[n] <= 1000*100 = 100000, 即最多10^5个人,所以枚举每个值x,求 ...

  6. HDU 5778 abs (BestCoder Round #85 C)素数筛+暴力

    分析:y是一个无平方因子数的平方,所以可以从sqrt(x)向上向下枚举找到第一个无平方因子比较大小 大家可能觉得这样找过去暴力,但实际上无平方因子的分布式非常密集的,相关题目,可以参考 CDOJ:无平 ...

  7. HDU 5776 sum (BestCoder Round #85 A) 简单前缀判断+水题

    分析:就是判断简单的前缀有没有相同,注意下自身是m的倍数,以及vis[0]=true; #include <cstdio> #include <cstdlib> #includ ...

  8. hdu 5101 Select (二分+单调)

    题意: 多多有一个智商值K. 有n个班级,第i个班级有mi个人.智商分别是v1,v2,.....vm. 多多要从这些人中选出两人.要求两人智商和大于K,并且两人不同班.问总共有多少种方案. 数据范围: ...

  9. hdu 5101(思路题)

    Select Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

随机推荐

  1. C Coding Standard

    1 共同 Rule 1 编译的Warnings不能被忽略掉 Rule 2 在已有Code或者三方的code基础上的改动,同意使用原来的coding standard Rule 3 假设同意C和C++都 ...

  2. hdu-1016素数环

    这个题就是给出一个数字n,表示有n个数.编号为1~n. 然后要求我们将这n个数连起来变成一个环,要求随意两个数相加所得值必须为素数. 假设满足条件就将这个环输出来! 这个题:dfs+回溯+推断.然后注 ...

  3. python 入门学习---模块导入三种方式及中文凝视

    Python 有三种模块导入函数 1. 使用import 导入模块 import modname : 模块是指一个能够交互使用,或者从还有一Python 程序訪问的代码段.仅仅要导入了一个模块,就能够 ...

  4. 实现TextView 文字排版,分散两端对齐

    參考:http://www.cnblogs.com/lcyty/p/3265335.html 方法一:使用HTML TextView textview=(TextView)findViewbyId(R ...

  5. 一些周期性GC的理由为何

    1.供tomcat:防止内存泄漏监听器 JreMemoryLeakPreventionListener在上班,每隔一小时默认触发一次System.gc Class clazz = Class.forN ...

  6. 辛星分析html中间name和id 差额

    朋友们可以新手也能很容易区分id和class,但不一定很容易区分id和name,以下说一下二者的差别和联系. 首先是联系,它们都能够用来标记一个元素,并且能够用JavaScript来操作,可是操作方法 ...

  7. auto property synthesis will not synthesize proterty ;it will be implementedby its superclass, use @

    Auto property synthesis will not synthesize property 'title'; it will be implemented by its supercla ...

  8. ecshop 浏览历史样式的修改

    ecshop的浏览历史的样式,例如我修改的是只让浏览历史显示浏览历史的商品名称 而不显示浏览历史的商品的价格和图片 首先找到要修改 的文件includes\lib_insert.php 找到函数fun ...

  9. JAVA命令参数详解

    JAVA命令参数详解 JAVA命令详解 结构 说明 Java 和 OldJava JIT 选项 另请参阅 结构 java [ options ] class [ argument ... ] java ...

  10. html弹窗,与弹出对话框

    弹出对话框 <script type="text/JavaScript"> <!-- alert("Good Morning!"); //al ...