> it should take an integer (n) and then do the following n times
> ...
> 3) delete the max and min integer that have been used in step 2.
Each time, we are removing two integers from the integers to be considered.
So after performing step 3) n/2 times, there will be no integers left.
> how can it be done without priority queue?
By using a sorted
std::vector<>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
// read an integer k
unsigned int k ;
std::cout << "k? " ;
// if we have been able to read k and k is at least 2
if( std::cin >> k && k > 1 )
{
// then read k integers
std::cout << "enter " << k << " integers\n" ;
// create a vector to hold these k integers
std::vector<int> integers ;
// read k integers one by one into the vector
int i ;
while( integers.size() < k && std::cin >> i ) integers.push_back(i) ;
if( integers.size() == k ) // if we have read k integers
{
// 2) take the max integer and the min integer and add the difference
// between them to the answer.
// 3) delete the max and min integer that have been used in step 2.
int answer = 0 ;
// rearrange the integers in ascending order
std::sort( integers.begin(), integers.end() ) ;
int pos_min = 0 ; // position of min integer
int pos_max = integers.size() - 1 ; // position of max integer
while( pos_min < pos_max ) // as long as there are at least two integers left
{
// add the difference between them to the answer.
answer += integers[pos_max] - integers[pos_min] ;
// delete the max and min integer that have been used in step 2
++pos_min ; // the new min integer is the next integer after the old min
--pos_max ; // the new max integer is the previous integer after the old max
}
// print out the answer
std::cout << "answer: " << answer << '\n' ;
}
}
}
|
> my idea was to make two priority queues and when i read an integer i add it to first priority queue
> and add it to second priority queue as a negative number.
> so that when I take the top element of first priority queue that would be the max
> and from the second that would be the min.
Yes, it is a good (inventive) idea.
Something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#include <iostream>
#include <queue>
#include <functional>
int main()
{
// read an integer k
unsigned int k ;
std::cout << "k? " ;
// if we have been able to read k and k is at least 2
if( std::cin >> k && k > 1 )
{
// then read k integers
std::cout << "enter " << k << " integers\n" ;
// create a pair of priority_queues to hold these k integers
std::priority_queue<int> pos_q ;
std::priority_queue<int> neg_q ; // holds negetive of the numbers
// read k integers one by one into the priority queues
int i ;
while( pos_q.size() < k && std::cin >> i )
{
pos_q.push(i) ;
neg_q.push( -i ) ;
}
if( pos_q.size() == k ) // if we have read k integers
{
// 2) take the max integer and the min integer and add the difference
// between them to the answer.
// 3) delete the max and min integer that have been used in step 2.
int answer = 0 ;
// take the half the number of integers from each priority queue
while( pos_q.size() > k/2 ) // as long as there are integers left
{
// add the difference between them to the answer.
answer += pos_q.top() + neg_q.top() ; // yes, as in your code +
// delete the max and min integer that have been used in step 2
pos_q.pop() ;
neg_q.pop() ;
}
// print out the answer
std::cout << "answer: " << answer << '\n' ;
}
}
}
|