Submission #313356

# Submission time Handle Problem Language Result Execution time Memory
313356 2020-10-15T21:06:47 Z mohamedsobhi777 Building Bridges (CEOI17_building) C++14
30 / 100
3000 ms 6708 KB
#include<bits/stdc++.h>


#pragma GCC optimize("-Ofast")
//#pragma GCC optimize("trapv")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx2,tune=native")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-funroll-loops")

#define I inline void 
#define S struct 
#define vi vector<int> 
#define vii vector<pair<int,int>>
#define pii pair<int,int>
#define pll pair<ll,ll>

using namespace std ; 
using ll = long long ; 
using ld = long double ; 

const int N = 2e5 + 7 , mod = 1e9 + 7 ; 
const ll inf = 2e18 ; 

// How interesting!

int n ; 
ll ans[N]; 
ll h[N], w[N] ; 
ll pref[N] ; 
vector<pair<ll,ll> > cht ; 

void add(ll x , ll b){
        cht.push_back({x , b}) ; 
}

ll get(ll x){
        ll ret = 1e18 ; 
        for(auto u : cht)
                ret = min(ret , u.first * x + u.second) ; 
        return ret ; 
}

ll eval(int l, int r){
        ll diff = h[r] - h[l] ; 
        return 1ll * diff * diff + pref[r - 1] - pref[l] ; 
}

int main(){
        ios_base::sync_with_stdio(0) ;
        cin.tie(0) ; 
        //freopen("in.in" , "r" , stdin) ; 
        cin >> n;  

        for(int i = 1 ;i <= n ; ++ i){
                cin >> h[i] ; 
        }
        for(int i = 1 ; i <= n ; ++ i){
                cin >> w[i] ; 
                pref[i] += w[i] ; 
                pref[i+1]+=pref[i] ; 
        }

        for(int i = 2 ; i <= n; ++ i){
                ll ret = eval(1 , i) ;
                ll cost = 1ll * h[i] * h[i] + pref[i - 1] ; 
                ret = min(ret , get(h[i]) + cost ) ; 
                ans[i] = ret ; 
                add( -2ll * h[i] , 1ll * h[i] * h[i] - pref[i] + ans[i] ) ; 
        }
        cout<<ans[n] ; 
        return 0 ; 
}

/*
        - bounds sir (segtree = 4N, eulerTour = 2N, ...)
        - a variable defined twice?
        - will overflow?
        - is it a good complexity?
        - don't mess up indices (0-indexed vs 1-indexed)
        - reset everything between testcases. 
*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 3081 ms 6708 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Execution timed out 3081 ms 6708 KB Time limit exceeded
7 Halted 0 ms 0 KB -