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

猜你喜欢

转载自www.cnblogs.com/hbhdhd/p/10887471.html