View Code of Problem 5

#include <stdio.h>
#include <stdlib.h>

struct MyStruct
{
	int a, b, i;
}s[100005];

int compare(const void *a, const void *b)
{
	MyStruct *a1 = (MyStruct *)a;
	MyStruct *b1 = (MyStruct *)b;
	if (a1->a > b1->a)
	{
		return -1;
	}
	else if (a1->a < b1->a)
	{
		return 1;
	}
	else
	{
		if (a1->b < b1->b)
			return 1;
		else if (a1->b > b1->b)
			return -1;
		else return b1->i - a1->i;
	}
}

int main()
{
	int t, n, m;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d %d", &n, &m);
		for (int i = 0; i < n; i++)
		{
			scanf("%d%d", &s[i].a, &s[i].b);
			s[i].i = i;
		}
		qsort(s, n, sizeof(MyStruct), compare);
		for (int i = 0; i < m; i++)
			printf("%d ", s[i].i + 1);
		printf("\n");
	}
}

Double click to view unformatted code.


Back to problem 5