Balanced Team

https://vjudge.net/problem/CodeForces-1133C

题意:在数组中找出一段  每两个元素差值不大于5的这段元素个数的最大值。

 1 #include <iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cstdlib>
 6 #include<cmath>
 7 
 8 using namespace std;
 9 int main()
10 {
11     int n;
12     int str[200005];
13     cin>>n;
14     for(int i=0; i<n; i++)
15         cin>>str[i];
16     sort(str,str+n);
17     int t=0;
18     int maxx=0;
19     for(int i=0; i<n; i++)
20     {
21         while(t<n&&str[t]-str[i]<=5)
22             t++;//注意此处的t值得变化
23                     //刚开始我以为是找出一段数组,判断大小,然后跳过这段,后来发现不
24                     // 对,因为当a[i]到a[j]这段数组符合条件时,t=j;但是可能a[i+1]到
25                     //a[j]、a[j+x]这段也符合条件,但后者的个数更大,所以t的值只能由
26                     //while循环改变,因为t始终是大于或等于i的,所以不用担心会出现倚遗
27                     //漏的
28         maxx=max(t-i,maxx);
29     }
30     printf("%d",maxx);
31     return 0;
32 }
View Code

 

转载于:https://www.cnblogs.com/hbhdhd/p/10887471.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值