【A + B + C + D】 問題

來源:互聯網
上載者:User

標籤:des   style   blog   color   io   os   ar   java   for   

A + B + C + D

Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 130    Accepted Submission(s): 44

Problem DescriptionLittle Robert asked his mother for two cents. "What did you do withthe money I gave you yesterday?""I gave it to a poor old woman," heanswered."You‘re a good boy," said the mother proudly. "Here aretwo cents more. But why are you so interested in the oldwoman?""She is the one who sells the candy."A joke, hereentered
Below n four tuple (A, B, C, D), respectively, from the A, B, C, Dselected a, b ,c ,d To calculate the number of combinations ofa+b+c+d = 0; 

 

InputThe first line of the input file contains the size of the lists n(1<= n <= 4000)
Then every line containing four integer values (with absolute valueas large as 2^28 ) OutputFor each input file, your program has to write the number whose sumis zero 

 

Sample Input6 -45 22 42 -16 -41 -27 56 30 -36 53 -37 77 -36 30 -75 -46 26 -38-10 62 -32 -54 -6 45 Sample Output5   
#include <cstdio>#include <iostream>#include <algorithm>using namespace std;const int maxn=4010;int n;int s1[maxn*maxn];int s2[maxn*maxn];int a[maxn],b[maxn],c[maxn],d[maxn];int main(){//   freopen("in.txt", "r", stdin);    while(~scanf("%d", &n))    {for (int i=0;i<n;i++)        scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);    for (int i=0;i<n;i++)        for (int j=0;j<n;j++)            s1[i*n+j]=a[i]+b[j];    for (int i=0;i<n;i++)        for (int j=0;j<n;j++)            s2[i*n+j] = c[i]+d[j];    sort(s1,s1+n*n);    sort(s2,s2+n*n);    int r = n*n-1;    int ans =0;    for (int i=0;i<n*n;i++)    {        while(r>=0 && s1[i]+s2[r]>0)            r--;        if (r < 0)break;        int tmp = r;        while (tmp >= 0 && s1[i]+s2[tmp] == 0)            ans++, tmp--;    }    printf("%d\n", ans);    }    return 0;}

 

【A + B + C + D】 問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.