View Code of Problem 5

#include <iostream>
#include <vector>
using namespace std;

struct fruit {
	int apple;
	int pear;
	int pos;
};

int main() {
	int t,n,m,a,p;
	vector<fruit> num;
	cin >> t;
	while (t--) {
		int pos = 1;
		cin >> n >> m;
		while (n--) {
			cin >> a >> p;
			num.push_back(fruit{ a, p,pos });
			pos++;
		}
		while (m--) {
			int max = -1;
			int max_pos = 0;
			for (int i = 0;i < num.size();i++){
				if (num[i].apple > max) {
					max = num[i].apple;
					max_pos = num[i].pos;
				}
				else if (num[i].apple == max) {
					if (num[i].pear > num[max_pos-1].pear) {
						max = num[i].apple;
						max_pos = num[i].pos;
					}
				}
			}
			num[max_pos - 1].apple = -1;
			num[max_pos - 1].pear = -1;
			cout << max_pos ;
			if (m == 0)
				cout << endl;
			else
				cout << " ";
		}
		
		num.clear();
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 5