C - Traveling Plan
C - Traveling Plan
O(N)
#include <bits/stdc++.h> #define rep(i,n) for(int i=0; i<(n); ++i) #define repx(i,x,n) for(int i=x; i<(n); ++i) #define fixed_setprecision(n) fixed << setprecision((n)) #define execution_time(ti) printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - ti) / CLOCKS_PER_SEC); #define pai 3.1415926535897932384 #define NUM_MAX 2e18 #define NUM_MIN -1e9 using namespace std; using ll = long long; using P = pair<int,int>; template<class T> inline bool chmax(T& a, T b){ if(a<b){ a=b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b){ if(a>b){ a=b; return 1; } return 0; } int main(){ int N; cin >> N; vector<int> A(N); for(auto &a:A) cin >> a; int sum=0; for(int i=0; i<N; i++){ int pos=0; if(i) pos = A[i-1]; sum += abs(A[i] - pos); } sum += abs(0 - A[N-1]); for(int i=0; i<N; i++){ int pos=0; int t = sum; if(i) pos = A[i-1]; if(i<N-1){ t -= abs(A[i] - pos); t -= abs(A[i+1] - A[i]); t += abs(A[i+1] - pos); }else{ t -= abs(0 - A[i]); t -= abs(A[i] - A[i-1]); t += abs(0 - A[i-1]); } cout << t << endl; } return 0; }