788A - Functions again - CodeForces Solution


dp two pointers *1600

Please click on ads to support us..

Python Code:

import sys

input = lambda: sys.stdin.readline().rstrip("\r\n")
printf = lambda d: sys.stdout.write(str(d) + "\n")
def read_int(): return int(input())
def read_ints(): return map(int, input().split())
def read_ints_grid(n): return [get_ints() for _ in range(n)]
def read_str(): return input().strip()
def read_strs(): return get_str().split(' ')

MAX = int(1e10) + 5
mod = 10 ** 9 + 7



def solve():
    n = read_int()
    nums = list(read_ints())

    maxv = float('-inf')
    pre1 = pre2 = 0
    flag = 1
    for i in range(0, n-1):
        v1 = abs(nums[i+1] - nums[i]) * (flag)
        v2 = abs(nums[i+1] - nums[i]) * (-flag)
        pre1 = max(pre1 + v1, v1)
        pre2 = max(pre2 + v2, v2)
        maxv = max(maxv, pre1, pre2)
        flag = -flag
    print(maxv)

solve()

C++ Code:

    #include <bits/stdc++.h>
    
   using namespace std;
   
   #define int            long long int
   #define F              first
   #define S              second
   #define pb             push_back
   #define si             set <int>
   #define vi             vector <int>
   #define pii            pair <int, int>
   #define vpi            vector <pii>
   #define vpp            vector <pair<int, pii>>
   #define mii            map <int, int>
   #define mpi            map <pii, int>
   #define spi            set <pii>
   #define endl           "\n"
   #define sz(x)          ((int) x.size())
   #define all(p)         p.begin(), p.end()
   #define double         long double
   #define que_max        priority_queue <int>
   #define que_min        priority_queue <int, vi, greater<int>>
   #define bug(...)       __f (#__VA_ARGS__, __VA_ARGS__)
   #define print(a)       for(auto x : a) cout << x << " "; cout << endl
   #define print1(a)      for(auto x : a) cout << x.F << " " << x.S << endl
   #define print2(a,x,y)  for(int i = x; i < y; i++) cout<< a[i]<< " "; cout << endl
   
   inline int power(int a, int b)
   {
     int x = 1;
     while (b)
     {
       if (b & 1) x *= a;
       a *= a;
       b >>= 1;
     }
     return x;
   }
    int ceil_div(int a,int b)
    {
       return (a+b-1)/b;
    }
   
   template <typename Arg1>
   void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
   template <typename Arg1, typename... Args>
   void __f (const char* names, Arg1&& arg1, Args&&... args)
   {
     const char* comma = strchr (names + 1, ',');
     cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
   }
   vector<int> sieve(int n) {int*arr = new int[n + 1](); vector<int > vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;}
   const int N = 200005;
   
   void solve() {
      int n;
      cin>>n;
      int a[n];
      for(auto &it:a)cin>>it;
       vector<int>v;
       for(int i=1;i<n;i++){
       	v.pb(abs(a[i]-a[i-1]));
       }
       int ans=v[0];
       int sum=0;
       for(int i=0;i<n-1;i++){
       	if(i&1)sum+=v[i];
       	else sum-=v[i];
       	 ans=max(ans,sum);
       	 if(sum<0)sum=0;
       } 
       sum=0;
       for(int i=0;i<n-1;i++){
       	if(i&1)sum-=v[i];
       	else sum+=v[i];
       	 ans=max(ans,sum);
       	 if(sum<0)sum=0;
       }
       
   cout<<ans<<endl;
         
         
   }
   
   int32_t main()
   {
     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 
   
   
   
     clock_t z = clock();
   
     int t = 1;
       
     while (t--) solve();
   
     cerr << "Run Time : " << ((double)(clock() - z) / CLOCKS_PER_SEC);
   
     return 0;
   }


Comments

Submit
0 Comments
More Questions

Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef