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- 样例输出
Case 1:
No
Yes
Yes
会了二分查找之后这类题型做起来简直爽翻。科科~果然二分一般都是最先接触的算法吗。。。注意一点就是题目中给的数据或者说中间数据会超出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 ...
随机推荐
- WCFSVC文件的分离
项目结构图如下: 新建一个实现内容和接口的项目: 接口内部如下: using DataModel; using System; using System.Collections.Generic; us ...
- UVA11478 Halum (差分约束)
每次操作是独立的,而且顺序并不影响,作用在同一个结点上的d可以叠加,所以令x(u) = sigma(dui). 最后就是要确定所有的x(u). 因为m越大,满足条件的边就越少,二分答案m. 对于一条边 ...
- ping请求超(iPV4)
ping请求超(iPV4) arp –a(查看局域网全部IP) cmd 管理员运行 netsh winsock reset(重置Winsock目录借以恢复网络) etsh int ip reset r ...
- Python SciPy Sparse模块学习笔记
1. sparse模块的官方document地址:http://docs.scipy.org/doc/scipy/reference/sparse.html 2. sparse matrix的存储 ...
- C#与SQLServer数据库连接
第一种连接数据库方法:直接通过数据库的用户名.密码等连接 步骤: (1)建立SqlConnection对象,指定SqlConnection对象的ConnectionString属性: (2)打开数据库 ...
- [提供可行性脚本] RHEL/CentOS 7 多节点SSH免密登陆
实验说明: 在自动化部署时,会经常SSH别的机器去操作,然而每次的密码认证却很令人烦躁,尤其是很长的密码,因此SSH免密登陆就显得必不可少: 在机器数目很多的时候,使用更过的往往是Ansible分发并 ...
- Django之模型---ORM 多表操作
多表操作 创建表模型 from django.db import models # Create your models here. class Author(models.Model): nid = ...
- (转)iOS获取设备型号
//获得设备型号 + (NSString *)getCurrentDeviceModel:(UIViewController *)controller { ]; size_t len; char *m ...
- LeetCode(232) Implement Queue using Stacks
题目 Implement the following operations of a queue using stacks. push(x) – Push element x to the back ...
- ax=1(%b) 求最小逆元
定理一:如果d = gcd(a, b),则必能找到正的或负的整数x和y,使 d = a*x+ b*y. 定理二:若gcd(a, b) = ,则方程ax ≡ c (mod b)在[, b-]上有唯一解. ...