Submission #1152115

#TimeUsernameProblemLanguageResultExecution timeMemory
1152115SmuggingSpunBuilding Bridges (CEOI17_building)C++20
100 / 100
66 ms64072 KiB
#include<bits/stdc++.h>
#define taskname "A"
using namespace std;
typedef long long ll;
const ll INF = 1e18;
template<class T>void minimize(T& a, T b){
	if(a > b){
		a = b;
	}
}
ll square(int x){
	return 1LL * x * x;
}
int n;
namespace sub1{
	void solve(){
		vector<int>h(n + 1), w(n + 1);
		for(int i = 1; i <= n; i++){
			cin >> h[i];
		}
		for(int i = 1; i <= n; i++){
			cin >> w[i];
		}
		vector<ll>dp(n + 1, INF);
		dp[1] = 0;
		for(int i = 2; i <= n; i++){
			ll sum = 0;
			for(int j = i - 1; j > 0; sum += w[j--]){
				minimize(dp[i], dp[j] + square(h[i] - h[j]) + sum);
			}
		}
		cout << dp[n];
	}
}
namespace sub23{
	const int lim = 1e5 + 5;
	const int LIM = 1e6 + 5;
	int h[lim];
	ll w[lim];
	pair<int, ll>lichao[LIM << 2];
	ll f(int x, const pair<int, ll>& line){
		return 1LL * line.first * x + line.second;
	}
	void update(int id, int l, int r, pair<int, ll>line){
		if(l == r){
			if(f(l, line) < f(l, lichao[id])){
				lichao[id] = line;
			}
			return;
		}
		int m = (l + r) >> 1;
		if(f(m, line) < f(m, lichao[id])){
			swap(line, lichao[id]);
		}
		if(f(l, line) < f(l, lichao[id])){
			update(id << 1, l, m, line);
		}
		else if(f(r, line) < f(r, lichao[id])){
			update(id << 1 | 1, m + 1, r, line);
		}
	}
	ll get(int id, int l, int r, int p){
		if(l == r){
			return f(l, lichao[id]);
		}
		int m = (l + r) >> 1;
		return min(f(p, lichao[id]), m < p ? get(id << 1 | 1, m + 1, r, p) : get(id << 1, l, m, p));
	}
	void solve(){
		fill(lichao, lichao + (LIM << 2), make_pair(0, INF));
		for(int i = 1; i <= n; i++){
			cin >> h[i];
		}
		w[0] = 0;
		for(int i = 1; i <= n; i++){
			cin >> w[i];
			w[i] += w[i - 1];
		}
		update(1, 0, LIM - 1, make_pair(-(h[1] << 1), square(h[1]) - w[1]));
		for(int i = 2; i < n; i++){
			ll dp = get(1, 0, LIM - 1, h[i]) + w[i - 1] + square(h[i]);
			update(1, 0, LIM - 1, make_pair(-(h[i] << 1), square(h[i]) + dp - w[i]));
		}
		cout << get(1, 0, LIM - 1, h[n]) + w[n - 1] + square(h[n]);
	}
}
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	if(fopen(taskname".inp", "r")){
		freopen(taskname".inp", "r", stdin);
	}
	cin >> n;
	if(n <= 1000){
		sub1::solve();
	}
	else{
		sub23::solve();
	}
}

Compilation message (stderr)

building.cpp: In function 'int main()':
building.cpp:90:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   90 |                 freopen(taskname".inp", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...