HDU 2578 二分
来源:互联网 发布:牙签弩淘宝多少钱 编辑:程序博客网 时间:2024/04/30 04:19
Dating with girls(1)
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3918 Accepted Submission(s): 1216
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直接k-x得到一组数据 然后 2分查找 注意去重 利用hash 也可以 只有hash#include<cstdio>#include<iostream>#include<algorithm>using namespace std;int a[100005]={0},b[100005]={0};bool hash1[1000005]={0};bool ok(int x);int n=0;int y=0;int main(){int t=0;scanf("%d",&t);while(t--){int k=0;int sum=0;y=0;memset(hash1,0,sizeof(hash1));scanf("%d%d",&n,&k);for(int i=0;i<n;i++){scanf("%d",&a[i]); if(hash1[k-a[i]]==0) {b[y++]=k-a[i];hash1[k-a[i]]=1; }}sort(a,a+n);for(int i=0;i<y;i++){if(ok(b[i])) sum++;}printf("%d\n",sum);}return 0;}bool ok(int x){int u1=0;int u2=n-1;int mid=(n-1)/2;if(a[0]==x||a[n-1]==x) return 1;while(u2-u1>1){if(x>a[mid]){u1=mid;}else if(x<a[mid]){u2=mid;}else{return 1;}mid=(u1+u2)/2;}return 0;}别人的#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>using namespace std;int a[200010],n;int find(int x){ int i=1,j=n; while(i<=j) { int mid=(i+j)/2; if(a[mid]==x||a[mid+1]==x) return 1; else if(a[mid]<x&&a[mid+1]>x) return 0; else if(a[mid]>x) j=mid-1; else i=mid+1; } return 0;}int main(){ int t; cin>>t; while(t--) { int k; cin>>n>>k; for(int i=1;i<=n;i++) scanf("%d",&a[i]); sort(a+1,a+n+1); __int64 sum=0; a[0]=-1; for(int i=1;i<=n;i++) { if(a[i]!=a[i-1]&&a[i]<=k&&find(k-a[i])) sum++; } printf("%I64d\n",sum); }}
0 0
- HDU 2578 二分
- hdu 2578 Dating with girls(1) (二分)
- hdu pie(二分+贪心)
- HDU 2899 求导+二分
- 【二分】hdu 4004
- 【二分】hdu 4022
- 【二分】hdu 4033
- hdu 2899 二分
- HDU 2141 二分
- hdu 4004 二分 过河
- hdu 4190 二分答案
- hdu 4190 #二分答案
- hdu 1150 二分匹配
- hdu 1281 二分匹配
- hdu 3081 二分匹配
- HDU 1045 二分匹配
- HDU 1083 二分匹配
- HDU 3646 DP + 二分
- 为什么谷歌要成立 Alphabet?
- java中的多态
- Vm+linux挂载U盘和SD卡的说明
- android数据库事务
- 探索javascript中函数的执行顺序
- HDU 2578 二分
- i5
- Python 并行分布式框架:Celery 超详细介绍
- opencv + Qt + codeblocks
- CentOS 上apache+Subversion搭建及常见问题处理
- JavaScript学习笔记
- sass
- HDU - 4277(暴力加+hash)
- BZOJ2223 PATULJCI COCI2009_CONTSET3