• POJ_2758 (二分)


     
    Description
     
    The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, b, c, d ) ∈ A x B x C x D are such that a + b + c + d = 0 . In the following, we assume that all lists have the same size n .
     
    Input
    The first line of the input file contains the size of the lists n (this value can be as large as 4000). We then have n lines containing four integer values (with absolute value as large as 2 28 ) that belong respectively to A, B, C and D .
     
    Output
    For each input file, your program has to write the number quadruplets whose sum is zero.
     
    Sample Input
    6
    -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 Output
    5
    Hint
    Sample Explanation: Indeed, the sum of the five following quadruplets is zero: (-45, -27, 42, 30), (26, 30, -10, -46), (-32, 22, 56, -46),(-32, 30, -75, 77), (-32, -54, 56, 30).

    题意为从给出的四列数中,每列选出一个数,可以重复选,问有多少组这四个数相加结果等于0。

    每列的个数最多四千 ,暴力4000^4,一定超时的。我们中的巨巨想到一个办法。先将数组预处理下两个两个合并,前两个合并,后两个合并。这样新生成的两个数组,第一个为前两列各个数字的和,第二个为后两列各个数字的和。这样的复杂度为O(n^2),16000000,这样不会超时,接下来遍历其中一个数组,二分另外一个数组,如果遍历的那个数组元素的相反数等于,在另外的一个数组中二分找到了,ans++;

    这个思路是很厉害的,可是交上去一直wa,我们各种改还是不对,比赛结束后才想到,万一二分的那个数组中要查找的数出现了多次怎么办!漏洞就在这,把查找到的那个数在向两边搜下看有没有相等的就好了,注意break;我在这超时了两发~~~~~

    #include<cstdio>
    #include<algorithm>
    using namespace std;
    const int maxn = 4005;
    const int maxnn = 16000000+25;
    int a[maxn],b[maxn],c[maxn],d[maxn],ab[maxnn],cd[maxnn];
    int n,m,k;
    
    int s_division(int x){
    	int l,r,mid,ans;
    	l = 0; r = m-1; mid = ans = 0;
    	while(l<=r){
    		mid=(l+r)>>1;
    		if(cd[mid]==x){
    			ans++;
    			for(int j=mid+1;j<m;j++){
    				if(cd[mid]==cd[j])
    					ans++;
                                    else break;
    			}
    			for(int j=mid-1;j>=0;j--){
    				if(cd[mid]==cd[j])
    					ans++;
                                    else break;
    			}
    			return ans;
    		}
    		if(cd[mid]<x)
    			l=mid+1;
    		if(cd[mid]>x)
    			r=mid-1;
    	}
    	return ans;	
    }
    
    int main(){
    	int n,sum;
    	while(scanf("%d",&n)==1){
    		sum = 0;k = m = 0;
    		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++){
    				ab[k++]=a[i]+b[j];
    			}
    		}
    		for(int i=0;i<n;i++){
    			for(int j=0;j<n;j++){
    				cd[m++]=c[i]+d[j];
    			}
    		}
    		sort(cd,cd+m);
    		for(int i=0;i<k;i++){
    			sum+=s_division(-1*ab[i]);
    		}
    		printf("%d
    ",sum);
    	}
    	return 0;
    }
    
  • 相关阅读:
    【python】一个文件内容写入另一个
    【Linux】批量修改权限
    【Git】git add git commit
    赌博游戏
    输出斐波那契数列前20项,每输出5个数换行
    Java线程的几种可用状态
    Java创建线程的方式
    Java虚拟机
    判断对象oStringObject是否为String
    throw跟throws关键字
  • 原文地址:https://www.cnblogs.com/gjy963478650/p/7305217.html
Copyright © 2020-2023  润新知