• [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. 1
    2. 5 3
    3. 3 4 2 1 3
    4. 1 2
    5. 3 4
    6. 4 5
  • 样例输出
    1. Case 1:
    2. No
    3. Yes
    4. Yes

会了二分查找之后这类题型做起来简直爽翻。科科~果然二分一般都是最先接触的算法吗。。。注意一点就是题目中给的数据或者说中间数据会超出int范围,第一次交RE了,改成__int64就AC了

代码:

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstdio>
  4. using namespace std;
  5. bool bi_search(const __int64 list[],const __int64 &len,const __int64 &goal)
  6. {
  7. __int64 l=0,r=len-1;
  8. while (l<=r)
  9. {
  10. __int64 mid=(l+r)>>1;
  11. if(list[mid]==goal)
  12. return true;
  13. else if(list[mid]<goal)
  14. {
  15. l=mid+1;
  16. }
  17. else
  18. {
  19. r=mid-1;
  20. }
  21. }
  22. return false;
  23. }
  24. int main(void)
  25. {
  26. int t,q;
  27. scanf("%d",&t);
  28. for (q=1; q<=t; q++)
  29. {
  30. __int64 n,m,i,d1,d2;
  31. scanf("%I64d%I64d",&n,&m);
  32. __int64 *list=new __int64[n];//用来放初始值
  33. __int64 *tlist=new __int64[n];//用来保存sort之后的有序序列,不然无法二分
  34. for (i=0; i<n; i++)
  35. {
  36. scanf("%I64d",&list[i]);
  37. tlist[i]=list[i];
  38. }
  39. sort(tlist,tlist+n);
  40. printf("Case %d:\n",q);
  41. for (i=0; i<m; i++)
  42. {
  43. scanf("%I64d %I64d",&d1,&d2);
  44. if(bi_search(tlist,n,list[d1-1]+list[d2-1]))
  45. printf("Yes\n");
  46. else
  47. printf("No\n");
  48. }
  49. delete []list;
  50. delete []tlist;
  51. }
  52. return 0;
  53. }

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. 如何将S/4HANA系统存储的图片文件用Java程序保存到本地

    我在S/4HANA的事务码MM02里为Material维护图片文件作为附件: 通过如下简单的ABAP代码即可将图片文件的二进制内容读取出来: REPORT zgos_api. DATA ls_appl ...

  2. HDU - 5457 Hold Your Hand (Trie + 最小割)

    Hold Your Hand Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)T ...

  3. [web开发] Vue + spring boot + echart 微博爬虫展示平台

    1.微博登录 2.爬取数据 3.mysql存储 4.pyechart本地展示 5.用vue搭建网站web展示 先放图: 1.微博登录 新浪微博的登录不是简单的post就能解决的,他的登录有加密,所以我 ...

  4. freespace

    这是一个总结freespace的博客,还不错,但算法目前看来比较老:https://blog.csdn.net/u010213183/article/details/70786516

  5. python 嵌套作用域 闭包函数

    #闭包函数 def multiplier(factor): def multiplyByFactory(number): return number*factor return multiplyByF ...

  6. 解决Error"基础连接已经关闭: 未能为SSL/TLS 安全通道建立信任关系

    今天写程序的时候调用到一个第三方的DLL文件,本机调试一切都正常,但是程序不是到服务器以后一直提示一个BUG:"基础连接已经关闭: 未能为SSL/TLS 安全通道建立信任关系".  ...

  7. Ubuntu Server 18.04 LTS安装

    Please choose your preferred language. 选择您喜欢的语言 这里直接选择[English] Keyboard configuration 键盘配置 Please s ...

  8. 批量ping IP并检测IP延迟率和丢包率脚本

    脚本文件如下: #!/bin/bash #Author:Mr.Ding #Created Time:2018-08-26 07:23:44 #Name:ping.sh #Description: sh ...

  9. Goroutines和Channels

    原文链接 https://golangbot.com/goroutines/ Goroutines Goroutines 可以被认为是多个函数或方法同时允许.可以认为是一个轻量级的线程.与线程的花费相 ...

  10. python中文件操作的其他方法

    前面介绍过Python中文件操作的一般方法,包括打开,写入,关闭.本文中介绍下python中关于文件操作的其他比较常用的一些方法. 首先创建一个文件poems: p=open('poems','r', ...