poj2782

简单题,先从大到小排序,然后从两端开始向中间看看能不能把右端的匹配给左端。

View Code
#include <iostream>
#include
<cstdio>
#include
<cstdlib>
#include
<cstring>
#include
<algorithm>
using namespace std;

#define maxn 100005

int n, m;
int f[maxn];

int cmp(int a, int b)
{
return a > b;
}

int main()
{
//freopen("t.txt", "r", stdin);
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++)
scanf(
"%d", &f[i]);
sort(f, f
+ n, cmp);
int l = 0, r = n - 1;
while (l <= r)
{
if (f[l] + f[r] <= m)
r
--;
l
++;
}
printf(
"%d\n", l);
return 0;
}

posted @ 2011-05-14 20:29  金海峰  阅读(186)  评论(0编辑  收藏  举报