hdu 5101(思路题)
2024-08-23 02:24:08
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 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.
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 )
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
3 1
1 2
1 2
2 1 1
Sample Output
5
Source
题意:dudu要在不同班级里面选择两名队友参加ACM,如果两名队友的IQ1+IQ2>k (k是dudu的IQ),那么就可以选择这两名队友参赛。问总共有多少种选法。
题解:对于某个班级,假设第 j 个人的IQ是 v,那么对应这个人可以找到的队友数等于 在所有班级里面找到 IQ>k-v 的人 - 在本班找到 IQ > K-v 的人 把这些全部加起来就好了。
用一个数组记录所有班级的人IQ,然后从小到大排序,二分查找大于等于 k-v 的第一个人的位置。这里可以使用 lower_bound 来进行二分查找。最后结果要除以二,因为算重了。
时间复杂度O(n*m*log(n*m))
lower_bound:函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N = ;
const int M = ;
int v[M];
int v1[N*M];
int sum[N];
struct Node{
int num;
int v[N];
}node[N];
int main(){
int tcase,n;
int k;
scanf("%d",&tcase);
while(tcase--){
scanf("%d%d",&n,&k);
int cnt = ;
for(int i=;i<=n;i++){
scanf("%d",&node[i].num);
sum[i] = sum[i-]+node[i].num;
for(int j=;j<=node[i].num;j++){
scanf("%d",&v[j]);
v1[cnt++] = v[j];
}
sort(v+,v+node[i].num+);
for(int j=;j<=node[i].num;j++){
node[i].v[j] = v[j];
}
}
sort(v1+,v1+cnt);
long long ans = ;
for(int i=;i<=n;i++){
for(int j=;j<=node[i].num;j++){
int v = node[i].v[j];
int pos1 = node[i].v+node[i].num+ - lower_bound(node[i].v+,node[i].v++node[i].num,k-v+);
int pos2 = v1+cnt - lower_bound(v1+,v1+cnt,k-v+);
ans += pos2-pos1;
}
}
printf("%lld\n",ans/);
}
return ;
}
最新文章
- Js位置与大小(1)&mdash;&mdash;正确理解和运用与尺寸大小相关的DOM属性
- Linux字符界面的优势
- c#字符串转换为日期,支持任意字符串
- java.lang.RuntimeException: Invalid action class configuration that references an unknown class named [xxxAction]。
- Oracle存储过程java 调用
- 【C语言】02-函数
- angularjs directive and component 指令与组件 ( 1.5.0 以后 )
- Django查询的琐碎记录
- hibernate查询出的数据和数据库不一致
- Java学习之路----计算圆形的面积和周长
- MemoryStream生成Excel
- java实现随机四则运算
- 使用Jexus服务器运行Asp.Net Core2.0程序
- python中修改工作目录
- IGMP Internet组管理协议 未完
- 那些年,我们追过的PHP自加自减运算(1)
- 吴裕雄 11-MySQL查询数据
- C# RSA加解密和MD5加密
- oracle 实现主键自增
- SpringBoot Mybatis 执行自定义SQL