/*The best way to get something done is to begin*/
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define fr(i,a,b) for(int i=a;i<=b;i++)
#define frd(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define fi first
#define se second
#define el '\n'
using namespace std;
template <class X, class Y> bool minimize(X &x, const Y &y) {if (x > y) {x = y;return true;} else return false;}
template <class X, class Y> bool maximize(X &x, const Y &y) {if (x < y) {x = y;return true;} else return false;}
/* Author: Huynh Quoc Luat */
/*Khai Bao*/
const long long inf=1e18;
const int oo=1e9;
const int LO=21;
const int CH=27;
const int N=1e5+5;
const int sm=1e9+7;
int n;
int h[N],w[N];
//void add(int &x,const int &y){x+=y;if(x>=sm)x-=sm;}
//void sub(int &x,const int &y){x-=y;if(x<0)x+=sm;}
void read()
{
cin >> n;
fr(i,1,n) cin >> h[i];
fr(i,1,n) cin >> w[i];
}
namespace sub1
{
int s[N];
const int N=1005;
int dp[N];
int cost(int i,int j){
return (h[i]-h[j])*(h[i]-h[j]);
}
void process()
{
fr(i,1,n) s[i]=s[i-1]+w[i];
memset(dp,0x3f,sizeof dp);
dp[1]=0;
fr(i,2,n){
fr(j,1,i-1){
minimize(dp[i],dp[j]+cost(i,j)+s[i-1]-s[j]);
}
}
cout << dp[n];
}
}
namespace sub2
{
int dp[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 1ll * a * x + b;}
} st[4 * N];
int s[N];
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 min(st[id](pos), get(id << 1 | 1, mid + 1, r, pos));
}
void process(){
fr(i,1,n) s[i]=s[i-1]+w[i];
upd(1,0,1e6,Line(-2*h[1],dp[1]+h[1]*h[1]+s[1]));
fr(i,2,n){
dp[i]=h[i]*h[i]+s[i-1]+get(1,0,1e6,h[i]);
upd(1,0,1e6,Line(-2*h[i],dp[i]+h[i]*h[i]+s[i]));
}
cout << dp[n];
}
}
void time() {cerr<< endl<<"Luattapcode: "<<clock()<<"ms"<<endl;}
main()
{
ios_base::sync_with_stdio(NULL);cin.tie(NULL);cout.tie(NULL);
#define TASK "qn"
if(fopen(TASK".INP","r"))
{
freopen(TASK".INP", "r", stdin);
freopen(TASK".OUT", "w", stdout);
}
int mtt=0;
int test=1;
if(mtt) cin>>test;
while(test--)
{
read();
if(n<=1000) sub1::process();
else sub2::process();
}
time();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
building.cpp:99:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
99 | main()
| ^~~~
building.cpp: In function 'int main()':
building.cpp:105:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
105 | freopen(TASK".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
building.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | freopen(TASK".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... |