Esempio n. 1
0
ASet* BinSearch(ASet v, ASet *a, int size)
{
	if (size==0)
		return 0;
	else if (v < a[size/2])
		return BinSearch( v, a, size/2);
	else if (v > a[size/2])
		return BinSearch( v, a+ size/2+1, size - size/2 -1);
	else
		return a + size/2;

}
Esempio n. 2
0
int main() {
	int iA, iB, iN, iIndex1, iIndex2;

	for (iN = 0; iN * iN < LIM; iN++)
		arrSquares[iN] = iN * iN;
	iN--;

	while (scanf("%d %d", &iA, &iB), iA || iB) {
		iIndex1 = BinSearch(iA, 0, iN);
		iIndex2 = BinSearch(iB, 0, iN);
		printf("%d\n", iIndex2 - iIndex1 + (arrSquares[iIndex2] == iB ? 1 : 0));
	}

	return 0;
}
Esempio n. 3
0
int main()
{
    int count = 0;
    int value = 0;

    SeqList* list = malloc(sizeof(SeqList) * MAX_COUNT);

    while(count < MAX_COUNT && scanf("%d", &value) != EOF)
    {
        list[count++].key = value;
    }

    qsort(list, count, sizeof(SeqList), comp);

    while(1)
    {
        int s = 0;
        printf("search for: ");
        scanf("%d", &s);
        int ret = BinSearch(list, count, s);
        printf("result : %d \n", ret);
    }

    return 0;
}
Esempio n. 4
0
void AddDontCare(ADNFClause dst, ADNFClause src)
{
  int dontCaresSize= DNFSize(src);
  ASet * dontCares= malloc(dontCaresSize);
  AAndClause *curDontCare;
  
  dontCaresSize=0;
  for (src=src->next; src; src= src->next)
  {
  	curDontCare= AddClause(dst, src->set); //add the and clause and keep a record of it
  	
//  	if (curDontCare && curDontCare->set== src->set) //if it is really added and not yet proven useful
  	//if the set is different than what was originally added, means it has simplified.
//  	{
 		dontCares[dontCaresSize]= src->set;
 		dontCaresSize++;
//  	}
  }
  
  QuickSort(dontCares, dontCaresSize);
  
  //Delete the ones who didn't make a difference
  for (dst= dst->next; dst; dst=dst->next)
  	if (BinSearch(dst->set, dontCares, dontCaresSize))
  		dst = RemoveClause(dst);	//go back to prev
}
Esempio n. 5
0
void SeekSum(int A[], int first, int end, int x) {
	int i;
	int pos;
	
	for (i = first; i < end; i++) 
		if ((pos = BinSearch(A, i + 1, end, x - A[i])) != -1) {
			printf("first = %d,end = %d\n",i + 1, pos);
			break;
		}
}			
Esempio n. 6
0
bool RBTree<T>::remove(T key)
    {
    bool found = true;
    pTreeNode<pair<bool, T> >* foundNode = BinSearch(key, this->root);

	// if the tree is empty or the data is not found
    if (!(foundNode && foundNode->getData() == key))
        {
        found = false;
        }
    else // remove by successor
        {
        pTreeNode<pair<bool, T> >* heir = successor(foundNode);
        pTreeNode<pair<bool, T> >* child = NULL == heir->right ? heir->left : heir->right;
        pTreeNode<pair<bool, T> >* parent = heir->parent;
        bool childIsLeft = true;
        
        foundNode->injectData(heir->getData());
        
        // detach child from heir (if child exists)
        if (NULL != child) // at least 1 child
            {
            child->parent = parent;
            }
        if (NULL == parent) // if heir is the root
            {
            this->root = child;
            if (NULL != this->root)
                {
                isBlack(this->root) = true;
                }
            }
        else
            {
            childIsLeft = heir == parent->left;
            if (true == childIsLeft)
                {
                parent->left = child;
                }
            else
                {
                parent->right = child;
                }
            if (true == isBlack(heir))
                {
                // child could be null, so parent is passed in
                deleteFixUp(parent, childIsLeft);
                }
            }
            
        delete heir;
        }
        
    return found;
    }
