hdu 2578 Dating with girls(1)

时间:2023-03-09 06:30:55
hdu 2578 Dating with girls(1)

题目连接

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

Dating with girls(1)

Description

Everyone in the HDU knows that the number of boys is larger than the number of girls. But now, every boy wants to date with pretty girls. The girls like to date with the boys with higher IQ. In order to test the boys ' IQ, The girls make a problem, and the boys who can solve the problem 
correctly and cost less time can date with them.
The problem is that : give you n positive integers and an integer k. You need to calculate how many different solutions the equation x + y = k has . x and y must be among the given n integers. Two solutions are different if x0 != x1 or y0 != y1.
Now smart Acmers, solving the problem as soon as possible. So you can dating with pretty girls. How wonderful!
hdu 2578 Dating with girls(1)

Input

The first line contain an integer T. Then T cases followed. Each case begins with two integers n(2 <= n <= 100000) , k(0 <= k < 2^31). And then the next line contain n integers.

Output

For each cases,output the numbers of solutions to the equation.

Sample Input

2
5 4
1 2 3 4 5
8 8
1 4 5 7 8 9 2 6

Sample Output

3
5

先排序,再二分,可能有重复的元素,去重即可。。。

 #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::pair;
using std::vector;
using std::unique;
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 Max_N = ;
typedef unsigned long long ull;
int n, m;
vector<int> vec;
void solve() {
int ans = ;
sort(all(vec));
vec.erase(unique(all(vec)), vec.end());
rep(i, sz(vec)) {
vector<int>::iterator ite = lower_bound(all(vec), m - vec[i]);
if ((ite != vec.end() && *ite + vec[i] == m) || vec[i] << == m) ans++;
}
vec.clear();
printf("%d\n", ans);
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int t, v;
scanf("%d", &t);
while (t--) {
scanf("%d %d", &n, &m);
rep(i, n) scanf("%d", &v), vec.pb(v);
solve();
}
return ;
}