#include <bits/stdc++.h>
using namespace std;
#define el "\n"
#define FOR(i,a,b) for(int i = (a), _b = (b); i <= _b; i++)
#define FORD(i,a,b) for(int i = (a), _b = (b); i >= _b; i--)
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define lg(x) __lg(x)
#define alla(a,n) a+1,a+n+1
#define ll long long
template <class T> bool maxi(T &x, T y) { if(x < y) { x = y ; return true ;} return false;}
template <class T> bool mini(T &x, T y) { if(x > y) { x = y ; return true ;} return false;}
const int N = 1e5 + 2;
int n, h[N], w[N];
void inp()
{
cin >> n;
FOR(i, 1, n) cin >> h[i];
FOR(i, 1, n) cin >> w[i];
}
/* Try your best
No regrets */
namespace subtask_1
{
ll sq(ll x)
{
return 1LL * x * x;
}
ll dp[N];
void slv()
{
memset(dp, 0x3f, sizeof dp);
dp[1] = 0;
FOR(i, 2, n) {
ll total = 0;
FORD(j, i - 1, 1) {
mini(dp[i], dp[j] + sq(h[i] - h[j]) + total);
total += w[j];
}
}
cout << dp[n];
}
}
namespace subtask_2
{
const long long INF = 1e18;
const int MAX = 1e6 + 2;
ll dp[N], sum[N];
struct Line
{
ll a, b;
Line() {a = 0, b = INF;}
Line(ll _a, ll _b) {a = _a, b = _b;}
ll operator()(ll x) {return a * x + b;}
} st[4 * MAX];
void upd(int id, int l, int r, Line L)
{
if(l == r) {
if(st[id](l) > L(l)) swap(st[id], L);
return;
}
int mid = (r + l) >> 1;
if(st[id].a > L.a) swap(st[id], L);
if(st[id](mid) > L(mid)) {
swap(st[id], L);
upd(id << 1 | 1, mid + 1, r, L);
}
else upd(id << 1, l, mid, L);
}
ll get(int id, int l, int r, int pos)
{
if(l == r) return st[id](pos);
int mid = (r + l) >> 1;
if(pos <= mid) return min(st[id](pos), get(id << 1, l, mid, pos));
else return get(id << 1 | 1, mid + 1, r, pos);
}
void slv()
{
FOR(i, 1, n) {
sum[i] = sum[i - 1] + w[i];
}
dp[1] = 0;
upd(1, 1, 1e6, Line(- 2 * h[1], dp[1] + 1LL * h[1] * h[1] - sum[1]));
FOR(i, 2, n) {
dp[i] = get(1, 1, 1e6, h[i]) + 1LL * h[i] * h[i] + sum[i] - w[i];
upd(1, 1, 1e6, Line(- 2 * h[i], dp[i] + 1LL * h[i] * h[i] - sum[i]));
}
cout << dp[n];
}
}
/* Code slowly, think carefully */
main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define __Azul__ "CEOI17_building"
if(fopen(__Azul__".inp", "r")) {
freopen(__Azul__".inp", "r", stdin);
freopen(__Azul__".out", "w", stdout);
}
bool qs = 0;
int T = 1;
if(qs) cin >> T;
while(T--) {
inp();
// subtask_2::slv(); return 0;
if(n <= 1000) subtask_1::slv();
else subtask_2::slv();
}
cerr << "\nTime" << 0.001 * clock() << "s "; return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
building.cpp:111:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
111 | main()
| ^~~~
building.cpp: In function 'int main()':
building.cpp:117:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
117 | freopen(__Azul__".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
building.cpp:118:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
118 | freopen(__Azul__".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |