Advertisement
Guest User

Untitled

a guest
Feb 7th, 2021
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define pb push_back
  5. #define pii pair<int,int>
  6. #define pll pair<ll,ll>
  7. //#define x first
  8. //#define y second
  9. #define all(x) x.begin(),x.end()
  10. #define f(i,a,b) for(ll i=a;i<b;++i)
  11. #define rof(i,a,b) for(ll i=a;i>b;--i)
  12. #define endl "\n" //to flush use endl not \n // cout<<endl; == cout<<"\n"<<flush; //632)D //for interactive comment this line and just use like cout<<"whatever"<<endl; cin>>x; //cout<<""<<endl; me hamesha endl use karna. cin ke bad kuch endl alag se nhi.//Is line ko Comment krke then Ekdum normal code h basically
  13. ll mod=1e9+7;
  14. // KMP // ll index=str1.find(str2); if(index!=string::npos) //found at index.. // str1.find(str2,starting_index)
  15. //(1LL<<i) left shift/powers of 2 //dont us pow(2,_)
  16. //All Combinations/Permu in circle (n-1)! // circle ka dekho OR 0 se end then from beg. to 0 , 1 se end then beg. to 1 , ...
  17. //cf dont use unordered map. use simply map<... , ... > m;
  18. //They are Iterator minus v.begin() krna //lower_bound(v.begin(),v.end(),x)-v.begin() //upper_bound(v.begin(),v.end(),x)-v.begin()
  19. // lower bound tells greater than or equal to // upper bound tells strictly greater than
  20. // ll lcm(ll a, ll b){ return a / __gcd(a, b) * b; }
  21. //memset can initialize to 0 or -1 ONLY //memset(str, 't', sizeof(str)); // string //memset(a, 0, sizeof(a)); //1D array // memset(a,0,sizeof(a[0][0])*n*n); //2D array
  22. // set.insert(),find() //for(auto it=set.begin();....) cout<<*it; // sort(v.begin(),v.end(),compare)
  23. //bool lexicographical_compare(all(s1),all(s2)) //1 if s1<s2
  24. //Binary Search socho. n me k size ka kaam krna ho aur n<10^6,k<10^9 me O(nlogk) works. 1 se 1e9 pe Binary search maro har me O(n) kaam!
  25. //s1+=s2 zada time. use s1.append(s2).
  26. //priority_queue <ll> pq; // By DEFAULT, MAX Priority Queue
  27. // priority_queue <ll, vector<ll>, greater<ll>> pq; // MIN Priority Queue
  28. //bool compare(pll h1,pll h2) { return (hi.first<h2.first); } //ascending
  29. // std::vector<vector<ll>> v(100001);
  30. std::vector<ll> v[100001];
  31. std::vector<ll> v1,v2;
  32. void solve()
  33. {
  34. ll n;cin>>n;
  35. ll a[n];
  36. f(i,0,n) cin>>a[i];
  37. for(ll i=n-1;i>=0;i--)
  38. {
  39. v[a[i]].pb(i);
  40. }
  41.  
  42. //D2
  43.  
  44.  
  45. f(i,0,n)
  46. {
  47. v[a[i]].pop_back();
  48. if(v1.size()==0) {v1.pb(a[i]);continue;}
  49. if(v2.size()==0)
  50. {
  51. if(a[i]==v1.back()) continue;
  52.  
  53. if(v[a[i]].size()==0)
  54. {v1.pb(a[i]);continue;}
  55. else
  56. {
  57. v2.pb(a[i]);continue;
  58. }
  59. // v2.pb(a[i]);continue;
  60. }
  61.  
  62. ll no1=v1.back(),no2=v2.back();
  63.  
  64. if(a[i]==no1||a[i]==no2) continue;
  65.  
  66. ll yaha1=n,yaha2=n;
  67. if(v[no1].size()!=0)
  68. yaha1=v[no1].back();
  69. if(v[no2].size()!=0)
  70. yaha2=v[no2].back();
  71.  
  72. if(yaha1>yaha2)
  73. {
  74. //v1 me daalo
  75. if(v1.back()!=a[i])
  76. {
  77. v1.pb(a[i]);
  78. }
  79. else if(v2.back()!=a[i])
  80. {
  81. v2.pb(a[i]);
  82. }
  83. }
  84. else
  85. {
  86. if(v2.back()!=a[i])
  87. {
  88. v2.pb(a[i]);
  89. }
  90. else if(v1.back()!=a[i])
  91. {
  92. v1.pb(a[i]);
  93. }
  94. }
  95. }
  96. // cout<<v2.size()<<" ";
  97.  
  98. cout<<(v1.size()+v2.size());
  99. }
  100.  
  101. int main()
  102. {
  103.  
  104. ios_base::sync_with_stdio(false);
  105. cin.tie(NULL);
  106. //cout << fixed << setprecision(14);
  107. ll t=1;
  108. // cin>>t;
  109. while(t--)
  110. {
  111. solve();
  112. }
  113. cerr << "Time elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement