Uva1152 4 Values whose Sum is 0 【中途相遇+二分】【例題8-3】

題目:4 Values whose Sum is 0

題意:給定4n1≤n≤4000)元素集合A,B,C,D,要求分別從中選取一個元素a,b,c,d,使得a+b+c+d=0。 問:有多少種選法?

思路1:按照紫書的思路中途相遇,就是先將a+b的所有值算出排序(二分需要),然後再枚舉 -(c+d) 的所有值在a+b的數組中二分查找,如果查找到就計數。

但是還有一種情況,就是a+b的數組中有相同的值,例如以下數據:


0 1 0 -1
1 0 -1 0

這組的話a+b:{0,1,1,2} ,當-(c+d)的值算出-1時是需要和a+b中的倆個1都可以組合,所有有倆個,所有要將相同的情況都算出。

我的思路是利用二分查找到位置後,再從本位置左右掃描一遍,如果有相等的都累計。

參考:紫書-例8-3-P237

代碼:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 4000 + 5;
const int col = 4;
LL arr[maxn][col],before[maxn*maxn];
int n,cnt;
inline void pretreat(){//預處理將a+b放入數組中後遞增排序
    cnt = 0;
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            before[cnt++] = arr[i][0] + arr[j][1];
    sort(before,before+cnt);
}
LL binarySearch(LL left,LL right,LL target){
    while(left < right){
        int pos = left + (right - left)/2;
        if(target == before[pos])return pos;
        else if(target < before[pos]) right = pos;
        else left = pos + 1;
    }
    return -123456;
}
inline int LRSearch(int pos,int value){//尋找當數字的相同數字
    int cot = 0;
    for(int i=pos-1;i>=0;i--)  if(before[i] == value) cot++;else break;
    for(int i=pos+1;i<cnt;i++) if(before[i] == value) cot++;else break;
    return cot;
}
inline void operate(){
    int ansCnt = 0;
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++){
            LL s = arr[i][2] + arr[j][3];//計算c+d
            int index = binarySearch(0,cnt,-s);//在a+b中二分查找-(c+d)返回下標
            if(index != -123456){//找到
                ansCnt++;//累計
                ansCnt += LRSearch(index,-s);//加上相同的
            }
        }
    printf("%d\n",ansCnt);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            for(int j=0;j<col;j++) scanf("%lld",&arr[i][j]);
        pretreat();
        operate();
        if(t) printf("\n");
    }
    return 0;
}

思路2:看了紫書代碼庫後,才知道用二分的上下界求出數組中有多少個相同的值即可:上界位置 - 下界位置 = 相同的個數。

參考:紫書代碼庫

代碼:

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 4000 + 5;
const int col = 4;
LL arr[maxn][col],before[maxn*maxn];
int n,cnt;
inline void pretreat(){//預處理將a+b放入數組中後遞增排序
    cnt = 0;
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            before[cnt++] = arr[i][0] + arr[j][1];
    sort(before,before+cnt);
}
inline void operat(){
    LL ans = 0,value;
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++){
            value = -(arr[i][2] + arr[j][3]);
            ans += upper_bound(before,before+cnt,value) - lower_bound(before,before+cnt,value);//將-(c+d)的上下界求出後上界-下界即得出當前數出現幾次了。
        }
    printf("%lld\n",ans);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            for(int j=0;j<col;j++) scanf("%lld",&arr[i][j]);
        pretreat();
        operat();
        if(t) printf("\n");
    }
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章