nyoj--71 独木舟上的旅行(贪心)

来源:互联网 发布:淘宝买家信誉中等 编辑:程序博客网 时间:2024/04/27 08:05

nyoj 71

题解

贪心策略:在不超载的情况下,尽可能让最轻的和最重的同乘一条船。

#include <iostream>#include <algorithm>using namespace std;const int maxn = 300 + 10;int   a[maxn];int   t, n, w;int main(){    for(cin >> t; t--; )    {        cin >> w >> n;        for(int i = 0; i < n; ++i) cin >> a[i];        sort(a, a + n);        int ans = 0;        for(int i = 0, j = n - 1; i <= j; )        {            if(a[i] + a[j] <= w) i++;            j--;            ans++;        }        cout << ans << endl;    }}
0 0
原创粉丝点击