hdu 5101 Select

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5101

Select

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 \leq 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 \leq n \leq 1000), k\ ( 0 \leq k \leq 2^{31} ).$
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 \leq m \leq 100 ), v[i]\ ( 0 \leq v[i] < 2^{31} )$

Output

For each test case, output a single integer.

Sample Input

1
3 1
1 2
1 2
2 1 1

Sample Output

5

思路:从所有数中选择的两个加和大于k的数的方案数-在同一个集合中选择的两个加和大于k的数的方案数。。。

先排序,再二分。。

 #include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
#include<set>
using std::cin;
using std::cout;
using std::endl;
using std::find;
using std::sort;
using std::set;
using std::map;
using std::pair;
using std::vector;
using std::lower_bound;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr,val) memset(arr,val,sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = ;
typedef long long ll;
int num[N], arr[N * ], rec[N][];
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
ll ans;
int t, n, k, tot;
scanf("%d", &t);
while (t--) {
ans = tot = ;
scanf("%d %d", &n, &k);
rep(i, n) {
scanf("%d", &num[i]);
rep(j, num[i]) scanf("%d", &rec[i][j]), arr[tot++] = rec[i][j];
}
sort(arr, arr + tot);
rep(i, tot) ans += tot - (lower_bound(arr + i, arr + tot, k - arr[i] + ) - arr);
rep(i, n) {
sort(rec[i], rec[i] + num[i]);
rep(j, num[i]) {
ans -= num[i] - (lower_bound(rec[i] + j, rec[i] + num[i], k - rec[i][j] + ) - rec[i]);
}
}
printf("%lld\n", ans);
}
return ;
}
上一篇:第一个只出现一次的字符,josephus环,最大子数组和


下一篇:redis客户端可以连接集群,但JedisCluster连接redis集群一直报Could not get a resource from the pool