Submission #887253

#TimeUsernameProblemLanguageResultExecution timeMemory
887253vjudge1Building Bridges (CEOI17_building)C++17
30 / 100
35 ms20112 KiB
#include <bits/stdc++.h> using namespace std; const long long INF = 1e6; long long h[100005], w[100005], s[100005], dp[100005]; int n; void sub1() { memset(dp, 0x3f, sizeof(dp)); dp[1] = 0; for(int i = 2; i <= n; ++i) for(int j = 1; j < i; ++j) dp[i] = min(dp[i], dp[j] + (h[i] - h[j]) * (h[i] - h[j]) + s[i - 1] - s[j]); cout << dp[n]; } struct line { long long a, b; }; long long Get(line L, long long x) { return L.a * x + L.b; } line st[4000100]; void update(int low, int high, line w, int id) { if(low > high) return; if(low == high) { st[id] = w; return; } int mid = (low + high) >> 1; if(st[id].a > w.a) swap(st[id], w); if(Get(st[id], mid) > Get(w, mid)) { swap(st[id], w); update(mid + 1, high, w, id << 1 | 1); } else update(low, mid, w, id << 1); } long long query(int low, int high, long long x, int id) { if(low > high) return INF * INF; if(low == high) return Get(st[id], x); int mid = (low + high) >> 1; if(x <= mid) return min(Get(st[id], x), query(low, mid, x, id << 1)); return min(Get(st[id], x), query(mid + 1, high, x, id << 1 | 1)); } void full() { dp[1] = 0; update(0, INF, {-2 * h[1], dp[1] - s[1] + h[1] * h[1]}, 1); for(int i = 2; i <= n; ++i) { dp[i] = query(0, INF, h[i], 1) + s[i - 1] + h[i] * h[i]; update(0, INF, {-2 * h[i], dp[i] - s[i] + h[i] * h[i]}, 1); } cout << dp[n]; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); #define Task "BRIDGES" if(fopen(Task".INP", "r")) { freopen(Task".INP","r",stdin); freopen(Task".OUT","w",stdout); } cin >> n; for(int i = 1; i <= n; ++i) cin >> h[i]; for(int i = 1; i <= n; ++i) { cin >> w[i]; s[i] = s[i - 1] + w[i]; } if(n <= 1000) { sub1(); return 0; } full(); return 0; }

Compilation message (stderr)

building.cpp: In function 'int main()':
building.cpp:66:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   66 |         freopen(Task".INP","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
building.cpp:67:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |         freopen(Task".OUT","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...