HackerEarth队列数据结构

时间:2019-06-15 18:27:39

标签: c++ vector data-structures queue time-complexity

有N个蜘蛛队列,其中只有X个蜘蛛被选择。每个蜘蛛都有一些与之相关的力量。队列中有X次迭代。 在每次迭代中,将X个Spider出队(如果队列中的条目少于X个,则将其全部出队),并选择功率最大的蜘蛛,然后将其余的Spider入队(以出队的顺序),但是它们的功率降低一单位。如果那些出队的蜘蛛中有多个蜘蛛具有最大的威力,则选择队列中最先出现的蜘蛛。如果任何时刻的任何蜘蛛的力量变为0,就无法进一步递减,它保持不变。

我试图将队列实现为向量,因为它更容易迭代。我正在使用两个队列。一个队列将具有用户输入的原始值,该值不会被更改,另一个队列将用于推式弹出操作以及获得最大结果,一旦我从中获得最大结果在执行操作的队列中,我将搜索原始值中的最大值,并且在每次迭代中都会减少循环。

#include "iostream"
#include "vector"
#include "algorithm"
using namespace std;
int Res(vector<int> vec, int num)
{
vector<int>::iterator itr;
for(itr=vec.begin();itr!=vec.end();++itr)
{
    if (*itr == num)
    {
        return distance(vec.begin(), itr)+1;
    }
}
return -1;
}
//Recursive Function
void result(vector<int> queue, vector<int> queueops, int iteration,int 
origcount)
{
int itrcount = origcount - iteration;
int index = 0;
int ele = 0;
bool AllZero = false;
if (iteration > 0)
{       //Finding the max element in the vector and taking the value a 
            //nd its index
    int max = *queueops.begin();
    for (size_t i = 0; i < iteration; ++i)
    {
        if (max <= queueops[i])
        {
            max = queueops[i];
            index = i;
        }
    }
            //If all elements are 0, then will be taking the first index 
            //as the max element
    for (size_t z = 0; z < iteration; ++z)
    {
        if (0 == queueops[z])
        {
            AllZero = true;
        }
        else
        {
            AllZero = false;
        }
    }
    if (AllZero == true)
    {
        max = *queueops.begin();
        index = 0;
    }
    for (size_t i = 0; i < iteration; ++i)
    {
        ele = *queueops.begin();
        queueops.erase(queueops.begin());
        if (i != index)
        {
            queueops.push_back(ele<=0 ? 0: ele-1);
        }
    } 
            //Doing a linear search since binary search is not possible, 
            //if the elements are sorted, the data wouldn't be correct.
    cout << Res(queue,max+itrcount)<< endl;
    result(queue, queueops,queueops.size(),origcount);
}
}
int main()
{
vector<int> input;
vector<int>output;
int n, y;
int& ocount = y;
cin >> n;
cin >> y;
    //Pushing all the elements in the vector given by the user
for (int i = 0; i < n; i++)
{
    int x;
    cin >> x;
    input.push_back(x);
}
output = input;
result(input, output, y,ocount);
return 0;
}

Expected Input
6 5
1 2 2 3 4 5
Expected Output
5 6 4 1 2 

The initial queue: 1,2,2,3,4,5
In first iteration, starting 5 entries are removed from the queue.
The removed entries are:[1,2,2,3,4].
The queue now becomes: [5] The maximum power spider is present at index 5 
in the given initial queue. So, we print 5 and power of remaining spiders 
in the removed ones is decremented by 1 unit and enqueued back to the 
 queue.
The queue now becomes: [5,0,1,1,2]

 In the second iteration, we print index 6 as element 5 is present at 
 index 6 in the initial queue.
 After the second iteration, the queue becomes: [0,0,0,1]
 Note that the power of spider with power 0 after the first iteration 
 still remains 0.
 The next output is 4 because after the second iteration [0,0,0,1] 1 is 
 the max element and it was initially 3 and in iteration it got 
 decremented, 3 initial queue is at index 4,so the answer is 4.

我的答案是不正确的,因为我要根据正在执行操作的队列的大小与要执行的初始迭代次数之间的差来设置itrcount的值。

0 个答案:

没有答案