一道贪心题
上代码:
#include<iostream> #include<algorithm> using namespace std; int a[100050]; int main() { int w, n; int ans = 0; cin >> w >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } sort(a + 1, a + 1 + n); int min = 1; int max = n; int sum = 0; while (min < max) { sum = a[min] + a[max]; if (sum > w) { max--; ans++; continue; } else { min++; max--; ans++; } } if (min == max) { ans++; } cout << ans << endl; return 0; }