Guest User

Untitled

a guest
Feb 16th, 2018
115
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //g++ filename.cpp -std=c++14 -DH
  2.  
  3. #ifdef H
  4. #include "/Users/michaelw/stdc++.h"
  5. #else
  6. #include <bits/stdc++.h>
  7. #endif
  8.  
  9. using namespace std;
  10.  
  11. #define pb push_back
  12. #define f first
  13. #define s second
  14. #define lb lower_bound
  15. #define ub upper_bound
  16. #define all(x) x.begin(), x.end()
  17. #define MOD 1e9 + 7
  18.  
  19. typedef long long ll;
  20. typedef pair<int, string> pis;
  21. typedef pair<int,int> pii;
  22.  
  23. //set tab size to 3
  24.  
  25. int main(){
  26.     //codeforces cin
  27.     ios_base::sync_with_stdio(false);
  28.     cin.tie(nullptr);
  29.     ifstream fin("filename.in");
  30.     ofstream fout("filename.out");
  31.     int n;
  32.     cin >> n;
  33.     vector<int> v(n);
  34.     int s = 1, end = 1000000, count = 0;
  35.     for(int i = 0; i < n; i++){
  36.         cin >> v[i];
  37.     }
  38.     int i = 0, j = v.size()-1;
  39.     while(i <= j){
  40.         while(s >= v[i] && i < v.size()){
  41.             // cout << "s " << s << " " << v[i] << endl;
  42.             i++;
  43.         }
  44.         while(end <= v[j] && j >= 0){
  45.             // cout << "end " << end << " " << v[j] << endl;
  46.             j--;
  47.         }
  48.         if(i > j) break;
  49.         // cout << "s " << s << " " << v[i] << endl;
  50.         // cout << "end " << end << " " << v[j] << endl;
  51.  
  52.         if(v[i] - s < end - v[j]){
  53.             count += abs(v[i] - s);
  54.             end -= abs(v[i] - s);
  55.             s = v[i];
  56.         }
  57.         else{
  58.             count += abs(end - v[j]);
  59.             s += abs(end - v[j]);
  60.             end = v[j];
  61.         }
  62.         // cout << "afters " << s << " " << i << endl;
  63.         // cout << "afterend " << end << " " << j <<endl;
  64.    
  65.     }
  66.     cout << count << endl;
  67.    
  68. }
RAW Paste Data