#45756: c++答案沒有詳解


yp11351100@yphs.tp.edu.tw (801-28胡可岳)

學校 : 臺北市私立延平高級中學
編號 : 276234
來源 : [45.38.17.252]
最後登入時間 :
2025-08-06 14:37:29

#include <iostream>
#include <algorithm>
using namespace std;
  
int main() {
    int w, n;
    while (cin >> w >> n){
        int a[n];
        for (int i=0; i<n; i++){
            cin >> a[i];
        }
        sort(a, a+n);
        int cnt = 0;
        int l = 0;
        int r = n-1;
        while (l < r){
            if (a[r] + a[l] <= w){
                l++;
                r--;
            } else {
                r--;
            }
            cnt++;
        }
        if (l == r) cnt++;
        cout << cnt << '\n';
    }
     
    return 0;
}