zoukankan      html  css  js  c++  java
  • 51nod 1267 4个数和为0

    【题解】

      先把数字两两组合,问题转化为求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
  • 相关阅读:
    轮播制作
    前端问题总结
    响应式 媒体查询 盒模型
    响应式 字体设置 flex 弹性布局
    C++ STL之set常用指令
    SRM468
    SRM470
    置换及其应用专题
    C++ STL之map常用指令
    C++ STL之pair常用指令
  • 原文地址:https://www.cnblogs.com/DriverLao/p/9571946.html
Copyright © 2011-2022 走看看