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; }
相关文章推荐
- mapreduce的debug
- mahout探索推荐引擎内部的秘密
- Hadoop ->> Hadoop是什么?
- Android打开/关闭蓝牙封装工具类
- 【LaTex】学习笔记:入门使用方法
- mahout 返回主页 WEB系统架构与底层研发
- linux之cut用法
- 百度云批量离线python程序
- php mysql_real_escape_string() 函数
- 深入理解C/C++ [Deep C (and C++)]
- Android-它们的定义Dialog
- 全屏轮播插件fullpage.js的使用
- 第6周—项目3 括号的匹配
- UVA315- Network(无向图割点)
- Linux里提示cannot find -lsocket解决办法
- HDU1010 Tempter of the Bone
- POJ 1020 Anniversary Cake 回溯DFS
- iOS 新浪微博-1.1框架升级
- 搜索与回溯算法(一)
- linux虚拟机配置双网卡