#include<iostream> #include<vector> #include<algorithm> using namespace std; bool cmp(int a, int b) { return a > b; } bool cmp1(vector<int>a, vector<int>b) { return a[0] > b[0]; } int main() { int n, m; while (cin >> n >> m) { vector<int>a(m); //糖的能量值 for (int i = 0; i < m; i++) cin >> a[i]; vector<vector<int>>b(n,vector<int>(2)); //存储小熊的战斗力和饥饿值 for (int i = 0; i < n; i++) { for (int j = 0; j < 2; j++) { cin >> b[i][j]; } } sort(a.begin(), a.end(),cmp); vector<vector<int>>b1(b.begin(), b.end()); sort(b1.begin(), b1.end(), cmp1); vector<int>flag(m, 0); vector<int>res(n,0); //存储小熊剩余饥饿值 for (int i = 0; i < n; i++) { int j = 0; int tmp = b1[i][1]; while (j < m) { if (a[j] <= tmp && !flag[j]) { tmp = tmp - a[j]; flag[j] = 1; } j++; } res[i]=tmp; } for (int i = 0; i < n; i++) //输出 { int t = b[i][0]; for (int j = 0; j < n; j++) { if (b[j][0] == t) { cout << res[j] << endl; break; } } } } } 不知道为什么只通过了40%,思路应该没问题,我觉得应该是超时了。求大神指点。