码迷,mamicode.com
首页 > 其他好文 > 详细

51nod 1267 4个数和为0

时间:2018-09-02 02:05:10      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:def   spl   lin   continue   sed   bre   point   long   fine   

技术分享图片

【题解】

  先把数字两两组合,问题转化为求a+b=0. 这个问题显然可以排序后two pointer做。需要注意选出来的两个数必须由4个不同位置的数组成,不能重复。

技术分享图片
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define LL long long
 5 #define rg register
 6 #define N 1000010
 7 using namespace std;
 8 int n,tot,a[N],b[N],pos[N][2];
 9 inline int read(){
10     int k=0,f=1; char c=getchar();
11     while(c<0||c>9)c==-&&(f=-1),c=getchar();
12     while(0<=c&&c<=9)k=k*10+c-0,c=getchar();
13     return k*f;
14 }
15 int main(){
16     n=read();
17     for(rg int i=1;i<=n;i++) b[i]=read();
18     for(rg int i=1;i<n;i++)
19         for(rg int j=i+1;j<=n;j++)
20         a[++tot]=b[i]+b[j],pos[tot][0]=i,pos[tot][1]=j;
21     sort(a+1,a+1+tot);
22     int l=1,r=tot;
23     while(l<r){
24         while(a[l]+a[r]>0) r--;
25         if(a[l]+a[r]==0){
26             for(rg int i=r;i>=l;i--)if(a[l]+a[r]==0){
27                 if(pos[l][0]==pos[r][0]||pos[l][1]==pos[r][0]||pos[l][0]==pos[r][1]||pos[l][0]==pos[r][1]) continue;
28                 puts("YES");
29                 return 0;
30             }
31             else break;
32         }
33         l++;
34     }
35     puts("NO");
36     return 0;
37 }
View Code

 

51nod 1267 4个数和为0

标签:def   spl   lin   continue   sed   bre   point   long   fine   

原文地址:https://www.cnblogs.com/DriverLao/p/9571946.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!