slow_hare's blog

By slow_hare, history, 3 years ago, In English

I had a solution to a problem which included priority queue.

I wish to know if we can change the comparator of priority queue at run time like if we initially had the declaration as priority_queue<int> pq which will store no.s in non-increasing order, can I use the same priority queue to store no.s in increasing order after pq.clear()

Thanks

Full text and comments »

  • Vote: I like it
  • +12
  • Vote: I do not like it

By slow_hare, history, 4 years ago, In English

Could anyone please explain how these expressions are evaluated?

#include <bits/stdc++.h>
using namespace std;

int main()
{
  int a = 1;
  a = a++ + a++;
  cout<<a<<endl;
  a = 1;
  a = ++a + ++a;
  cout<<a<<endl;
  a = 1;
  a = a++ + ++a;
  cout<<a<<endl;
  a = 1;
  a = ++a + a++;
  cout<<a<<endl;
  
  return 0;
}

OUTPUT:

3

6

4

5

Full text and comments »

  • Vote: I like it
  • +16
  • Vote: I do not like it

By slow_hare, history, 4 years ago, In English
void del(node* &head, int val)
{
    if (head == NULL) {
        cout << "Element not present in the list\n";
        return;
    }
    if (head->info == val) {
        node* t = head;
        head = head->link;
        delete (t);
        return;
    }
    del(head->link, val);
}

Intuition:

  • We are passing node* (node pointer) as a reference to the 'del' function (as in node* &head).

  • Suppose node containing 'val' is the first node, head = head->next changes the actual head in the main function which now points to the current beginning of the list (which was second node before deletion).

  • Deleting any other node, now since current node* is derived from previous node's next and previous node's next is passed by reference , thus we can say that the pointer being on the current node can alter previous node's next.

  • Thus when we do head = head->next means previous node's next gets changed to head->next which is the required operation while deletion of a node in the linked list.

  • Thus there is no need to have a separate case for deletion of first node or last node.

Full text and comments »

  • Vote: I like it
  • -28
  • Vote: I do not like it

By slow_hare, history, 4 years ago, In English

Problem Link : Here

How to approach this problem?

I got the logic to solve as:

  • The no.s in the range >60 and <=n(given) which are not special(See the problem statement) has to be prime.
  • So the ans would be like n — (count of primes in range [61,n])
  • Would seive work Here?(as constraints are high)

I came to know about the prime no. theorem which gives the approx count of prime no.s less than x [as pi(x) = x/ln(x) or pi(x) = x/(ln(x)-1)]

Every bit of help would be appreciated :)

Thank you so much! for giving a look to this blog.

Full text and comments »

  • Vote: I like it
  • +11
  • Vote: I do not like it