您的位置:首页 > 其它

独木舟上的旅行(贪心)

2011-10-22 14:26 211 查看
/*日期:2011-10-22
  作者:xiaosi
  题目: 独木舟上的旅行(贪心)
*/
#include<iostream>
#include<cstdio>
#include<stdlib.h>
using namespace std;
int weight[301];
int cmp(const void *a,const void *b)
{
    return *(int *)b-*(int *)a;
}
int main()
{
    int N;
    while(scanf("%d",&N)!=EOF)
    {
        while(N--)
        {
            int i,j,W,n,count=0,k;
            scanf("%d %d",&W,&n);
            for(i=0;i<n;i++)
            {
                scanf("%d",&weight[i]);
            }
            qsort(weight,n,sizeof(weight[0]),cmp);
            i=0;k=n-1;
            while(i<=k)
            {
                if(i!=k)
                {
                    if(weight[i]+weight[k]<=W)
                    {
                        i++;
                        k--;
                    }
                    else
                    {
                        i++;
                    }
                }
                else
                {
                    i++;
                }
              count++;
            }
            printf("%d\n",count);
        }
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: