Submission #477268

#TimeUsernameProblemLanguageResultExecution timeMemory
477268AkiYuuBuilding Bridges (CEOI17_building)C++17
30 / 100
3074 ms3524 KiB
#include <bits/stdc++.h> #define task "GROUP" #define ll long long #define ld long double #define pb(u) emplace_back(u) #define ffw(i,a,b) for (ll i = a; i <= b; i++) #define fbw(i,b,a) for (ll i = b; i >= a; i--) #define adj(v,adj,u) for (auto v : adj[u]) #define rep(i,a) for (auto i : a) #define reset(a) memset(a, 0, sizeof(a)) #define sz(a) a.size() #define all(a) a.begin(),a.end() using namespace std; const int mxn = 1e6 + 5; const int inf = 1e18 + 7; typedef pair<ll, ll> ii; void fastio(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); // freopen(task".inp", "r", stdin); // freopen(task".out", "w", stdout); } ll n, w[mxn], h[mxn], dp[mxn]; bool sol2; ll tot = 0; void subtask1(){ ll res; ffw(i,2,n){ dp[i] = inf; res = 0; fbw(j,i-1,1){ ll cur = res + dp[j] + (h[i] - h[j]) * (h[i] - h[j]); if ( cur < dp[i]) dp[i] = cur; res += w[j]; } } cout << dp[n] << '\n'; } ll trans(int i){ return (h[1]-h[i]) * (h[1]-h[i]) + (h[i]-h[n]) * (h[i]-h[n]) + tot - w[1] - w[i] - w[n]; } void subtask2(){ map<ll, set<ll> > check; ll res = tot - w[1] - w[n] + (h[n] - h[1]) * (h[n] - h[1]); ffw(i,2,n-1){ ffw(j,-20,20){ if (check[j].size() != 0){ auto point = check[j].lower_bound((h[i] + h[1])/2); res = min(res, trans(i)); if ( point != check[j].end()) res = min(res,(h[1]-(*point)) * (h[1]-(*point)) + ((*point)-h[i]) * ((*point)-h[i]) + (h[n]-h[i])*(h[n]-h[i]) + tot - w[1] - w[i] - w[n] - j); if ( point != check[j].begin()){ point = prev(point); res = min(res,(h[1]-(*point)) * (h[1]-(*point)) + ((*point)-h[i]) * ((*point)-h[i]) + (h[n]-h[i]) * (h[n]-h[i]) + tot - w[1] - w[i]- w[n] - j); } } } check[w[i]].insert(h[i]); } cout << res << '\n'; } void solve(){ cin >> n; ffw(i,1,n) cin >> h[i]; sol2 = true; ffw(i,1,n) { cin >> w[i]; if ( abs(w[i]) > 2) sol2 = false; tot += w[i]; } if (!sol2) subtask1(); else subtask2(); } int main(){ fastio(); solve(); }

Compilation message (stderr)

building.cpp:16:22: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
   16 | const int inf = 1e18 + 7;
      |                 ~~~~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...