Submission #1108559

#TimeUsernameProblemLanguageResultExecution timeMemory
1108559wayzzjrBuilding Bridges (CEOI17_building)C++17
100 / 100
53 ms67408 KiB
// ----------------------- TEMPLATE NO.4 --------------------------- #include<bits/stdc++.h> using namespace std; #define ll long long const int N = 1e5 + 100; const int MX = 1e6 + 2; const ll oo = 1e16; // loops #define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i) #define ROF(i, b, a) for (int i = (b), _a = (a); i >= _a; --i) // others :> #define nl '\n' //----------------------------------SOLUTION-----------------------------------// int n; ll w[N], h[N], dp[N], pre[N]; struct line{ ll a, b; line() : a(0), b(oo) {} line(ll a, ll b) : a(a), b(b) {} ll calc(ll x) { return a * x + b; } ll slope() { return a; } } f[4 * MX]; #define mid ((lx + rx) / 2) void update(int idx, int lx, int rx, line x){ if(lx == rx){ f[idx] = (x.calc(lx) < f[idx].calc(lx) ? x : f[idx]); return; } if(f[idx].calc(mid) > x.calc(mid)) swap(x, f[idx]); if(f[idx].slope() < x.slope()) update(idx<<1, lx, mid, x); if(f[idx].slope() > x.slope()) update(idx<<1|1, mid+1, rx, x); } ll query(int idx, int lx, int rx, int p){ ll cur = f[idx].calc(p); if(lx == rx) return cur; if(p <= mid) return min(cur, query(idx<<1, lx, mid, p)); else return min(cur, query(idx<<1|1, mid+1, rx, p)); } ll sqr(ll x){ return x * x; } void solve(){ cin >> n; FOR(i, 1, n) cin >> h[i]; FOR(i, 1, n) cin >> w[i]; FOR(i, 1, n) pre[i] = pre[i-1] + w[i]; // dp[i] = min(dp[j] + h[i] ^ 2 - 2 * h[i] * h[j] + h[j] ^ 2 + pre[i-1] - pre[j]) // dp[i] = min(dp[j] + h[j] ^ 2 - pre[j] - 2 * h[i] * h[j]) + h[i] ^ 2 + pre[i-1] update(1, 0, MX, line(-2 * h[1], sqr(h[1]) - pre[1]) ); FOR(i, 2, n){ dp[i] = query(1, 0, MX, h[i]) + sqr(h[i]) + pre[i-1]; update(1, 0, MX, line(-2 * h[i], dp[i] + sqr(h[i]) - pre[i])); } cout << dp[n]; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define Wayzz "name" if (fopen (Wayzz".inp", "r")) { freopen (Wayzz".inp", "r", stdin); freopen (Wayzz".out", "w", stdout); } int T = 1; while(T--) solve(); cerr <<"\nTime Elapse: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; }

Compilation message (stderr)

building.cpp: In function 'int main()':
building.cpp:79:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   79 |   freopen (Wayzz".inp", "r", stdin);
      |   ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
building.cpp:80:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |   freopen (Wayzz".out", "w", stdout);
      |   ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...