hdu2578(sort + 二分)

来源:互联网 发布:软件开发学习课程 编辑:程序博客网 时间:2024/06/05 20:08

思路:先sort一下,,,要找a + b = k,那么就枚举a二分查找b,然后就是判重;

点击题目链接

/*****************************************Author      :Crazy_AC(JamesQi)Time        :2015File Name   :找两个数,a + b = k,枚举a,二分查找b;*****************************************/// #pragma comment(linker, "/STACK:1024000000,1024000000")#include <iostream>#include <algorithm>#include <iomanip>#include <sstream>#include <string>#include <stack>#include <queue>#include <deque>#include <vector>#include <map>#include <set>#include <stdio.h>#include <string.h>#include <math.h>#include <stdlib.h>#include <limits.h>using namespace std;#define MEM(a,b) memset(a,b,sizeof a)typedef long long LL;typedef unsigned long long ULL;typedef pair<int,int> ii;const int inf = 1 << 30;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;inline int Readint(){char c = getchar();while(!isdigit(c)) c = getchar();int x = 0;while(isdigit(c)){x = x * 10 + c - '0';c = getchar();}return x;}int a[100050];int bin(int x,int n){int low = 0,high = n - 1;while(low <= high){int mid = (low + high) >> 1;if (x < a[mid]) high = mid - 1;else if (x > a[mid])low = mid + 1;else return 1;//a[mid] == x;}return 0;//Not find;}int main(){// freopen("in.txt","r",stdin);// freopen("out.txt","w",stdout);int t;int n,k;scanf("%d",&t);while(t--){bool flag = true;scanf("%d%d",&n,&k);int ans = 0;for (int i = 0;i < n;i++)scanf("%d",&a[i]);sort(a,a + n);for (int i = 0;i < n;i++){if (bin(k - a[i],n)){ans++;if (i != 0 && a[i] == a[i - 1])//判重;ans--;}}printf("%d\n",ans);}return 0;}


0 0
原创粉丝点击