NOJ——1649Find Sum(二分查找)
[1649] Find Sum
- 时间限制: 1000 ms 内存限制: 65535 K
- 问题描述
This problem is really boring.
You are given a number sequence S which contain n integers and m queries, each time i will give your two integers id1 and id2, you need to tell me whether i can get S[id1] + S[id2] from the n integers.
- 输入
- Input starts with an integer T(T <= 5) denoting the number of test cases.
For each test case, n(1 <= n <= 100000, 1 <= m <= 10000) are given, and the second line contains n integers, each of them is larger then 0 and no larger than 10^11.
Next m lines, each line contains two integers id1 and id2(1 <= id1, id2 <= n). - 输出
- For each case, first print the case number.
For each query, print “Yes” if i can get S[id1] + S[id2] from S, otherwise print “No”(without the quote). - 样例输入
- 1
- 5 3
- 3 4 2 1 3
- 1 2
- 3 4
- 4 5
- 1
- 样例输出
- Case 1:
- No
- Yes
- Yes
- Case 1:
会了二分查找之后这类题型做起来简直爽翻。科科~果然二分一般都是最先接触的算法吗。。。注意一点就是题目中给的数据或者说中间数据会超出int范围,第一次交RE了,改成__int64就AC了
代码:
- #include<iostream>
- #include<algorithm>
- #include<cstdio>
- using namespace std;
- bool bi_search(const __int64 list[],const __int64 &len,const __int64 &goal)
- {
- __int64 l=0,r=len-1;
- while (l<=r)
- {
- __int64 mid=(l+r)>>1;
- if(list[mid]==goal)
- return true;
- else if(list[mid]<goal)
- {
- l=mid+1;
- }
- else
- {
- r=mid-1;
- }
- }
- return false;
- }
- int main(void)
- {
- int t,q;
- scanf("%d",&t);
- for (q=1; q<=t; q++)
- {
- __int64 n,m,i,d1,d2;
- scanf("%I64d%I64d",&n,&m);
- __int64 *list=new __int64[n];//用来放初始值
- __int64 *tlist=new __int64[n];//用来保存sort之后的有序序列,不然无法二分
- for (i=0; i<n; i++)
- {
- scanf("%I64d",&list[i]);
- tlist[i]=list[i];
- }
- sort(tlist,tlist+n);
- printf("Case %d:\n",q);
- for (i=0; i<m; i++)
- {
- scanf("%I64d %I64d",&d1,&d2);
- if(bi_search(tlist,n,list[d1-1]+list[d2-1]))
- printf("Yes\n");
- else
- printf("No\n");
- }
- delete []list;
- delete []tlist;
- }
- return 0;
- }
NOJ——1649Find Sum(二分查找)的更多相关文章
- [LeetCode] #167# Two Sum II : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
- [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针
一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...
- noj二分查找
二分查找: 要么左边,要么右边,哈哈哈哈 描述 给定一个单调递增的整数序列,问某个整数是否在序列中. 输入 第一行为一个整数n,表示序列中整数的个数:第二行为n(n不超过10000)个整数:第三行 ...
- UVA - 1152 4 Values whose Sum is 0问题分解,二分查找
题目:点击打开题目链接 思路:暴力循环显然会超时,根据紫书提示,采取问题分解的方法,分成A+B与C+D,然后采取二分查找,复杂度降为O(n2logn) AC代码: #include <bits/ ...
- LA 2678 Subsequence(二分查找)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- PHP-----二维数组和二分查找
二维数组由行和列组成.由arr[$i][$j]表示,先后表示行和列,类似于坐标点. 打印二维数组-----通过两次遍历,第一次遍历每一行,第二次遍历每一行的具体元素,并且通过使用count($arr[ ...
- Monthly Expense(二分查找)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17982 Accepted: 7190 Desc ...
- C. Tavas and Karafs 二分查找+贪心
C. Tavas and Karafs #include <iostream> #include <cstdio> #include <cstring> #incl ...
- SPOJ TEMPLEQ - Temple Queues(二分查找+树状数组)
题意: 有N个队伍(1 <= N <= 100,000),每个队伍开始有ai个人[0 <= ai<= 100,000,000],有Q个操作[0<=Q<= 500,0 ...
随机推荐
- 如何将S/4HANA系统存储的图片文件用Java程序保存到本地
我在S/4HANA的事务码MM02里为Material维护图片文件作为附件: 通过如下简单的ABAP代码即可将图片文件的二进制内容读取出来: REPORT zgos_api. DATA ls_appl ...
- HDU - 5457 Hold Your Hand (Trie + 最小割)
Hold Your Hand Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 65535/102400 K (Java/Others)T ...
- [web开发] Vue + spring boot + echart 微博爬虫展示平台
1.微博登录 2.爬取数据 3.mysql存储 4.pyechart本地展示 5.用vue搭建网站web展示 先放图: 1.微博登录 新浪微博的登录不是简单的post就能解决的,他的登录有加密,所以我 ...
- freespace
这是一个总结freespace的博客,还不错,但算法目前看来比较老:https://blog.csdn.net/u010213183/article/details/70786516
- python 嵌套作用域 闭包函数
#闭包函数 def multiplier(factor): def multiplyByFactory(number): return number*factor return multiplyByF ...
- 解决Error"基础连接已经关闭: 未能为SSL/TLS 安全通道建立信任关系
今天写程序的时候调用到一个第三方的DLL文件,本机调试一切都正常,但是程序不是到服务器以后一直提示一个BUG:"基础连接已经关闭: 未能为SSL/TLS 安全通道建立信任关系". ...
- Ubuntu Server 18.04 LTS安装
Please choose your preferred language. 选择您喜欢的语言 这里直接选择[English] Keyboard configuration 键盘配置 Please s ...
- 批量ping IP并检测IP延迟率和丢包率脚本
脚本文件如下: #!/bin/bash #Author:Mr.Ding #Created Time:2018-08-26 07:23:44 #Name:ping.sh #Description: sh ...
- Goroutines和Channels
原文链接 https://golangbot.com/goroutines/ Goroutines Goroutines 可以被认为是多个函数或方法同时允许.可以认为是一个轻量级的线程.与线程的花费相 ...
- python中文件操作的其他方法
前面介绍过Python中文件操作的一般方法,包括打开,写入,关闭.本文中介绍下python中关于文件操作的其他比较常用的一些方法. 首先创建一个文件poems: p=open('poems','r', ...