fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool identical(vector<int>v,int k){
  5. map<int,int>m;
  6. for(int i=0;i<v.size();i++){
  7. if(m[v[i]]==0){
  8. m[v[i]]=i+1;
  9. }else{
  10. if((i+1-m[v[i]])<=k){
  11. return true;
  12. }else{
  13. m[v[i]]=i+1;
  14. }
  15. }
  16. }
  17. return false;
  18. }
  19.  
  20. int main() {
  21. // your code goes here
  22. int n;cin>>n;
  23. vector<int>v(n);
  24. for(int i=0;i<n;i++)cin>>v[i];
  25. int k;cin>>k;
  26. if(identical(v,k)){
  27. cout<<"YES"<<endl;
  28. }else{
  29. cout<<"NO"<<endl;
  30. }
  31. return 0;
  32. }
Success #stdin #stdout 0s 5320KB
stdin
5
3 2 4 3 1
3
stdout
YES