Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. //  Copyright © 2017 Adán López Alatorre. All rights reserved.
  2. //
  3.  
  4. #include <iostream>
  5. #include <vector>
  6. #include <string.h>
  7. #include <deque>
  8. #include <queue>
  9. #include <algorithm>
  10. #include <set>
  11. #include <map>
  12. #include <sstream>
  13. #include <stack>
  14. #include <iomanip>
  15. #include <fstream>
  16. #include <climits>
  17. #include <cmath>
  18. #define fi first
  19. #define se second
  20. #define FI first.first
  21. #define SE first.second
  22. #define TH second
  23. #define MAXN maxN
  24.  
  25. using namespace std;
  26.  
  27. typedef long long ll;
  28. typedef pair<int, int> ii;
  29. typedef pair<ii, int> iii;
  30. typedef pair<ll, ll> dd;
  31. typedef vector<int> vi;
  32. typedef set<ii>::iterator iter;
  33.  
  34. const int maxN = 2e5 + 3, MOD = 1e9 + 7, AL = 10;
  35.  
  36. int arr[maxN], n, act[maxN];
  37. ll tot = 0;
  38.  
  39. int main(){
  40.     ios::sync_with_stdio(false);
  41.     cin.tie(0), cout.tie(0);
  42.    
  43.     cin >> n;
  44.     for(int i = 0; i < n; i++){
  45.         cin >> arr[i];
  46.     }
  47.    
  48.     stack<int> st;
  49.    
  50.     int cur = 0;
  51.     for(int i = 0; i < n; i++){
  52.         st.push(i);
  53.         while(cur < arr[i] + 1){
  54.             cur++;
  55.             act[st.top()]++;
  56.             st.pop();
  57.         }
  58.     }
  59.    
  60.     for(int i = 1; i < n; i++){
  61.         act[i] += act[i - 1];
  62.     }
  63.    
  64.     for(int i = 0; i < n; i++){
  65.         tot += act[i] - arr[i] - 1;
  66.     }
  67.     cout << tot << '\n';
  68.    
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement