A .A Prize No One Can Win

题意:给定N,S,你要从N个数中选最多是数,使得任意两个之和不大于S。

思路:排序,然后贪心的选即可。

#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=2000010;
ll a[maxn];
int main()
{
    int N,ans; ll M;
    scanf("%d%lld",&N,&M);
    rep(i,1,N) scanf("%lld",&a[i]);
    sort(a+1,a+N+1);
    ans=1;
    rep(i,2,N){
        if(a[i]+a[i-1]<=M) ans=i;
    }
    printf("%d\n",ans);
    return 0;
}
View Code

相关文章:

  • 2021-06-16
  • 2021-12-02
  • 2021-11-07
  • 2021-07-20
  • 2021-09-15
  • 2021-12-08
  • 2021-09-30
  • 2022-01-20
猜你喜欢
  • 2021-11-20
  • 2021-08-08
  • 2021-08-11
  • 2022-01-31
  • 2021-11-18
  • 2022-03-05
  • 2021-09-20
相关资源
相似解决方案