HDU2578 Dating with girls(1)(二分)
来源:互联网 发布:淘宝能不能花钱解封 编辑:程序博客网 时间:2024/06/05 16:13
Dating with girls(1)
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4870 Accepted Submission(s): 1557
Problem 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!
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
25 41 2 3 4 58 81 4 5 7 8 9 2 6
Sample Output
35
Source
HDU 2009-5 Programming Contest
http://acm.split.hdu.edu.cn/showproblem.php?pid=2578题意很给力 典型的二分 注意位置不同也算一种 相同数字去掉
#include<stdio.h>#include<algorithm>#include<string.h>using namespace std;int aa[100010];int main(){ int t,n,k,i; scanf("%d",&t); while(t--) { scanf("%d %d",&n,&k); for(i=0; i<n; i++) scanf("%d",&aa[i]); sort(aa,aa+n); int len=unique(aa,aa+n)-aa; long long sum=0; int l,r; for(i=0; i<len; i++) { l=0,r=len; int hj=k-aa[i]; while(l<r) { int mid=(l+r)>>1; if(aa[mid]<hj) l=mid+1; else if(aa[mid]>hj)r=mid-1; else {l=mid; break;} } if(aa[l]==hj) sum++; } printf("%I64d\n",sum); } return 0;}
阅读全文
0 0
- HDU2578 Dating with girls(1)(二分)
- HDU2578 Dating with girls(1)(二分)
- HDU2578:Dating with girls(1)
- HDU2578--Dating with girls(1) (map||排序+二分)
- HDU2578.Dating with girls(1)——二分
- Dating with girls(1)(二分)
- HDU2578——Dating with girls
- hdu 2578 Dating with girls(1)(二分查找)
- hdu 2578 Dating with girls(1)(二分查找)
- hdoj 2578 Dating with girls(1)(二分+队列)
- hdu 2578 Dating with girls(1) (二分)
- Dating with girls(1)
- Dating with girls(1)
- Dating with girls(1)
- hdu 2578 Dating with girls(1) 排序+二分查找 水题
- HDOJ-2578-Dating with girls(1) 【排序 去重 二分】
- hdoj 2578 Dating with girls(1) 【排序+二分+去重】
- hdu (2578) Dating with girls(1)
- item点击波纹动画
- WWDC 2016 Session笔记
- 《HTML5开发手册》学习笔记(一)
- chap7内存分配
- Android APK反编译就这么简单 详解(附图)
- HDU2578 Dating with girls(1)(二分)
- RxJava2和Retrofit封装的RetrofitClient2终于来了!
- 删除一个无头单链表的非尾节点【每日一题】
- [BZOJ]4810 由乃的玉米田 莫队+bitset
- WWDC 2016 Session笔记
- 解决报错:;Syntax error on token(s), misplaced construct(s)
- 【JavaSE学习笔记】面向对象_接口、形式参数问题、内部类
- ARC 061E すぬけ君の地下鉄旅行 最短路(拆点)
- Hive几种数据导出方式