题目
思路就是简单排序,但要注意一下没有组成2个的单独讨论
- 注意的点就是先输入N 然后定义数组已经不适用了
- bool 数组标记有没有选择过
- == 与 =
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 30010;
int a[N];
bool b[N];
int main()
{
int max; cin >> max;
int n; cin >> n;
int count = 0;
for(int i=0; i<n; i++) cin >> a[i];
sort(a,a+n);
for(int j=0; j<n; j++)
{
for(int k=n-1; k>=j; k--)
{
if(a[k] + a[j] <= max && !b[j] && !b[k] )
{
count += 1;
b[j] = true; b[k] = true;
}
}
}
for(int i=0; i<n; i++)
{
if(!b[i]) count++;
}
cout << count << endl;
return 0;
}