测试点可能会有10000人都在一排的情况,但是直接string matrix[10001][10001],又会报段错误,讨巧写了[101][10001]过了,但是感觉正确写法应该要 dynamic allocate。。。
#include<iostream> #include<string> #include<map> #include<vector> #include<algorithm> #include<queue> #include<set> #include<stack> using namespace std; struct stu { string name; int h; }; bool cmp(stu s1, stu s2) { if (s1.h != s2.h) { return s1.h > s2.h; } else return s1.name < s2.name; } int main() { int num, k; cin >> num >> k; vector<stu> arr(10001); for (int i = 0; i < num; i++) { stu temp; cin >> temp.name >> temp.h; arr.push_back(temp); } sort(arr.begin(), arr.end(), cmp); int m = num / k; int lm = m + num%k; string matrix[100][100]; int i = 0; for (i; i < lm; i++) { if (i % 2 == 1) { matrix[k - 1][lm / 2 + 1 - (i / 2 + 1)] = arr[i].name; } else matrix[k - 1][lm / 2 + 1 + (i / 2)] = arr[i].name; } for (int a = 1; a < k ; a++) { for (int b = 0; b < m; b++) { if (b % 2 == 1) { matrix[k - 1 - a][m / 2 + 1 - (b / 2 + 1)] = arr[i].name; i++; } else { matrix[k - 1 - a][m / 2 + 1 + (b / 2)] = arr[i].name; i++; } } } for (int i = 1; i <= lm; i++) { if(i==1) cout << matrix[k-1][i]; else cout <<' '<< matrix[k - 1][i]; } cout << endl; for (int a = 1; a < k; a++) { for (int b = 1; b <= m; b++) { if (b == 1) cout << matrix[k - 1 - a][b]; else cout <<' '<< matrix[k - 1 - a][b]; } cout << endl; } system("pause"); }