首页 > 代码库 > poj 2785 4 Values whose Sum is 0 (简单二分)

poj 2785 4 Values whose Sum is 0 (简单二分)

 
//每列选一个数相加为0的个数 
# include <stdio.h>
# include <algorithm>
# include <string.h>
using namespace std;	
int ab[4010*4010],cd[4010*4010];
int main()
{
	
	int n,i,k,j,count,a[4010],b[4010],c[4010],d[4010];
	while(~scanf("%d",&n))
	{
		
		for(i=0;i<n;i++)
			scanf("%d %d %d %d",&a[i],&b[i],&c[i],&d[i]);
		int cot1=0;
		int cot2=0;
		for(i=0;i<n;i++)
		{
			for(j=0;j<n;j++)
			{
				ab[cot1++]=a[i]+b[j];
				cd[cot2++]=-(c[i]+d[j]);
			}
		}
		sort(cd,cd+cot2);
		count=0;
		for(i=0;i<cot1;i++)
		{	
			int left=0;
		    int right=n*n-1;
			while(left<=right)
			{
				int mid=(left+right)/2;
				if(ab[i]==cd[mid])
				{
					count++;
					for(k=mid+1;k<cot2;k++)
					{
						if(ab[i]==cd[k])
							count++;
						else
							break;
					}
					for(k=mid-1;k>=0;k--)
					{
						if(ab[i]==cd[k])
							count++;
						else
							break;
					}
					break;
				}
				else if(ab[i]<cd[mid])
					 right=mid-1;
				else
					 left=mid+1;

			}
		}
		printf("%d\n",count);
	}
	return 0;
}