华为机试4.13
第一题:硬件资源最佳分配
模拟+排序
#笔试题目#/** -*- encoding: utf-8 -*- Version :1.0 */ #include <iostream> #include <vector> #include <string> #include <algorithm> #include <map> using namespace std; bool cmp1(const vector<int> &a, const vector<int> &b) { return a[1] < b[1] ? true : (a[1] == b[1] && a[2] < b[2] ? true : false); } bool cmp2(const vector<int> &a, const vector<int> &b) { return a[2] < b[2] ? true : (a[2] == b[2] && a[1] < b[1] ? true : false); } int main() { int n; while (cin >> n) { vector<string> str_vec(n); for (int i = 0; i < n; i++) { cin >> str_vec[i]; } vector<vector<int>> vec(n, vector<int>()); map<int, vector<int>> mp; for (int i = 0; i < n; i++) { string temp; for (int j = 0; j < str_vec[i].size(); j++) { if (str_vec[i][j] == ',') { mp[i].push_back(stoi(temp)); temp = ""; } else { temp += str_vec[i][j]; } } mp[i].push_back(stoi(temp)); } vector<int> require(6); for (int i = 0; i < 6; i++) { cin >> require[i]; } int max_num = require[0]; int strategy = require[1]; int cpu = require[2]; int size = require[3]; int arch = require[4]; int zeor_or_one = require[5]; int count = mp.size(); if (arch != 9) { for (int i = 0; i < count; i++) { if (mp[i][1] < cpu || mp[i][3] != arch) { mp.erase(i); } } } if (zeor_or_one != 2) { for (int i = 0; i < count; i++) { if (mp.find(i) != mp.end()) { if (mp[i][1] < cpu || mp[i][4] != zeor_or_one) { mp.erase(i); } } } } for (int i = 0; i < count; i++) { if (mp.find(i) != mp.end()) { if (mp[i][1] < cpu) { mp.erase(i); } } } vector<vector<int>> ans; for (auto it : mp) { ans.push_back(it.second); } int res_num = 0; vector<int> res; if (ans.size() <= max_num) { res_num = ans.size(); for (auto it : ans) { res.push_back(it[0]); } } else { res_num = max_num; if (strategy == 1) { sort(ans.begin(), ans.end(), cmp1); } else { sort(ans.begin(), ans.end(), cmp2); } for (int i = 0; i < max_num; i++) { res.push_back(ans[i][0]); } } if (res_num == 0) { cout << res_num << endl; } else { cout << res_num << " "; } for (int i = 0; i < res_num; i++) { if (i == res_num - 1) { cout << res[i] << endl; } else { cout << res[i] << " "; } } } return 0; }