fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  4. int main() {
  5. FAST
  6. int n;cin>>n;
  7. vector<pair<int,long long>> v(n);
  8. for(int i=0;i<n;i++) {
  9. long long x;cin>>x;
  10. v[i].first=x;
  11. v[i].second=i+1;
  12. }
  13. sort(v.begin(),v.end());
  14. for(int i=0;i<n;i++) {cout<<v[i].first<<" "<<v[i].second<<endl;}
  15. if(v[0].first==v[n-1].first) {cout<<0;return 0;}
  16. long long l=v[0].second;long long r=v[n-1].second;
  17. for(int i=0;i<n-1;i++) {
  18. if(v[i].first==v[i+1].first) {l=v[i+1].second;}
  19. else
  20. break;
  21. }
  22. for(int i=n-1;i>0;i--) {
  23. if(v[i].first==v[i-1].first){ r=v[i-1].second;}
  24. else
  25. break;
  26. }
  27. cout<<r-l;
  28. }
  29.  
  30.  
Success #stdin #stdout 0.01s 5292KB
stdin
5
3 3 2 1 1 
stdout
1 4
1 5
2 3
3 1
3 2
-4