#include <bits/stdc++.h>
using namespace std;
// Typedef
typedef long double ld;
typedef long long int int64;
typedef unsigned long long int uint64;
typedef std::pair<int, int> PLL;
typedef std::pair<int64, int64> PII;
typedef std::vector<int> VI;
typedef std::vector<long long> VLL;
// Define For-loop
#define FOR(i, j, k, in) for (int i = (j); i < (k) ; i += (in))
#define FORW(i, j, k, in) for (int i = (j); i <= (k); i += (in))
#define RFOR(i, j, k, in) for (int i = (j); i >= (k); i -= (in))
// Define Data structure func
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.rbegin(), cont.rend()
#define sz(cont) int((cont).size())
#define pb push_back
#define mp make_pair
#define fi first
#define se second
// Define number
#define IINF 0x3f3f3f3f
#define LLINF 1000111000111000111LL
#define PI 3.1415926535897932384626433832795
// Other
#define elif else if
#define lend '\n'
#define hardio(name) freopen(name".inp","r",stdin), freopen(name".out","w",stdout);
void FastIO() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); }
const int MOD = 1e9 + 7, MOD2 = 1e9 + 9;
// ======================================================================
int64 h[100006], pf[100006] = {};
int64 dp[100006] = {};
int sqN = 0, n;
struct CVH {
vector<PII> lines;
double intersect(PII& l, PII& r) {
return (1.0*(l.se - r.se))/(1.0*(r.fi - l.fi));
}
bool bad(PII& a, PII& b, PII& c) {
return intersect(a, c) <= intersect(b, a);
}
void addLine(PII line, bool flag) {
if (flag) {
while (sz(lines) >= 2 && bad(lines[sz(lines) - 2], lines[sz(lines) - 1], line)) lines.pop_back();
if (sz(lines) == 1 && lines[0].fi == line.fi) lines.pop_back();
lines.pb(line);
} else {
lines.pb(line);
RFOR(i, sz(lines) - 1, 1, 1) {
if (lines[i - 1] <= lines[i]) swap(lines[i - 1], lines[i]);
else break;
}
}
}
int64 query(int64 x) {
int l = 0, r = sz(lines) - 1, m, best = -1;
while (l <= r) {
m = (l + r) >> 1;
double pos = m > 0 ? intersect(lines[m], lines[m - 1]) : -IINF;
if (pos <= x) best = m, l = m + 1;
else r = m - 1;
}
return best == -1 ? IINF : lines[best].fi * x + lines[best].se;
}
} off, aux, pil;
void Clear_Stack() {
aux.lines.clear();
int esq = 0, dir = 0;
while(esq < sz(off.lines) && dir < sz(pil.lines)) {
if(off.lines[esq] >= pil.lines[dir]) aux.addLine(off.lines[esq], 1), ++esq;
else aux.addLine(pil.lines[dir], 1), dir++;
}
while(esq < sz(off.lines)) aux.addLine(off.lines[esq], 1), ++esq;
while(dir < sz(pil.lines)) aux.addLine(pil.lines[dir], 1), ++dir;
off.lines = aux.lines, pil.lines.clear(), aux.lines.clear();
}
int main(int argc, char* argv[]) {
FastIO();
#ifndef ONLINE_JUDGE
hardio("input");
#endif
cin >> n; sqN = sqrt(n);
FORW(i, 1, n, 1) cin >> h[i];
FORW(i, 1, n, 1) cin >> pf[i], pf[i] += pf[i - 1];
dp[1] = 0;
pil.addLine(PII(-2ll * h[1], 1ll * h[1] * h[1] - pf[1]), 0);
FORW(i, 2, n, 1) {
int64 x = off.query(h[i]);
for(auto& u : pil.lines) x = min(x, u.fi * h[i] + u.se);
dp[i] = x + h[i] * h[i] + pf[i - 1];
pil.addLine(PII(-2ll * h[i], 1ll * h[i] * h[i] - pf[i] + dp[i]), 0);
if (sz(pil.lines) >= sqN) Clear_Stack();
}
cout << dp[n] << endl;
return 0; }
Compilation message
building.cpp: In function 'int main(int, char**)':
building.cpp:30:43: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | #define hardio(name) freopen(name".inp","r",stdin), freopen(name".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
building.cpp:88:5: note: in expansion of macro 'hardio'
88 | hardio("input");
| ^~~~~~
building.cpp:30:74: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | #define hardio(name) freopen(name".inp","r",stdin), freopen(name".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
building.cpp:88:5: note: in expansion of macro 'hardio'
88 | hardio("input");
| ^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |