• [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(二分查找)的更多相关文章

  1. [LeetCode] #167# Two Sum II : 数组/二分查找/双指针

    一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...

  2. [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针

    一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...

  3. noj二分查找

    二分查找: 要么左边,要么右边,哈哈哈哈 描述 给定一个单调递增的整数序列,问某个整数是否在序列中.   输入 第一行为一个整数n,表示序列中整数的个数:第二行为n(n不超过10000)个整数:第三行 ...

  4. UVA - 1152 4 Values whose Sum is 0问题分解,二分查找

    题目:点击打开题目链接 思路:暴力循环显然会超时,根据紫书提示,采取问题分解的方法,分成A+B与C+D,然后采取二分查找,复杂度降为O(n2logn) AC代码: #include <bits/ ...

  5. LA 2678 Subsequence(二分查找)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  6. PHP-----二维数组和二分查找

    二维数组由行和列组成.由arr[$i][$j]表示,先后表示行和列,类似于坐标点. 打印二维数组-----通过两次遍历,第一次遍历每一行,第二次遍历每一行的具体元素,并且通过使用count($arr[ ...

  7. Monthly Expense(二分查找)

    Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17982 Accepted: 7190 Desc ...

  8. C. Tavas and Karafs 二分查找+贪心

    C. Tavas and Karafs #include <iostream> #include <cstdio> #include <cstring> #incl ...

  9. SPOJ TEMPLEQ - Temple Queues(二分查找+树状数组)

    题意: 有N个队伍(1 <= N <= 100,000),每个队伍开始有ai个人[0 <= ai<= 100,000,000],有Q个操作[0<=Q<= 500,0 ...

随机推荐

  1. 《spss统计分析与行业应用案例详解》:实例十二 卡方检验

    卡方检验的功能与意义 SPSS的卡方检验是非参数检验方法的一种,其基本功能足通过样本的 频数分布来推断总体是否服从某种理论分布或某种假设分布,这种检验过程是通过分析实际的频数与理论的频数之间的差别或是 ...

  2. anaconda 安装各种库

    在anaconda prompt 添加清华源 https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/ conda config --add channe ...

  3. Win10开机启动项

    键盘输入:win+r 输入命令:shell:startup

  4. 01_2_Servlet简介

    01_2_Servlet简介 1. Servlet简介 Servlet是服务器小应用程序 用来完成B/S架构下,客户端请求的响应处理 平台独立,性能优良,能以线程方式运行 Servlet API为Se ...

  5. d3.js--04(enter和exit)

    enter() 当DOM数量少于data的数量,或者压根一个都没有的时候,我们一般会希望让程序帮忙创建. <!DOCTYPE html> <html> <head> ...

  6. 二十一、C++中的临时对象

    思考: 构造函数是一个特殊的函数 是否可以直接调用? 是否可以在构造函数中调用构造函数? 直接调用构造函数的行为是什么? 答: 直接调用构造函数将产生一个临时对象 临时对象的生命周期只有一条语句的时间 ...

  7. JS正则表达式学习总结

    JS正则:java RegExp对象,它是对字符串执行模式匹配的强大工具.运用最多的就是在输入处验证输入的字符串是否合法,指定用户输入字符串的格式. 定义方法: 1:直接量语法:var re=/pat ...

  8. Eclipse上进行java web项目的打包

    以下是一个基于maven搭建的Spring Boot项目的目录结构 版权声明:本文为博主原创文章,未经博主允许不得转载. 原文地址: https://www.cnblogs.com/poterliu/ ...

  9. 阿里大鱼短信发送 FOR DT

    //增加了参数$action 来标志发送的是什么短信 注册短信 验证码短信 提示短信等 function send_sms($mobile, $message, $word = 0, $time = ...

  10. 【js】input 焦点到内容的最后

     //引用部分应支持jQuery  function find_focus(obj){    var curr = jQuery(obj);    var val = curr.val();    c ...