Algorithm to calculate number of intersecting discs

前端 未结 30 1569
鱼传尺愫
鱼传尺愫 2020-12-12 10:57

Given an array A of N integers we draw N discs in a 2D plane, such that i-th disc has center in (0,i) and a radius

30条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-12 11:42

    #include 
    #include 
    void sortPairs(int bounds[], int len){
        int i,j, temp;
        for(i=0;i<(len-1);i++){
            for(j=i+1;j bounds[j]){
                    temp = bounds[i];
                    bounds[i] = bounds[j];
                    bounds[j] = temp;
                    temp = bounds[i+len];
                    bounds[i+len] = bounds[j+len];
                    bounds[j+len] = temp;
                }
            }
        }
    }
    int adjacentPointPairsCount(int a[], int len){
        int count=0,i,j;
        int *bounds;
        if(len<2) {
            goto toend;
        }
        bounds = malloc(sizeof(int)*len *2);
        for(i=0; i< len; i++){
            bounds[i] = i-a[i];
            bounds[i+len] = i+a[i];
        }
        sortPairs(bounds, len);
        for(i=0;i100000){
                    count=-1;
                    goto toend;
                }
                count++;
            }     
        }
    toend:
        free(bounds);
        return count;   
    }
    

提交回复
热议问题