#include using namespace std; struct Animal { char t; int s; int d; }; vector animals; vector subset; vector combo; bool compareByDest(const Animal &a, const Animal &b) { return a.d < b.d; } void sortByIncrDest() { sort(animals.begin(), animals.end(), compareByDest); } bool areFriends(int a_idx, int b_idx) { string a_type = string(1, animals[a_idx].t); string b_type = string(1,animals[b_idx].t); string combinedType; if (b_type < a_type) { combinedType = (b_type + a_type); } else { combinedType = (a_type + b_type); } if (combinedType == "DE" || combinedType == "CD" || combinedType == "CM" || combinedType == "EM") return false; return true; } bool isOverlap(int a_idx, int b_idx) { int a_dest = animals[a_idx].d; int a_start = animals[a_idx].s; int b_dest = animals[b_idx].d; int b_start = animals[b_idx].s; if (a_start >= b_dest || b_start >= a_dest) return false; return true; } bool isCompatible(int a_idx, int b_idx) { if (isOverlap(a_idx, b_idx)) { if (!areFriends(a_idx, b_idx)) return false; } return true; } bool isValidCombo() { for (int i = 0; i < combo.size()-1; i++) { for (int j = i+1; j < combo.size(); j++) { if (!isCompatible(combo[i], combo[j])) return false; } } return true; } bool comboMachine(int offset, int k) { if (k == 0) { if (isValidCombo()) return true; else return false; } for (int i = offset; i <= subset.size()-k; ++i) { combo.push_back(subset[i]); if (comboMachine(i + 1, k - 1)) return true; combo.pop_back(); } return false; } bool currentBucketHasAnswer(int numAnimals, int limit) { subset.clear(); for (int i = 0; i <= limit; i++) { subset.push_back(i); } combo.clear(); return comboMachine(0, numAnimals); } int getMinZooNum(int numAnimals) { for (int j = numAnimals - 1; j < animals.size(); j++) { if (currentBucketHasAnswer(numAnimals, j)) return animals[j].d; } return -1; // No Answer Found } vector minimumZooNumbers(int m, int n, vector t, vector s, vector d) { // Return a list of length n consisting of the answers vector minZooNum; animals.clear(); // fill animals struct vector for (int j = 0; j < n; j++) { animals.push_back({t[j],s[j],d[j]}); } // sort animals struct vector in-place by ascending destination sortByIncrDest(); for (int i = 1; i <= n; i++) { minZooNum.push_back(getMinZooNum(i)); } return minZooNum; } int main() { int cases; cin >> cases; for(int a0 = 0; a0 < cases; a0++){ int m; int n; cin >> m >> n; vector t(n); for(int t_i = 0; t_i < n; t_i++){ cin >> t[t_i]; } vector s(n); for(int s_i = 0; s_i < n; s_i++){ cin >> s[s_i]; } vector d(n); for(int d_i = 0; d_i < n; d_i++){ cin >> d[d_i]; } vector result = minimumZooNumbers(m, n, t, s, d); for (ssize_t i = 0; i < result.size(); i++) { cout << result[i] << (i != result.size() - 1 ? " " : ""); } cout << endl; } return 0; }