POJ2366【二分】

题意:
给两个序列,问两个序列中是否有两个数加起来=1e4;
思路:
直接先排序好b序列,然后枚举a序列,二分查找b序列就好了;
贴一发挫code….

//#include <bits/stdc++.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<cstdio>
#include<algorithm>
using namespace std;
const float pi=acos(float(-1));

const int N=5e4+10;
int a[N];
int b[N];

int main()
{
    int n,m;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    scanf("%d",&m);
    for(int j=1;j<=m;j++)
        scanf("%d",&b[j]);
    sort(b+1,b+m+1);

    int s,t,mid;
    for(int i=1;i<=n;i++)
    {
        int x=10000-a[i];
//        if(m==1)
//        {
//            if(b[m]==x)
//            {
//                printf("YES
");
//                return 0;
//            }
//            continue;
//        }
        s=1,t=m;
        while(s<=t)
        {
            int mid=(s+t)/2;
            if(b[mid]<x)
            {
                s=mid+1;
            }
            else if(b[mid]==x)
            {
                printf("YES
");
                return 0;
            }
            else
                t=mid-1;
        }
    }
    printf("NO
");
    return 0;
}
原文地址:https://www.cnblogs.com/keyboarder-zsq/p/5934864.html