Esempio n. 7
0
//Binary Search - return index of key in deck, or -1
int BinSearch(Card key, Card deck[], uint beginning, uint end)
{
  //Sanity Check
  if (end < beginning) return -1;

  //Do I only have one?
  if (end == beginning)
  {
    if(equal(key,deck[beginning])) return beginning;
    else return -1;
  }

  //Check middle item
  int middle = (end-beginning)/2 + beginning;
  if (equal(key,deck[middle])) return middle;

  //Not here.  Recurse to find it in one of my halves
  if(lessthan(key,deck[middle])) return BinSearch(key,deck,beginning,middle-1);
  return BinSearch(key,deck,middle+1,end);
}
Esempio n. 8
0
void Solve()
{
    int ans=0,i,r,k;
    for (i=1;i+ans<n;i++)
    {
        r=BinSearch(i,i+1,n);
        k=MaxRMQ(i,r);
        if (len[k]>len[i]) ans=TMax(ans,k-i);
    }
    if (ans==0) printf("-1\n");
    else printf("%d\n",ans);
}
Esempio n. 9
0
int main()
{
    int x,y,i,j,cnt,x2,y2;
    freopen("poj2002.txt","r",stdin);
    freopen("poj2002ans.txt","w",stdout);
    while (scanf("%d",&n) && n)
    {
        for (i=1;i<=n;i++) scanf("%d%d",&point[i].x,&point[i].y);
        QSort(1,n);
        cnt=0;
        for (i=1;i<=n;i++)
            for (j=i+1;j<=n;j++)
            {
       	        x=point[i].y-point[j].y+point[i].x;
       	        y=point[j].x-point[i].x+point[i].y;
       	        if (!BinSearch(x,y)) continue;
       	        x2=point[i].y-point[j].y+point[j].x;
       	        y2=point[j].x-point[i].x+point[j].y;
       	        if (BinSearch(x2,y2)) cnt++;
            }
        printf("%d\n",cnt/2);
    }
    return 0;
}
Esempio n. 10
0
int main()
{
    int i;
    freopen("poj3261.txt","r",stdin);
    freopen("poj3261ans.txt","w",stdout);
    while (scanf("%d%d",&n,&k)!=EOF)
    {
        for (i=0;i<n;i++) scanf("%d",&num[i]);
        num[n]=0;
        DA(num,sa,rank,n+1,10000);
        CalcHeight(num,sa,height,n);
        BinSearch(1,n);
        printf("%d\n",ans);
    }
}
Esempio n. 11
0
void main()
{
    KeyType a[] = {2, 4, 7, 9, 10, 14, 18, 26, 32, 40}, k = 7;
    LineList R[MaxSize];
    int n = 10, i;

    for (i = 0; i < n; i++)
        R[i].key = a[i];

    i = BinSearch(R, n, k);

    if (i >= 0)
        printf("R[%d].key=%d\n", i, k);
    else
        printf("%d不在a中\n", k);
}
Esempio n. 12
0
int ListBinSearch (list_t list, void *ptrToItem,
		   CompareFunction compareFunction)
{
	int index;

	index = BinSearch (ITEMPTR (list, 0),
			   (int) (*list)->numItems,
			   (int) (*list)->itemSize, ptrToItem,
			   compareFunction);

	if (index >= 0)
		index++;			/* lists start from 1 */
	else
		index = 0;			/* item not found */

	return index;
}
Esempio n. 13
0
static uint32_t ADC_to_Value(uint32_t adcValue)
{
	uint8_t index = 0;
	uint32_t referValue = 0;
	uint32_t Value = 0;
	uint8_t len = sizeof(refer_ADC_Value)/sizeof(uint32_t);
	
	if(adcValue <= refer_ADC_Value[0]){
		referValue = refer_Value[0];
	}else if(adcValue >= refer_ADC_Value[len-1]){
		referValue = refer_Value[len-1];
	}else{
		index = BinSearch(refer_ADC_Value,len,adcValue);
		referValue = ((2 * (refer_Value[index+1] - refer_Value[index]) * (adcValue - refer_ADC_Value[index])) / (refer_ADC_Value[index+1] - refer_ADC_Value[index])+1)/2 + refer_Value[index];
	}
	Value = ((2*adcValue*referValue/0xFFF)+1)/2;
	printf("index=%d,refer_ADC_Value=%d,refer_ADC_Value_1=%d\n",index,refer_ADC_Value[index],refer_ADC_Value[index+1]);
	printf("index=%d,refer_Value=%d,refer_Value_1=%d\n",index,refer_Value[index],refer_Value[index+1]);
    printf("adc=%d,referValue=%d,Value=%d\n\n",adcValue,referValue,Value);
	return Value;	
}
Esempio n. 14
0
int main(int argc, char const *argv[])
{
	int arr[] = {1,2,3,4,5,7,9};
	int res;
	int len;
	len = sizeof(arr)/sizeof(arr[0]);
	res = SeqSearch(arr,len,5);
	printf("-----SeqSearch----\n");
	if (0 == res)
	{
		printf("Not find\n");
	}
	else
		printf("Find, location:%d\n", res+1);
	printf("-----BinSearch-----\n");
	res = BinSearch(arr,len,3);
	if (0 == res)
	{
		printf("Not find\n");
	}
	else
		printf("Find, location:%d\n", res+1);
	return 0;
}
Esempio n. 15
0
bool RBTree<T>::insert(T key)
    {
    bool unique = true;
    bool childIsLeft = false;
    
    // find primary insertion place:
    //      NULL -> empty tree,
    //      data = key -> duplicate,
    //      data > key -> left is NULL -> child is left,
    //      data < key -> right is NULL -> child is right,
    pTreeNode<pair<bool, T> >* topNode = BinSearch(key, this->root);
    
    // Insert to empty tree
    if (NULL == topNode)
        {
        this->root = new pTreeNode<pair<bool, T> >(pair<bool, T>(true, key));
        }
    else if (topNode->getData() == key)
        {
        topNode->injectData(key);
        unique = false;
        }
    else
        {
        // this Node is red and has parent set to insertAfter
        pTreeNode<pair<bool, T> >* child = new pTreeNode<pair<bool, T> >(pair<bool, T>(false, key));
        bool parentIsLeft = false;
        child->parent = topNode;
        
        childIsLeft = topNode->getData() > key;
        if (true == childIsLeft)
            {
            topNode->left = child;
            }
        else
            {
            topNode->right = child;
            }
        
        // maintain RedBlack properties
        // note: grandparent is black in loop, since parent is red
        while (NULL != child->parent && false == isBlack(child->parent))
            {
            // due to iteration, topNode is not yet
            // guaranteed to be child's parent
            parentIsLeft = child->parent == child->parent->parent->left;
            // make topNode the uncle of the child
            topNode = parentIsLeft ? child->parent->parent->right : child->parent->parent->left;
            
            // case 1: uncle is red
            if (NULL != topNode && false == isBlack(topNode))
                {
                isBlack(child->parent) = isBlack(topNode) = true;
                isBlack(topNode->parent) = false;
                // Move up to the grandparent
                child = topNode->parent;
                childIsLeft = (this->root != child && child->parent->left == child);
                }
            else // uncle is a black Node or nil
                {
                // case 2: parent and child orientation are not aligned
                // rotate parent, stay at the same level (the parent becometh the child)
                if (childIsLeft != parentIsLeft)
                    {
                    child = child->parent;
                    rotate(child, parentIsLeft);
                    }
                // case 3: orientations are aligned
                // parent must be black (in order to take over grandparent's position)
                // grandparent change to red
                isBlack(child->parent) = true;
                isBlack(child->parent->parent) = false;
                rotate(child->parent->parent, !parentIsLeft); // parent moves up
                }
            }
        // property 1: root is black
        isBlack(this->root) = true;
        }
    
    return unique;
    }