(White Book Training Program) fig 1152 4 values whose sum is 0 (midway encounter ..)

Source: Internet
Author: User

Subject address: Ultraviolet A 1152

First, enumerate the sum of set a and Set B, and then enumerate the sum of set C and set D to see how many values are opposite to each other. Question. You can use binary to find the stored value.

The Code is as follows:

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include <set>#include <algorithm>using namespace std;int a[5000], b[5000], c[6000], d[6000], p[17000000];int main(){    int t, n, m, ans, i, j, k;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        m=0;        for(i=0;i<n;i++)        {            scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);        }        for(i=0;i<n;i++)        {            for(j=0;j<n;j++)            {                p[m++]=a[i]+b[j];            }        }        sort(p,p+m);        int low, high, mid;        ans=0;        for(i=0;i<n;i++)        {            for(j=0;j<n;j++)            {                int x=-c[i]-d[j];                low=0, high=m-1;                while(low<=high)                {                    mid=low+high>>1;                    if(p[mid]>x) high=mid-1;                    else if(p[mid]<x) low=mid+1;                    else                    {                        for(k=mid;k>=0;k--)                        {                            if(p[k]==x)                            {                                ans++;                            }                            else                                break;                        }                        for(k=mid+1;k<m;k++)                        {                            if(p[k]==x)                            {                                ans++;                            }                            else                                break;                        }                        break;                    }                }            }        }        printf("%d\n",ans);        if(t)            puts("");    }    return 0;}


(White Book Training Program) fig 1152 4 values whose sum is 0 (midway encounter ..)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.