百度笔试第二题-9.3
#include <iostream> #include <algorithm> #include <vector> using namespace std; typedef pair<int, int> PAIR; bool cmp1(vector<int>& nums1, vector<int>& nums2) { if (nums1[1] < nums2[1]) return true; else return false; } bool cmp0(vector<int>& nums1, vector<int>& nums2) { if (nums1[0] < nums1[0]) return true; else return false; } void quicksort(vector<vector<int>>& nums, int index, int begin, int end) { if (end < begin) return; int front = begin, back = end; int pp = front; vector<int> num_pp = nums[pp]; while (front < back) { while (nums[back][index] > num_pp[index] && back > front) { back--; } nums[front] = nums[back]; while (nums[front][index] < num_pp[index] && back>front) { front++; } nums[back] = nums[front]; } nums[front] = num_pp; quicksort(nums, index, begin, front - 1); quicksort(nums, index, front + 1, end); } void Mysort(vector<vector<int>>& nums, int index) { int size = nums.size(); int front = 0, back = size - 1; quicksort(nums, index, front, back); } int main() { int t; cin >> t; while (t--) { int n, m; cin >> n >> m; vector<vector<int>> people; vector<vector<int>> character; vector<int> chosen(n,-1); int num; for (int i = 0; i < n; i++) { cin >> num; people.push_back(vector<int>{i, num}); } for (int i = 0; i < m; i++) { cin >> num; character.push_back(vector<int>{i, num}); } sort(people.begin(), people.end(), cmp1); sort(character.begin(), character.end(), cmp1); for (vector<int> peo : people) { cout << peo[0] << "," << peo[1] << " "; } cout << endl; int k = 0; for (int i = 0; i < m; i++) { if (character[i][1] >= people[k][1]) { chosen[people[k][0]] = character[i][0] + 1; k++; if (k > n - 1) break; } } for (int i = 0; i < n; i++) { cout << chosen[i] << " "; } } return 0; }
#笔试题目##百度#