思路:

  1. 将人员按照服务时间从小到大排序。
  2. 遍历排序后的人员列表,计算每个人等待的总时间。
  3. 如果等待总时间大于等于当前人的服务时间,则不会失望,将不失望人数加1。
  4. 输出不失望人数。

代码实现如下:

#include #include

using namespace std;

int main() { int n; cin >> n;

int* t = new int[n];
for (int i = 0; i < n; i++) {
    cin >> t[i];
}

sort(t, t + n);

int totalWaitTime = 0;
int numNotDisappointed = 0;
for (int i = 0; i < n; i++) {
    if (totalWaitTime <= t[i]) {
        totalWaitTime += t[i];
        numNotDisappointed++;
    }
}

cout << numNotDisappointed << endl;

delete[] t;

return 0;

原文地址: https://www.cveoy.top/t/topic/h7Vt 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录