1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| #include <iostream> #include <algorithm> #include <vector> #include <string> #include <deque> using namespace std;
int main() { int first = 1; typedef pair<string, int> person; vector<person> P; int n, k; cin >> n >> k; for (int i = 0; i < n; i++) { string name; int height; cin >> name >> height; P.emplace_back(name, height); }
sort(P.begin(), P.end(), [](const person &p1, const person &p2) { return p1.second != p2.second ? p1.second > p2.second : p1.first < p2.first; });
int rowcount = n / k; int rest = n % k; deque<int> row; for (int i = 0, left = 1; i < n; i++) { if (left) row.push_back(i); else row.push_front(i); left = 1 - left; if (row.size() == rowcount + rest) { rest = 0; for (auto &&i : row) { cout << (first ? "" : " ") << P[i].first; first = 0; } first = 1; cout << endl; row.clear(); left = 1; } } for (auto &&i : row) { cout << (first ? "" : " ") << P[i].first; first = 0; } return 0; }
|