51nod 1267 4个数和为0
来源:互联网 发布:企业qq for mac版 编辑:程序博客网 时间:2024/05/16 06:23
题目链接:
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1267
题解:
感觉是一道非常好的二分题目。
本题的方法是先确定两个数,在用两个指针去确定具体的值。
for example:
-3 -4 1 5 6 7 8 (已经对给出的数字进行了sort)
这里,我们用两个for来确定sum的值,sum=-num[i]-num[j],这里就先假设确定了-3和-4,然后我们要取的数字一定是在-3和-4之后的数字(因为已经排好了序列的),对于后面的取值的方式就主要使用二分,例如,我们先确定1和8,结果发现比7大,所以再改为1和7发现刚好吻合,当然如果发现前面的数字-3和-4没有与之匹配的数字的时候,就将其往后移动,即改为-4和1,再用上述的方法继续进行。
代码:
#include <cmath>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define met(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3ftypedef long long ll;const ll maxn = 1e3+10;int num[maxn];int main(){ int n; while(cin>>n) { for(int i=0;i<n;i++) cin>>num[i]; sort(num,num+n); for(int i=0;i<n;i++) for(int j=i+1;j<n;j++) { int sum=-num[i]-num[j]; int left=j+1,right=n-1; while(left<right) { if(num[left]+num[right]==sum) { cout<<"Yes"<<endl; return 0; } else if(num[left]+num[right]>sum) right--; else left++; } } cout<<"No"<<endl; }}
0 0
- 51nod 1267:4个数和为0 哈希
- 51 nod 1267 4个数的和为0
- 51nod 1267 4个数和为0
- 51NOD 1267 4个数和为0(二分 + 排序)
- 51nod 1267 4个数和为0 哈希+二分
- 51Nod-1267-4个数和为0
- 51nod 1267 4个数和为0
- 51nod-【1267 4个数和为0】
- 51nod 1267 4个数和为0
- 51Nod 1267 4个数和为0
- 51nod 1267 4个数和为0
- 51Nod 1267 4个数和为0 ( 二分
- 51nod 1267 4个数和为0
- 51 nod 1267 4个数和为0
- 51nod 1267 4个数和为0
- 51nod 1267 4个数和为0
- 51nod 1267 4个数和为0(二分)
- 51nod 1267 4个数和为0(暴力)
- FAAC源码阅读(4)——MPEG的基本原理
- mybatis缓存
- 机器学习(十):PCA
- linux系统下制作fat32格式windows10 U盘启动盘
- Python3.5配置Selenium
- 51nod 1267 4个数和为0
- read tetgen file
- Java编程题练习2017-02-08
- 开启聊天机器人模式
- Codeforces 764A Taymyr is calling you(水题翻译)
- Git学习使用(三):版本控制
- 关于用webpack并且实现在手机上面测试项目
- .Net基础体系和跨框架开发普及
- 陌兮大魔王带你深入 学习ArrayList(二)