fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. int main() {
  7. long long n,x,max=-1;
  8. cin>>n;
  9.  
  10. vector <pair < long,long> >v,m;
  11. v.resize(n);
  12. m.resize(n);
  13.  
  14. for(int i=0;i<n;++i){
  15. cin>>x;
  16. v[i].first=x;
  17. v[i].second=i;
  18. m[i].first=-v[i].first;
  19. m[i].second=-v[i].second;
  20. }
  21.  
  22. sort(m.begin(),m.end());
  23.  
  24. int plus=0;
  25.  
  26. for(int i=0;i<n;++i){
  27. if(i==n-1){
  28. cout<<0;
  29. return 0;}
  30.  
  31.  
  32.  
  33. if(v[i].first<-m[plus].first &&v[i].second < -m[plus].second){
  34. cout<<-m[plus].first-v[i].first+1<<" ";}
  35. else if(v[i].first<-m[plus].first &&v[i].second > -m[plus].second)
  36. ++plus;
  37.  
  38. else if(v[i].first==-m[plus].first && v[i].second==-m[plus].second){
  39. cout<<0<<" ";
  40. ++plus;}
  41.  
  42. }
  43.  
  44. return 0;
  45. }
Success #stdin #stdout 0s 16056KB
stdin
5
1 2 3 1 2 4
stdout
3 2 0 2 0