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的数的方案数。。。

先排序,再二分。。

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<vector>
 7 #include<map>
 8 #include<set>
 9 using std::cin;
10 using std::cout;
11 using std::endl;
12 using std::find;
13 using std::sort;
14 using std::set;
15 using std::map;
16 using std::pair;
17 using std::vector;
18 using std::lower_bound;
19 #define pb(e) push_back(e)
20 #define sz(c) (int)(c).size()
21 #define mp(a, b) make_pair(a, b)
22 #define all(c) (c).begin(), (c).end()
23 #define iter(c) decltype((c).begin())
24 #define cls(arr,val) memset(arr,val,sizeof(arr))
25 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
26 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
27 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
28 const int N = 1010;
29 typedef long long ll;
30 int num[N], arr[N * 100], rec[N][102];
31 int main() {
32 #ifdef LOCAL
33     freopen("in.txt", "r", stdin);
34     freopen("out.txt", "w+", stdout);
35 #endif
36     ll ans;
37     int t, n, k, tot;
38     scanf("%d", &t);
39     while (t--) {
40         ans = tot = 0;
41         scanf("%d %d", &n, &k);
42         rep(i, n) {
43             scanf("%d", &num[i]);
44             rep(j, num[i]) scanf("%d", &rec[i][j]),  arr[tot++] = rec[i][j];
45         }
46         sort(arr, arr + tot);
47         rep(i, tot) ans += tot - (lower_bound(arr + i, arr + tot, k - arr[i] + 1) - arr);
48         rep(i, n) {
49             sort(rec[i], rec[i] + num[i]);
50             rep(j, num[i]) {
51                 ans -= num[i] - (lower_bound(rec[i] + j, rec[i] + num[i], k - rec[i][j] + 1) - rec[i]);
52             }
53         }
54         printf("%lld
", ans);
55     }
56     return 0;
57 }
View Code
By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
原文地址:https://www.cnblogs.com/GadyPu/p/4605398.html