zoukankan      html  css  js  c++  java
  • EOJ2530 数字之和

    http://acm.cs.ecnu.edu.cn/problem.php?problemid=2530

    题意:求一个数组中是否存在三个数的和为0。n=1000,朴素的n^3会超时,故用二分搜索,即是把正负数存入不同的两个数组,在一个数组中任取两个,求和,然后在另一个数组中利用二分查找其相反数是否存在, 复杂度将降为n^2logn 级别

     1 #include<map>
     2 #include<cmath>
     3 #include<cctype>
     4 #include<cstdio>
     5 #include<string>
     6 #include<cstdlib>
     7 #include<cstring>
     8 #include<iostream>
     9 #include<algorithm>
    10 using namespace std;
    11 bool b_search(int key,int *num,int start,int end){
    12     while(start<=end){
    13         int m=(start+end)/2;
    14         if(num[m]>key)
    15             end=m-1;
    16         else if(num[m]<key)
    17             start=m+1;
    18         else return true;
    19     }
    20     return false;
    21 }    
    22 int main(){
    23     int T;
    24     cin>>T;
    25     while(T--){
    26         int n,i=0,j=0;
    27         int pos[1005],neg[1005];
    28         cin>>n;
    29         while(n--){
    30             int x;
    31             scanf("%d",&x);
    32             if(x>=0)pos[i++]=x;           //存入不同数组
    33             else neg[j++]=x;
    34         }
    35         sort(pos,pos+i);
    36         sort(neg,neg+j);
    37         bool f=0;
    38         for(int k=0;k<i;k++)
    39             for(int h=k+1;h<i;h++){                //任取两个,然后二分查找
    40                 int temp=pos[k]+pos[h];
    41                 if(b_search(-1*temp,neg,0,j-1)){f=1;break;}
    42             }
    43         if(!f){
    44             for(int k=0;k<j;k++)
    45                 for(int h=k+1;h<j;h++){
    46                     int temp=neg[k]+neg[h];
    47                     if(b_search(-1*temp,pos,0,i-1)){f=1;break;}
    48                 }
    49         }
    50         if(f)cout<<"YES"<<endl;
    51         else cout<<"NO"<<endl;
    52     }
    53     return 0;
    54 }
    View Code

     我又来hash了。。。

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string>
     4 #include <algorithm>
     5 #include <string.h>
     6 #include <stdlib.h>
     7 
     8 using namespace std;
     9 short hash[300005];
    10 
    11 int main()
    12 {
    13     //freopen("testin.txt", "r", stdin);
    14     //freopen("testout.txt", "w", stdout);
    15     
    16     int t;
    17     cin >> t;
    18     while(t--)
    19     {
    20         int n, na, nb, ia=0, ib=0;
    21         int a[1005], b[1005];
    22         cin >> n;
    23         while(n--)
    24         {
    25             int x;
    26             scanf("%d", &x);
    27             if(x >= 0)
    28                 a[ia++] = x;
    29             else
    30                 b[ib++] = x;
    31         }
    32         na = ia, nb = ib;
    33         memset(hash, 0, sizeof(hash));
    34         for(int i=0; i<na-1; i++)
    35             for(int j=i+1; j<na; j++)
    36                 hash[a[i]+a[j]] ++;
    37         int isyes = 0;
    38         for(int i=0; i<nb; i++)
    39             if(hash[-1*b[i]])
    40             {
    41                 isyes = 1; 
    42                 break;
    43             }
    44         if(isyes)
    45             cout << "YES" << endl;
    46         else
    47             cout << "NO" << endl; 
    48     }
    49     
    50     
    51     return 0;
    52 }
    View Code
  • 相关阅读:
    hdu 4521 小明系列问题——小明序列(线段树 or DP)
    hdu 1115 Lifting the Stone
    hdu 5476 Explore Track of Point(2015上海网络赛)
    Codeforces 527C Glass Carving
    hdu 4414 Finding crosses
    LA 5135 Mining Your Own Business
    uva 11324 The Largest Clique
    hdu 4288 Coder
    PowerShell随笔3 ---别名
    PowerShell随笔2---初始命令
  • 原文地址:https://www.cnblogs.com/KimKyeYu/p/3132401.html
Copyright © 2011-2022 走看看