View Code of Problem 133

#include <stdio.h>
#include "stdio.h"
#include "string.h"
#include "math.h"

void quicksort(int left,int right,int *a)
{
    int i=left,j=right,key=a[left];
    if(left>=right) return;
    while(i<j)
    {
        while(i<j&&a[j]>=key)
        {
            j--;
        }
        a[i]=a[j];
        while(i<j&&a[i]<=key)
        {
            i++;
        }
        a[j]=a[i];
    }
    a[i]=key;
    quicksort(left,i-1,a);
    quicksort(i+1,right,a);
}

int Bsort(int *a,int e,int key)
{
    int left=0,end=e,mid;
    while(left<=end)
    {
        mid=(left+end)/2;
        if(key<a[mid]) end=mid-1;
        else if(key>a[mid]) left=mid+1;
        else if(key==a[mid]) return a[mid];
    }
    return -99999;
}

int main()
{
    int m;
    scanf("%d",&m);
    while(m--)
    {
        int a,b,ss[100001],leap=0;
        scanf("%d %d",&a,&b);
        for(int i=0;i<a;i++)
        {
            scanf("%d",&ss[i]);
        }
        quicksort(0,a-1,ss);
        
        for(int i=0;i<a;i++)
        {
            int tmp=b-ss[i];
            if(ss[i]==ss[i-1]) {
                continue;
            }
            if(Bsort(ss,a-1,tmp)!=-99999)
            {
                leap=1;
                printf("YES\n");
                break;
            }
        }
        if(!leap) printf("NO\n");
    }
    return 0;
}

Double click to view unformatted code.


Back to problem 133