Advertisement
Guest User

Untitled

a guest
Oct 12th, 2014
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4. #include <map>
  5. #include <stack>
  6. #include <fstream>
  7. #include <string>
  8. #include <algorithm>
  9. #include <sstream>
  10. #include <cmath>
  11.  
  12. using namespace std;
  13.  
  14. #define JUDJE
  15.  
  16. #ifndef JUDJE
  17.  
  18. ifstream in("input.txt");
  19. ofstream out("output.txt");
  20. #define cin in
  21. #define cout out
  22.  
  23. #endif
  24.  
  25. #define ll long long
  26. #define MOD 1000000007
  27. #define mp(a, b) make_pair(a, b)
  28. #define pii pair<int, int>
  29.  
  30. ll depth[223456];
  31.  
  32.  
  33. int main()
  34. {
  35. ios_base::sync_with_stdio(0);
  36. int n;
  37. cin >> n;
  38. set<int> st;
  39. int t;
  40. cin >> t;
  41. st.insert(t);
  42. depth[t] = 0;
  43. ll answer = 0;
  44. for(int i = 2; i <= n; ++i)
  45. {
  46. int a;
  47. cin >> a;
  48.  
  49. set<int>::iterator ubound = upper_bound(st.begin(), st.end(), a);
  50. if(ubound == st.end())
  51. answer += depth[a] = depth[*(--ubound)] + 1;
  52. else
  53. {
  54. if(ubound == st.begin())
  55. answer += depth[a] = depth[*ubound] + 1;
  56. else
  57. {
  58. set<int>::iterator lbound = --upper_bound(st.begin(), st.end(), a);
  59. if(depth[*lbound] > depth[*ubound])
  60. answer += depth[a] = depth[*lbound] + 1;
  61. else
  62. answer += depth[a] = depth[*ubound] + 1;
  63. }
  64. }
  65.  
  66. st.insert(a);
  67. }
  68.  
  69. cout << answer;
  70. return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement