Given n
points in a two-dimensional space, sort all the points in ascending order.
(x1,y1) > (x2,y2) if and only if (x1>x2) or (x1==x2 && y1<y2)
Input specification:
The first line consists of an integer t
, the number of test cases. Then for each test case, the first line consists of an integer n
, the number of points. Then the next n
lines contain two integers xi
, yi
which represents the point.
Output Specification:
For each test case print the sorted order of the points. Input constraints:
1 <= t <= 10
1 <= n <= 100000
- 10 ^ 9 <= co - ordinates <= 10 ^ 9
NOTE: Strict time limit. Prefer scanf
/printf
/BufferedReader
instead of cin
/cout
/Scanner
.
Sample Input:
1
5
3 4
-1 2
5 -3
3 3
-1 -2
Sample Output:
-1 2
-1 -2
3 4
3 3
5 -3
I declared a set
, now I want to sort descendingly(values) if the keys are equal. Here is my code:
int main()
{
int n, i, hold = 0;
set<pair<int, int>>s;
int x, y, t;
set<pair<int, int>>::iterator it;
SF(t)
while (t--)
{
SF(n) while (n--) {
SF(x) SF(y)
s.insert({ x,y });
}
for (it = s.begin(); it != s.end(); it++) {
PF(it->first) printf(" "); PF(it->second); printf("\n");
}
s.clear();
}
return 0;
}
my output
-1 -2
-1 2
3 3
3 4
5 -3
I want the key values to be sorted descendingly if the keys are same.
Set doesn't sort the way you want by default, so you have to supply your own comparison function.
struct MyComp
{
bool operator()(const pair<int,int>& x, const pair<int,int>& y) const
{
return x.first < y.first || (x.first == y.first && x.second > y.second);
}
};
set<pair<int,int>, MyComp> s;