#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], h[1] * h[1] - s[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
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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
19360 KB |
Output is correct |
2 |
Correct |
35 ms |
19288 KB |
Output is correct |
3 |
Correct |
35 ms |
19316 KB |
Output is correct |
4 |
Correct |
32 ms |
19092 KB |
Output is correct |
5 |
Incorrect |
27 ms |
8028 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
36 ms |
19360 KB |
Output is correct |
7 |
Correct |
35 ms |
19288 KB |
Output is correct |
8 |
Correct |
35 ms |
19316 KB |
Output is correct |
9 |
Correct |
32 ms |
19092 KB |
Output is correct |
10 |
Incorrect |
27 ms |
8028 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |