示例#1
0
float handleQuery(float prev,int next,MaxHeap& maxheap,MinHeap& minheap){

    if(next>prev){
        if(minheap.n>maxheap.n){
            maxheap.insert(minheap.extractMin());
        }

        minheap.insert(next);
    }else{
        if(maxheap.n>minheap.n){
            minheap.insert(maxheap.extractMax());
        }
        maxheap.insert(next);
    }

    if((maxheap.n+minheap.n)%2==0){


        float l = maxheap.a[0];
        float r = minheap.a[0];

        return (l+r)/2;

    }else{

        if(maxheap.n>minheap.n)
            return maxheap.a[0];

        return minheap.a[0];
    }


}
示例#2
0
文件: 1306.cpp 项目: caiminf/timus
int main()
{
	int n;
	cin >> n;
	MaxHeap mHeap;
	uint tmp;
	for (int i = 0; i < n; i++){
		cin >> tmp;
		if (i <= n / 2){
			mHeap.insert(tmp);
		}
		else{
			mHeap.insert(tmp);
			mHeap.extractMax();
		}
	}
	uint num1, num2;
	if (n % 2 != 0){
		cout << mHeap.extractMax() << ".0" << endl;
	}
	else{
		num1 = mHeap.extractMax();
		num2 = mHeap.extractMax();
		if ((num1 + num2) % 2 == 0){
			cout << (num1 + num2) / 2 << ".0" << endl;
		}
		else{
			cout << (num1 + num2) / 2 << ".5" << endl;
		}
	}
	return 0;
}
int MedianMaintenance(MaxHeap<int>& heapLow, MinHeap<int>& heapHigh, int elem)
{
	if(heapLow.size() == heapHigh.size())
	{
		if(heapLow.size())
		{
			if(elem > heapHigh.get_min())
			{
				heapHigh.insert(elem);
				heapLow.insert(heapHigh.extract_min());
			}
			else
				heapLow.insert(elem);
		}
		else
			heapLow.insert(elem);
	}
	else
	{
		if(elem < heapLow.get_max())
		{
			heapLow.insert(elem);
			heapHigh.insert(heapLow.extract_max());
		}
		else
			heapHigh.insert(elem);
	}

	return heapLow.get_max();
}
bool test_maxheap()
{
	MaxHeap<int> maxheap;
	int lim = 1000;
	for(int i = 0;i < lim; i++)
	{
		maxheap.insert(rand() % lim  + (rand() < (RAND_MAX/8)?-lim/10:lim));
	}
	int *max_sort = new int[maxheap.size()];
	int i = 0;
	while(maxheap.size())
	{
		max_sort[i++] = maxheap.remove_max();
	}
	i = 1;
	for(int i = 1; i < lim; i++)
	{
		if(max_sort[i] > max_sort[i-1])return false;
	}
	return true;
}
示例#5
0
int main(int argc, const char * argv[])
{
    PrefixTree tree;
    ScrabbleLookup lookup;
    string dictionaryFile;
    
    // Parse the command line argument by looping through all
    // of the command line arguments
    for (int i = 0; i < argc; i++)
    {
        if (strcmp(argv[i],"-d") == 0) dictionaryFile = argv[i+1];
    }

    // Open all dictionary file, and import all the words into the
    // trie.
    ifstream myfile;
    myfile.open(dictionaryFile.c_str());
    while (!myfile.eof())
    {
        string word;
        myfile >> word;
        tree.insert(word);
    }
    
    
    // Print tree information.
    cout << "Number of word in tree: " << tree.getNumWord() << endl;
    cout << "Number of nodes in tree: " << tree.getNumNodes() << endl;
    cout << "Maximum depth of the tree: " << tree.getMaxDepth() << endl;
    cout << "Number of bytes in tree: " << tree.getNumBytes() << endl << endl;
    
    // Continue looping the program until all of the words have been entered.
    string word = "";
    while (word != "!")
    {
        MaxHeap heap;
        
        // Prompt the user for the word to solve.
        cout << "Please enter a collection of letters (all lowercase) or questions marks to solve, or ! if you want to quit: ";
        cin >> word;
        
        if (word == "!") break;
        
        cout << endl << "Any restrictions? Please enter the position number of the letter first, and then the character.  An example is 2a for forcing the second letter to be a. Keep entering restrictions, and then enter ! when finished. ";
        string restric = "";
        while (restric != "!")
        {
            cin >> restric;
            if (restric == "!") break;
            RestrictionNode restrictNode;
            restrictNode.letter = restric[1];
            restrictNode.position = atoi(restric.substr(0,1).c_str());
            restrictions.push_back(restrictNode);
        }
    
        // Generate all of the combinations of the collection of letters, and store in a vector.
        for (int x = 0; x <= word.length(); x++)
            generate_combinations(word, x);
        
        // Change all of the question marks in the set to 26 different words per question mark
        convertAllQuestionMarks();
        
        // From the combinations, make permutations of all of the words
        for (int x = 0; x < combinations.size(); x++)
            generate_permutations(combinations[x]);
        
        
        // Iterate through all of the permutations, and include known words into the heap.
        for (set<string>::iterator it = permutation_set.begin();
             it != permutation_set.end(); it++) {
            if (tree.isStored(*it))
            {
                heap.insert(*it, lookup.pointValue(*it));
            }
        }
        
        cout << endl << "The following are the top Scrabble words: " << endl;
        
        // Output the words in order of the word score.
        for (int i = 0; i < WORDS_TO_OUTPUT; i++)
        {
            string word = heap.dequeueMax();
            int score = lookup.pointValue(word);
            if (word != "")
                cout << word << " " << score << endl;
        }
        
        cout << endl;
        
        combinations.clear();
        restrictions.clear();
        combinations_pre.clear();
        permutation_set.clear();
    }
}