fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. bool fun(int mid, vector<int>v, int k){
  4. int i=mid;
  5. while(i<v.size()-1 && v[i]==v[i+1]){
  6. i++;
  7. }
  8.  
  9. if((i+1)%(k)==0){
  10. //mid=i+k-1;
  11. return true;
  12. }
  13. // mid=i;
  14. return false;
  15. }
  16. int main()
  17. {
  18. vector<int>v={
  19. 10, 10, 20, 20, 20, 30, 30, 30
  20. };
  21. int n=v.size();
  22. int k=3;
  23. int l=0;
  24. int h=n-1;
  25. int ans=0;
  26.  
  27. while(l<=h){
  28. int mid=(l+h)/2;
  29. //cout<<l<<" "<<h<<" "<<mid<<endl;
  30. if(fun(mid,v, k)==true){
  31. l=mid+1;
  32. }
  33. else{
  34.  
  35. h=mid-1;
  36. ans=v[mid];
  37. }
  38.  
  39. }
  40. cout<<" ans "<<ans<<endl;
  41.  
  42. }
  43.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
 ans 10