思路
- 贪心思想,首先将纪念品价格排序,再尽量将最高价格与最低价格的纪念品组合。
代码实习
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int w, n;
cin >> w >> n;
vector<int> arr(n);
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
sort(arr.begin(), arr.end());
int ans = 0;
int i = 0, j = arr.size() - 1;
while (i < j)
{
if (arr[i] + arr[j] <= w)
{
i++;
}
j--;
ans++;
}
if (i == j)
{
ans++;
}
cout << ans;
return 0;
}