题目链接:http://codeforces.com/contest/1133/problem/C

       题意是给了n个数,让你找出m个数,使得这m个数中的最大值减去最小值不大于5,求最大的m

       本来感觉暴力可以写,但是不知道为什么wa8(可能姿势不太对,看别人的O(n)扫一遍也能过),然后就用upper_buond写了,先排个序,然后对于每一位去求第一个大于pre[i]+5所在的位置,从而更新最大值就好了。


AC代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n,m;
int pre[200005];

int main()
{
  scanf("%d",&n);
  for(int i=0;i<n;i++){
    scanf("%d",&pre[i]);
  }
  sort(pre, pre + n);
  int ans = 1;
  for(int i=0;i<n-1;i++){
    int cnt = upper_bound(pre, pre + n, pre[i] + 5) - pre;
    ans = max(ans, cnt - i);
  }
  cout<<ans<<endl;
  return 0;
}