View Code of Problem 3686

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
/*可以分两步*/
/*先将沙漏的上半部分求出来 再求下半部分*/ 
int inc(const void *a,const void *b)
{
	return *(int *)a-*(int *)b;
}
int main(void)
{
	int t;
	scanf("%d",&t);
	while(t--)
	{	getchar();
		long  n;
		int x;
		scanf("%ld%d",&n,&x);
		int a[n];
		for(int i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
		}
		qsort(a,n,sizeof(int ),inc);
		int low=0,high=n-1;
		while(low<high)
		{
			if(a[low]+a[high]==x)
			{
				printf("YES\n");
				break;
			}
			while(a[low]+a[high]>x)
			{
				high--;
			}
			while(a[low]+a[high]<x)
			{
				low++;
			}
		}
		if(low>=high)
		{
			printf("NO\n");
		}
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 3686