[51nod-1432]独木舟 题解

题目传送门
题目很简单,明显的贪心,每次如果最大值和最小值可以在同一个独木舟上,那么就在一起,不然最大的自己一个。
注意最后如果只剩下一个的情况。
代码:

#include
#include
#include
#include
#include
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
#define Clear(a,x) memset(a,x,sizeof(a))
#define ll long long
#define INF 2000000000
#define eps 1e-8
using namespace std;
int read(){
    int x=0,f=1; 
    char ch=getchar();
    while (ch<'0'||ch>'9') f=ch=='-'?-1:f,ch=getchar();
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
const int maxn=10005;
int n,m,ans;
int a[maxn];
int main(){
    n=read();m=read();
    rep(i,1,n) a[i]=read();
    sort(a+1,a+n+1);
    int l=1,r=n;
    while (lif (a[l]+a[r]<=m) ans++,l++,r--;
            else ans++,r--;
    }
    if (l==r) ans++;
    printf("%d\n",ans);
    return 0;
}

你可能感兴趣的:(水题,贪心,51nod)