您的位置:首页 > 其它

hdu2578(sort + 二分)

2015-10-05 18:44 246 查看
思路:先sort一下,,,要找a + b = k,那么就枚举a二分查找b,然后就是判重;

点击题目链接

/*****************************************
Author      :Crazy_AC(JamesQi)
Time        :2015
File 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: