Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

排序之后扫描那里太复杂了,要简化一下

Posted by frkstyc at 2006-03-25 12:49:39 on Problem 2785
In Reply To:TLE~~~大牛请指教一下#_# Posted by:bluetiger at 2006-03-25 12:47:02
可以取定n1作参考,然后在n2里面找相反数

> #include<stdio.h>
> #include<math.h>
> #include<algorithm>
> using namespace std;
> int a[4001],b[4001],c[4001],d[4001],n1[16000001],n2[16000001];
> int num,n;
> __int64 cnt;
> int main()
> {
>     int i,j,p1,p2,c1,c2,pt1,pt2;
>     scanf("%d",&n);
>     for(i=0;i<n;i++)scanf("%d %d %d %d",&a[i],&b[i],&c[i],&d[i]);
>     num=0;
>     for(i=0;i<n;i++)
>         for(j=0;j<n;j++)
>             n1[num]=a[i]+b[j],n2[num]=c[i]+d[j],num++;
>     sort(n1,n1+num);
>     sort(n2,n2+num);
>     p1=0;p2=num-1;cnt=0;
>     while(p1<num&&p2>=0)
>     {
>         if(n1[p1]+n2[p2]==0)
>         {
>             c1=c2=1;
>             pt1=p1;pt2=p2;
>             while(p1+1<num&&n1[pt1]==n1[++p1])c1++;
>             while(p2-1>=0&&n2[pt2]==n2[--p2])c2++;
>             cnt+=c1*c2;
>         }    
>         else if(n1[p1]<=0&&n2[p2]>=0)
>         {
>             if(abs(n1[p1])<abs(n2[p2]))p2--;
>             else p1++;
>         }    
>         else if(n1[p1]>=0&&n2[p2]<=0)
>         {
>             if(abs(n1[p1])<abs(n2[p2]))p1++;
>             else p2--;
>         }    
>         else if(n1[p1]<=0&&n2[p2]<=0)p1++;
>         else if(n1[p1]>=0&&n2[p2]>=0)p2--;
>     }     
>     printf("%I64d\n",cnt);
>     return 0;
> }    

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator