1 #include<iostream> 2 #include<vector> 3 #include<algorithm> 4 using namespace std; 5 6 struct GT_cls //创建函数对象 7 { 8 public: 9 GT_cls(int t=0):a(t){} 10 11 bool operator()(int x) 12 { 13 return x>a; 14 } 15 private: 16 int a; 17 }; 18 19 int main() 20 { 21 int T,N,M; 22 cin>>T; 23 for(int i=0; i<T; i++) 24 { 25 cin>>N>>M; 26 vector<int> eigenval(N); 27 for(int j=0; j<N; j++) 28 cin>>eigenval[j]; 29 30 sort(eigenval.begin(), eigenval.end()); //从小到大排序 31 32 int max=1; 33 for(int j=0; j<N-1; j++) 34 { 35 vector<int>::iterator it = find_if(eigenval.begin()+j, eigenval.end(), GT_cls(eigenval[j]+M)); //所选取的几种化合物只要满足最小特征值和最大特征值不大于阈值M即可 36 unsigned num = it-(eigenval.begin()+j); 37 if(num>max) 38 max = num; 39 } 40 cout<<max<<endl; 41 } 42 return 0; 43 }