C++ Deque -> d.push_back(d.front());

d.push_back(d.front());
d.pop_front();

why is this causing “SIGTERM” error ?

logic → I want to move the first element at the back of the deque.

complete program →

    #include <bits/stdc++.h>
    using namespace std;
    #define PLL pair<long long, long long>
    #define ll long long int

    int main()
    {
        ll n, m;
        cin >> n >> m;
     
        deque<PLL> d(n);
        ll i = 1;
        for (auto &x : d)
        {
            cin >> x.first;
            x.second = i;
            i++;
        }
        ll ans = 0;
        while(true)
        {
            if (d.size() == 1)
            {
                cout << d.front().second << endl;
                return 0;
            }
            if (d.front().first > m)
            {
                d.push_back(d.front());
                d.pop_front();
            }
            else
            {
                d.pop_front();
            }
        }
        return 0;
    }

problem → Problem - 450A - Codeforces

What test input are you using? For me, your program just goes into an infinite loop with the first set of test input.

Edit:

In the case where the child needs more than m candies, you are not doing this:

so that child never gets any candies, and the algorithm never terminates.

1 Like

5 2
1 3 1 4 2

Ohh yes I guess you are right it is running infinite times

got it. thank you.

1 Like