Advertisement
JosepRivaille

P34682: Fixed points

Mar 23rd, 2016
792
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6. int position(const vector<int> &S, const int a, int l, int r)
  7. {
  8.     if (l > r) return -1;
  9.     else {
  10.         int m = (l+r)/2;
  11.         if (S[m]+a == m+1 && (m == 0 || m != S[m-1] + a)) return m+1;
  12.         if (S[m]+a < m+1) return position(S, a, m+1, r);
  13.         else return position(S, a, l, m-1);
  14.     }  
  15. }
  16.  
  17. void process_data(const vector<int> &S, const vector<int> &A)
  18. {
  19.     for (int i = 0; i < A.size(); ++i) {
  20.         int pos = position(S, A[i], 0, S.size()-1);
  21.         if (pos == -1) cout << "no fixed point for " << A[i] << endl;
  22.         else cout << "fixed point for " << A[i] << ": " << pos << endl;
  23.     }
  24.     cout << endl;
  25. }
  26.  
  27. void read_vector(vector<int> &v)
  28. {
  29.   int siz = v.size();
  30.   for (int i = 0; i < v.size(); ++i)
  31.     cin >> v[i];
  32. }
  33.  
  34.  
  35. int main()
  36. {
  37.     int n, count = 0;
  38.     while (cin >> n) {
  39.       cout << "Sequence #" << ++count << endl;
  40.       vector<int> S(n);
  41.       read_vector(S);
  42.       cin >> n;
  43.       vector<int> A(n);
  44.       read_vector(A);
  45.       process_data(S, A);
  46.     }
  47. }
  48.  
  49. //JosepRivaille
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement