题目连接

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 = ;
  28. typedef unsigned long long ull;
  29. int n, m;
  30. vector<int> vec;
  31. void solve() {
  32. int ans = ;
  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] << == 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 ;
  55. }

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

  1. HDU 3784 继续xxx定律 & HDU 2578 Dating with girls(1)

    HDU 3784 继续xxx定律 HDU 2578 Dating with girls(1) 做3748之前要先做xxx定律  对于一个数n,如果是偶数,就把n砍掉一半:如果是奇数,把n变成 3*n+ ...

  2. hdu 2578 Dating with girls(1) (hash)

    Dating with girls(1) Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  3. 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/Oth ...

  4. hdu 2578 Dating with girls(1) 满足条件x+y=k的x,y有几组

    Dating with girls(1) Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  5. hdu 2579 Dating with girls(2)

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2579 Dating with girls(2) Description If you have sol ...

  6. hdu 2579 Dating with girls(2) (bfs)

    Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  7. Dating with girls(1)(二分+map+set)

    Dating with girls(1) Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  8. 二分-B - Dating with girls(1)

    B - Dating with girls(1) Everyone in the HDU knows that the number of boys is larger than the number ...

  9. hdoj 2579 Dating with girls(2)【三重数组标记去重】

    Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

随机推荐

  1. tar: 从成员名中删除开头的“/”

    在压缩文件时,当后面的备份目录使用绝对路径时,会出现: tar zcvf   /usr/OutFile.tar.gz  /data/CTest tar: 从成员名中删除开头的“/” 此时,对tar增加 ...

  2. java.sql.SQLException: Access denied for user 'root'

    程序在连接远程 mysql服务器时出错 java.sql.SQLException: Access denied for user 'root'@'192.168.27.129' (using pas ...

  3. XML Namespace 命名空间

    根据 Namespaces in XML W3C 推荐标准的定义,XML 命名空间 是由国际化资源标识符 (IRI) 标识的 XML 元素和属性集合:该集合通常称作 XML“词汇”. 定义 XML 命 ...

  4. 华为OJ平台——矩阵乘法

    题目描述: 如果A是个x行y列的矩阵,B是个y行z列的矩阵,把A和B相乘,其结果将是另一个x行z列的矩阵C. 输入: 1.第一个矩阵的行数 2.第一个矩阵的列数(也是第二个矩阵的行数) 3.第二个矩阵 ...

  5. asp.net中如何绑定combox下拉框数据(调用存储过程)

    #region 绑定类型(商品类型.仓库名称) public void DataType_Bind(int _peoid) { DataTable dt_goodsname = new DataTab ...

  6. ERS-2卫星

    ERS-2卫星是欧洲空间局的第二颗资源遥感卫星,携带有雷达测高仪.综合孔径雷达等多种科学仪器,是一颗多任务.多学科的科学考察卫星.[1] ERS-1 ERS-2 欧空局分别于1991年和1995年发射 ...

  7. Asp.net默认配置下,Session莫名丢失的原因及解决

    Asp.net默认配置下,Session莫名丢失的原因及解决 我们平时写的asp.net程序,里面要用到Session来保存一些跨页面的数据.但是Session会经常无故丢失,上网查查,也没找到原因. ...

  8. .NET 命名规范 代码示例

    class Person { /// <summary> /// 公有字段.属性 首字母大写 /// </summary> public string FirstName; p ...

  9. libpcap报文解析: ipv4、ipv6 @ 2014.7.2

    #include <string.h> #include <stdlib.h> #include <pcap.h> #include <stdio.h> ...

  10. hbase很有价值的读写性能提升

    NoSQL现在风生水起,hbase的使用也越来越广,但目前几乎所有的NoSQL产品在运维上都没法和DB相提并论,在这篇blog中来总结下我们在运维hbase时的一些问题以及解决的方法,也希望得到更多h ...