【杭电oj】5101 - Select(STL & 二分)
2016-07-07 11:58
267 查看
点击打开题目
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1581 Accepted Submission(s): 441
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
第一次在每个班找人,再去其他班找人,最后得结果,然后就 TLE 了,虽然做了个剪枝。
后来的思路是:在全部人中找两两之和大于 k 的,然后减去一个班里满足条件的,最后除以 2 就行了。
代码如下:
#include <cstdio>
#include <algorithm>
using namespace std;
int iq[1011][111];
int stu[100000+11];
int main()
{
int u;
int n,k;
int m;
__int64 ans;
int ant;
scanf ("%d",&u);
while (u--)
{
scanf ("%d %d",&n,&k);
ans = m = 0;
for (int i = 0 ; i < n ; i++)
{
scanf ("%d",&iq[i][0]);
for (int j = 1 ; j <= iq[i][0] ; j++)
{
scanf ("%d",&iq[i][j]);
stu[m++] = iq[i][j];
}
sort (iq[i] + 1 , iq[i] + iq[i][0] + 1);
}
sort (stu , stu + m);
for (int i = m - 1 ; i >= 0 ; i--)
{
ant = m - (upper_bound (stu , stu + m , k - stu[i]) - stu);
if (ant)
ans += ant;
else
break;
}
for (int i = 0 ; i < n ; i++)
{
for (int j = iq[i][0] ; j >= 1 ; j--)
{
ant = iq[i][0] - (upper_bound (iq[i] + 1 , iq[i] + 1 + iq[i][0] , k - iq[i][j]) - iq[i]) + 1;
if (ant)
ans -= ant;
else
break;
}
}
ans >>= 1; //计算的结果是答案的二倍
printf ("%I64d\n",ans);
}
return 0;
}
Select
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1581 Accepted Submission(s): 441
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
第一次在每个班找人,再去其他班找人,最后得结果,然后就 TLE 了,虽然做了个剪枝。
后来的思路是:在全部人中找两两之和大于 k 的,然后减去一个班里满足条件的,最后除以 2 就行了。
代码如下:
#include <cstdio>
#include <algorithm>
using namespace std;
int iq[1011][111];
int stu[100000+11];
int main()
{
int u;
int n,k;
int m;
__int64 ans;
int ant;
scanf ("%d",&u);
while (u--)
{
scanf ("%d %d",&n,&k);
ans = m = 0;
for (int i = 0 ; i < n ; i++)
{
scanf ("%d",&iq[i][0]);
for (int j = 1 ; j <= iq[i][0] ; j++)
{
scanf ("%d",&iq[i][j]);
stu[m++] = iq[i][j];
}
sort (iq[i] + 1 , iq[i] + iq[i][0] + 1);
}
sort (stu , stu + m);
for (int i = m - 1 ; i >= 0 ; i--)
{
ant = m - (upper_bound (stu , stu + m , k - stu[i]) - stu);
if (ant)
ans += ant;
else
break;
}
for (int i = 0 ; i < n ; i++)
{
for (int j = iq[i][0] ; j >= 1 ; j--)
{
ant = iq[i][0] - (upper_bound (iq[i] + 1 , iq[i] + 1 + iq[i][0] , k - iq[i][j]) - iq[i]) + 1;
if (ant)
ans -= ant;
else
break;
}
}
ans >>= 1; //计算的结果是答案的二倍
printf ("%I64d\n",ans);
}
return 0;
}
相关文章推荐
- 深入浅出ES6(三):生成器 Generators
- SSIS用MDX 查询或多维数据库作为数据源报错的解决办法
- Xcode中的iOS模拟器(iOS Simulator)的介绍和使用心得
- 深入浅出ES6(三):生成器 Generators
- 很简洁的分页控件 适合MVC
- (C)位字段(bit-field)
- mac 安装tomcat8
- Object和Function
- 【Python】学习selenium自动化测试:操作浏览器
- 关于如何使用 chroot, 为程序设置新的执行环境chroot & schroot etc.(文件系统虚拟化)
- SAXParseException:cvc-elt.1:找不到元素“beans”的声明
- leetcode No31. Next Permutation
- $().click(function(){}) 不管用 live()替代品 append之后
- (C)位字段(bit-field)
- python 中颜色的表示
- 吐槽啊
- (C)非局部跳转语句(setjmp和longjmp)
- iOS ARC下OC对象和CF对象之间的桥接(bridge)
- html5页面简单判断当前有无网络
- 数据结构与算法简记:根据广义表构建二叉树