独木舟上的旅行(贪心)

来源:互联网 发布:landwell巡更棒软件 编辑:程序博客网 时间:2024/05/18 02:02
/*日期: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;}


原创粉丝点击