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!

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

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

 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::pair;
15 using std::vector;
16 using std::unique;
17 using std::lower_bound;
18 #define pb(e) push_back(e)
19 #define sz(c) (int)(c).size()
20 #define mp(a, b) make_pair(a, b)
21 #define all(c) (c).begin(), (c).end()
22 #define iter(c) decltype((c).begin())
23 #define cls(arr,val) memset(arr,val,sizeof(arr))
24 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
25 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
26 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
27 const int Max_N = 100010;
28 typedef unsigned long long ull;
29 int n, m;
30 vector<int> vec;
31 void solve() {
32     int ans = 0;
33     sort(all(vec));
34     vec.erase(unique(all(vec)), vec.end());
35     rep(i, sz(vec)) {
36         vector<int>::iterator ite = lower_bound(all(vec), m - vec[i]);
37         if ((ite != vec.end() && *ite + vec[i] == m) ||  vec[i] << 1 == m) ans++;
38     }
39     vec.clear();
40     printf("%d\n", ans);
41 }
42 int main() {
43 #ifdef LOCAL
44     freopen("in.txt", "r", stdin);
45     freopen("out.txt", "w+", stdout);
46 #endif
47     int t, v;
48     scanf("%d", &t);
49     while (t--) {
50         scanf("%d %d", &n, &m);
51         rep(i, n) scanf("%d", &v), vec.pb(v);
52         solve();
53     }
54     return 0;
55 }

时间: 2025-01-01 21:03:08

hdu 2578 Dating with girls(1)的相关文章

HDU 2578 Dating with girls(1) [补7-26]

Dating with girls(1) Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3869    Accepted Submission(s): 1196 Problem Description Everyone in the HDU knows that the number of boys is larger than the

hdu 2579 Dating with girls(2)

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2579 Dating with girls(2) Description If you have solved the problem Dating with girls(1).I think you can solve this problem too.This problem is also about dating with girls. Now you are in a maze and th

hdu 2579 Dating with girls(2) 【经典三维BFS】

Dating with girls(2)                                                           Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2443    Accepted Submission(s): 697 链接:http://acm.hdu.edu.cn/showp

HDU 2579 Dating with girls(2) (BFS)

Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2416    Accepted Submission(s): 690 Problem Description If you have solved the problem Dating with girls(1).I think you can

HDU 2579 Dating with girls(2) BFS 余数判重

对于石头的处理就按照每个位置的时间取k的余数判一下重复就好,其他随意写 #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <vector> #include <string> #include <queue> #include <deque> #include

HDU 5145 NPY and girls(莫队算法+乘法逆元)

[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5145 [题目大意] 给出一个数列,每次求一个区间数字的非重排列数量.答案对1e9+7取模. [题解] 我们发现每次往里加入一个新的数字或者减去一个新的数字,前后的排列数目是可以通过乘除转移的,所以自然想到用莫队算法处理.因为答案要求取模,所以在用除法的时候要计算逆元. [代码] #include <cstdio> #include <algorithm> #include <

HDU 5145 NPY and girls (莫队分块离线)

题目地址:HDU 5145 莫队真的好神奇..这样的复杂度居然只有n*sqrt(n)... 裸的莫队分块,先离线,然后按左端点分块,按块数作为第一关键字排序,然后按r值作为第二关键字进行排序.都是从小到大,可以证明这样的复杂度只有n*sqrt(n).然后进行块之间的转移. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <

hdu 5145 NPY and girls 莫队

题意: 给你1-n属于的班级 给你一个[l,r]区间 问你如果要访问这个区间内所有的女生 有多少种走不同教室的方法 思路: 和小z差不多 只不过这个维护的是阶乘 找出来公式之后莫队直接离线处理 莫队更多的是离线排序优化的思想 把所有查询排序处理 然后逐个处理 可以应用到很多方面 1 #include<bits/stdc++.h> 2 #define cl(a,b) memset(a,b,sizeof(a)) 3 #define debug cerr<<#a<<"

HDU 2578(二分查找)

686MS 1 #include <iostream> 2 #include <cstdlib> 3 #include <cstdio> 4 #include <algorithm> 5 6 using namespace std; 7 8 int a[100010]; 9 10 int n; 11 12 bool bsearch(int key) 13 { 14 int lo = 0,hi = n-1,mid = lo+(hi-lo)/2; 15 whil