51Nod 1001数组中和等于K的数对

8 9
-1
6
5
3
4
2
9
0
8
-1 9
0 8
2 6
3 5
first try:
#include "bits/stdc++.h"
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f3f
#define PI acos(-1)
#define N 50010
int arr[N];
int main()
{
int n,k;
while(~scanf("%d%d",&k,&n)){
for(int i=;i<n;i++){
scanf("%d",&arr[i]);
}
sort(arr,arr+n);
int c=;
for(int i=;i<n;i++){
for(int j=i+;j<n;j++){
if(arr[i]+arr[j]==k){
printf("%d %d\n",arr[i],arr[j]);
c++;
}
}
}
if(!c){
printf("No Solution\n");
}
}
return ;
}
O(n^2)
Time limit exceeded,优化
second try:
第二层从后向前找,及时跳出
#include "bits/stdc++.h"
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f3f
#define PI acos(-1)
#define N 50010
int arr[N];
int main()
{
int n,k;
while(~scanf("%d%d",&k,&n)){
for(int i=;i<n;i++){
scanf("%d",&arr[i]);
}
sort(arr,arr+n);
int c=;
for(int i=;i<n;i++){
for(int j=n-;j>=i+;j--){
if(arr[i]+arr[j]==k){
printf("%d %d\n",arr[i],arr[j]);
c++;
break;
}
}
}
if(!c){
printf("No Solution\n");
}
}
return ;
}
Time limit exceeded,再优化
third try:
转换思维,查找互补数是否存在,二分查找,分治
#include "bits/stdc++.h"
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f3f
#define PI acos(-1)
#define N 50010
int arr[N];
int Find(int n,int x){
int r=n-,l=,m;
while(l<=r){
m=(r+l)/;
if(x==arr[m])
return m;
else if(x>arr[m])
l=m+;
else
r=m-;
}
return -;
}
int main()
{
int n,k;
while(~scanf("%d%d",&k,&n)){
for(int i=;i<n;i++){
scanf("%d",&arr[i]);
}
sort(arr,arr+n);
int c=,tt;
for(int i=;i<n;i++){
tt=Find(n,k-arr[i]);
if(tt!=-){
if(tt<=i)
break;
printf("%d %d\n",arr[i],k-arr[i]);
c++;
}
}
if(!c){
printf("No Solution\n");
}
}
return ;
}
51Nod 1001数组中和等于K的数对的更多相关文章
- 51nod 1001 数组中和等于K的数对【二分查找/排序】
1001 数组中和等于K的数对 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 给出一个整数K和一个无序数组A,A的元素为N个互不相同的整数,找出数组 ...
- 51Nod 1001 数组中和等于K的数对 And 1015 水仙花数
1001 数组中和等于K的数对 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 给出一个整数K和一个无序数组A,A的元素为N个互不相同的整数,找出数组A中所有和等于K ...
- 51Nod 1001 数组中和等于K的数对 Label:Water
给出一个整数K和一个无序数组A,A的元素为N个互不相同的整数,找出数组A中所有和等于K的数对.例如K = 8,数组A:{-1,6,5,3,4,2,9,0,8},所有和等于8的数对包括(-1,9),(0 ...
- 51Nod 1001 数组中和等于K的数对 Set
给出一个整数K和一个无序数组A,A的元素为N个互不相同的整数,找出数组A中所有和等于K的数对.例如K = 8,数组A:{-1,6,5,3,4,2,9,0,8},所有和等于8的数对包括(-1,9),(0 ...
- 51Nod 1001 数组中和等于K的数对
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1001一开始的想法是排序后二分搜索,发现会进行非常多不必要的遍历,十分耗时 ...
- 1001 数组中和等于K的数对
1001 数组中和等于K的数对 基准时间限制:1 秒 空间限制:131072 KB 给出一个整数K和一个无序数组A,A的元素为N个互不相同的整数,找出数组A中所有和等于K的数对.例如K = 8,数组A ...
- 1001 数组中和等于K的数对 1002 数塔取数问题 1003 阶乘后面0的数量 1004 n^n的末位数字 1009 数字1的数量
1001 数组中和等于K的数对 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 给出一个整数K和一个无序数组A,A的元素为N个互不相同的整数,找出数组A中所有和等于K ...
- 1001 数组中和等于K的数对 1090 3个数和为0
二分查找.对数组每个V[i],在其中查找K-V[i],查找完成后修改v[i]避免重复输出 #include<iostream> #include<algorithm> #inc ...
- 51Nod - 1001:数组中和等于K的数对
基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 给出一个整数K和一个无序数组A,A的元素为N个互不相同的整数,找出数组A中所有和等于K的数对.例如K = 8,数组A: ...
随机推荐
- NProgress.js加载进度插件的简单实用方法
NProgress.js 说明: NProgress是基于jquery的,且版本要 >1.8 下载地址: https://github.com/rstacruz/nprogress API: N ...
- PHPCMS v9的表单向导实现问答咨询功能的方法
本文主要介绍了在phpcms v9的表单向导里实现问答咨询功能的方法 phpcms v9内容管理系统本身是没有问答模块的,只有表单向导,但表单向导有很大的局限性,通过表单向导,我们只能查看用户提交的信 ...
- 在cmd里面使用mysql命令
1.先找出mysqld文件所在的位置,我的是在C:\Program Files\MySQL\MySQL Server 5.1\bin. 2.cd C:\Program Files\MySQL\MySQ ...
- iOS开发UUIView动画方法总结
#动画设置 UIView动画实现 @interface ViewController () @property (weak, nonatomic) IBOutlet UIView *myView; @ ...
- iOS关于setContentOffset的一些细节问题
在UIScrollView,setContentOffset方法的功能是跳转到你指定内容的坐标, setContentOffset有两种方法:setContentOffset:和setContentO ...
- 【week6】约跑App视频链接
约跑视频链接发布在优酷,链接如下: http://v.youku.com/v_show/id_XMTc3NTcyNTcyNA==.html 秒拍视频连接: http://www.miaopai.com ...
- IIS部署网部常用问题汇总
1.unrecognized attribute 'targetframework' A: 需要注册.net framework到iis.步骤如下: (1)'Start' -> 'CMD' (2 ...
- java直接访问JNDI工具代码
import java.sql.*; import java.util.*; import javax.naming.*; import javax.sql.DataSource; public cl ...
- bsxfun函数
函数功能:两个数组间元素逐个计算的二值操作 使用方法:C=bsxfun(fun,A,B) 两个数组A合B间元素逐个计算的二值操作,fun是函数句柄或者m文件,也可以为如下内置函数: @plus 加@m ...
- 【hdu3555】Bomb 数位dp
题目描述 求 1~N 内包含数位串 “49” 的数的个数. 输入 The first line of input consists of an integer T (1 <= T <= 1 ...