很简单的一个小贪心,
贴出代码:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int maxn = 1111l;
int n;
int c;
int a[maxn];
int main()
{
while (scanf("%d%d", &n, &c) != EOF && n != 0)
{
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
sort(a, a + n);
int i = 0;
int j = n;
if (a[j] > c)
{
printf("-1\n");
continue;
}
int cnt = 0;
while (i < j)
{
if (a[i] + a[j] <= c)
{
i++;
j--;
cnt++;
}
else
{
j--;
}
}
if (i == j)
{
cnt++;
}
printf("%d\n", cnt);
}
system("pause");
return 0;
}