51Nod 1001 数组中和等于K的数对
- http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1001
一开始的想法是排序后二分搜索,发现会进行非常多不必要的遍历,十分耗时间。
解决方法:在得到一对数之后顺便令数组长度进行缩短能够有效减少运行时间。- 1 #include<iostream>
- #include<algorithm>
- using namespace std;
- //int bs(int key,int num,int i,int n[],int sum);
- int main()
- {
- int sum,num;
- cin >> sum >> num;
- int n[num];
- for(int i=; i<num; i++)
- {
- cin >> n[i];
- }
- sort(n,n+num);
- int left = ;
- int f=;
- int right = num-;
- while(left < right)
- {
- if((sum-n[left])==n[right])
- {
- f=;
- cout << n[left] << ' ' << sum - n[left] << endl;
- right--;
- left++;
- }
- else if((sum-n[left])>n[right])
- {
- left++;
- }
- else if((sum-n[left])<n[right])
- {
- right--;
- }
- }
- // t=bs(n[i],num,i,n,sum);
- // if(t)
- // {
- // cout << n[i] << ' ' << sum - n[i] << endl;
- // }
- // for(int i=0;res[i][1]!=0&&(res[i][1]!=res[i][0]);i++)
- // {
- // cout << res[i][0] << ' ' << res[i][1] << endl;
- // f=1;
- // }
- if(f==)
- cout << "No Solution";
- return ;
- }
- //int bs(int key,int num,int left,int n[],int sum)
- //{
- // int right = num;
- // int mid;
- // while(left < right)
- // {
- // mid = (left + right) / 2;
- // if((sum-key)==n[mid])
- // return n[mid];
- // else if((sum-key)>n[mid])
- // left = mid;
- // else if((sum-key)<n[mid])
- // right = mid;
- // }
- //
- // return 0;
- //}
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的数对
Input示例 8 9 -1 6 5 3 4 2 9 0 8 Output示例 -1 9 0 8 2 6 3 5 first try: #include "bits/stdc++.h&quo ...
- 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: ...
随机推荐
- Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'zoneId' in 'class java.lang.String'
本文为博主原创,未经允许不得而转载: 异常展示: dao层定义的接口为: public int getClientTotal(); 在mybatis中的sql为: <select id=&quo ...
- 51nod 1832 先序遍历与后序遍历(dfs+高精度)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1832 题意: 思路: 官方题解如下: 可以看一下这篇文章:https:/ ...
- Codeforces Round #332 (Div. 2) D. Spongebob and Squares(枚举)
http://codeforces.com/problemset/problem/599/D 题意:给出一个数x,问你有多少个n*m的网格中有x个正方形,输出n和m的值. 思路: 易得公式为:$\su ...
- 【Python】【一些概念与对比】
type.__new__() : 返回类.可以把类看作是metaclass 创建出来的实例 普通类里的__new__() : 返回类的实例. __new__() : 返回类的实例.Python解释器 ...
- RN 使用第三方组件之react-native-image-picker(拍照/从相册获取图片)
首先给个github地址:https://github.com/react-community/react-native-image-picker 英文不行的看下面这个笔记 该插件可以同时给iOS和 ...
- [原][粒子特效][spark]事件action
深入浅出spark粒子特效连接:https://www.cnblogs.com/lyggqm/p/9956344.html group调用action的地方: 可以看到使用action的可以是出生一次 ...
- 《剑指offer》第四十九题(丑数)
// 面试题49:丑数 // 题目:我们把只包含因子2.3和5的数称作丑数(Ugly Number).求按从小到 // 大的顺序的第1500个丑数.例如6.8都是丑数,但14不是,因为它包含因子7. ...
- Android开发 打开已存在的项目(以虹软人脸识别sdk的demo为例)
详细流程参考博客https://blog.csdn.net/z979451341/article/details/79468785 个人遇到的问题与注意点 1.下载Demo后忘记修改appid和sdk ...
- Python中什么是变量
在Python中,变量的概念基本上和初中代数的方程变量是一致的. 例如,对于方程式 y=x*x ,x就是变量.当x=2时,计算结果是4,当x=5时,计算结果是25. 只是在计算机程序中,变量不仅可以是 ...
- 2018年浙江理工大学程序设计竞赛校赛 Problem I: 沙僧
沙僧 思路: dfs序+差分数组 分层考虑,通过dfs序来查找修改的区间段,然后用差分数组修改 代码: #include<bits/stdc++.h> using namespace st ...