osource's blog

By osource, history, 3 years ago, In English

Hello everyone!

While solving this problem, I defined my own comparator function for sorting a vector of coordinates (represented as struct). However I got runtime error upon using the comparator function like this —

struct Point
{
    int x,y;
};
int main()
{
    vector<Point> v;
    sort(v.begin(),v.end(), [&](Point &p1, Point &p2){
            if(p1.x!=p2.x)
                return p1.x<=p2.x;
            return p1.y<=p2.y;
        });
}

and also like this —

struct Point
{
    int x,y;
};
int main()
{
    vector<Point> v;
    sort(v.begin(),v.end(), [&](Point &p1, Point &p2){
            if(p1.x!=p2.x)
                return p1.x<p2.x;
            return p1.y<=p2.y;
        });
}

But I got AC when I used the comparator like this —

struct Point
{
    int x,y;
};
int main()
{
    vector<Point> v;
    sort(v.begin(),v.end(), [&](Point &p1, Point &p2){
            if(p1.x!=p2.x)
                return p1.x<=p2.x;
            return p1.y<p2.y;
        });
}

Can anyone explain why the first 2 comparator definitions gave RE but the third one gave AC?

Submission 1 (RE) — Submission 1

Submission 2 (RE) — Submission 2

Submission 3 (AC) — Submission 3

Full text and comments »

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

By osource, history, 3 years ago, In English

let's say I have 2 numbers x and y. How do I check if x*y results in an overflow in C++?. Both x and y are long long int.

Full text and comments »

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

By osource, history, 3 years ago, In English

Hello,

I was solving CSES problemset and am stuck in this question — Question My code — Code

I am getting TLE on 4 test cases. I'm not able to figure out why as I have used standard BFS and therefore the time complexity should be O(m*n). Can someone help me in this?

Full text and comments »

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

By osource, history, 3 years ago, In English

You have 2 arrays A and B each of size n, that contains integers from 1 to n inclusive. In one move, you can swap corresponding elements of two arrays. Determine if it is possible to have such series of operations (possibly zero) that both arrays contain integers from 1 to n exactly once, if possible print the series of operations (ie indices on which you apply the operations) and -1 if not possible.

eg

A = 3 1 2 1 2

B = 5 4 4 5 3

ans = 3 4

explanation :

after operations

A = 3 1 4 5 2

B = 5 4 2 1 3

Can someone tell me the approach/pseudocode for this?

Full text and comments »

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