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
先排序,再二分,可能有重复的元素,去重即可。。。
#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 ;
}
hdu 2578 Dating with girls(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+ ...
- 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 ...
- 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 ...
- 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 ...
- hdu 2579 Dating with girls(2)
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2579 Dating with girls(2) Description If you have sol ...
- 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 ...
- Dating with girls(1)(二分+map+set)
Dating with girls(1) Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 二分-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 ...
- hdoj 2579 Dating with girls(2)【三重数组标记去重】
Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
随机推荐
- C语言,不是从hello world开始
开始看C语言,主要是复习,所以就没必要从hello world开始了,写点例子熟悉下就好了. 使用公式℃=(5/9)(℉-32)打印下列华氏温度与摄氏温度对照表: #include <stdio ...
- 学习总结 java连接数据库
package com.hanqi.test; import java.sql.*; public class jdbcTest { public static void main(String[] ...
- Mysql-5.7.10启动失败 。
Mysql-5.7.10在免安装后启动服务失败. 查看日志得到如下: 2016-02-19T03:41:05.557095Z 0 [Warning] TIMESTAMP with implicit D ...
- UTC格式转换 & 十六进制换算为十进制
UTC格式转换成北京时间格式: /// <summary> /// UTC格式与datatime的转换 /// </summary> /// <param name=&q ...
- LED点阵显示名字
学习了一周的单片机,然后中途有事打断了两周,现在温习一下之前学的东西,用“普中科技单片机”上的LED点阵显示自己的名字. P1接J19(绿).P2接J20(红),P0接J12(数码管段选):5V电压: ...
- cacti快速安装
一.cacti概述 1. cacti是用php语言实现的一个软件,它的主要功能是用snmp服务获取数据,然后用rrdtool储存和更新数据,当用户需要查看数据的时候用rrdtool生成图表呈现给用户. ...
- 【MySQL】删除大表的讨论【转】
转自http://tech.ddvip.com/2013-07/1373269453198566.html 微博上讨论MySQL在删除大表engine=innodb(30G+)时,如何减少MySQL ...
- leetcode 9
判断一个数是否为回文数,不利用额外的空间. 思路:将数反转后进行比较. 注意:反转之后数越界的判断,若越界,则不是回文数:负数不是回文数: 代码如下: class Solution { public: ...
- Linux下对于inode的理解
0x01 什么是inode 文件存储在硬盘上,硬盘的最小存储单位叫做“扇区”(Sector),每个扇区储存512字节: 操作系统读取硬盘时,不会一个个扇区地读取,这样效率低,而是一次性连续读取多个扇区 ...
- Rich控件二
Calendar控件 使用案例: 在Default.aspx中: <div> <h1>Calendar控件</h1> <asp:Calendar ID=&q ...