Submission #934309

# Submission time Handle Problem Language Result Execution time Memory
934309 2024-02-27T06:59:09 Z Jawad_Akbar_JJ Building Bridges (CEOI17_building) C++17
30 / 100
287 ms 11724 KB
#include <iostream>
 
using namespace std;
#define int long long
 
const int N = 1e6 + 500;
int dp[N];
int h[N];
int pre[N];
bool seen[N];
int val[N];

int dist(int i,int j){
	return (h[i] - h[j]) * (h[i] - h[j]);
}
int point(int i){
	return pre[i] - pre[i-1];
}

void sub1(int n){
	dp[1] = pre[n];
	for (int i=2;i<=n;i++){
		dp[i] = 1e17;
		for (int j=1;j<i;j++)
			dp[i] = min(dp[i],dp[j] + dist(i,j) - point(i));
	}
	cout<<dp[n]<<endl;
	exit(0);
}


void sub2(int n){
	dp[1] = pre[n];
	int ans = dist(1,n) + pre[n];

	for (int i=2;i<n;i++)
		ans = min(ans,pre[n] + dist(1,i) + dist(i,n) - point(i));

	for (int i=2;i<=n;i++)
		dp[i] = pre[n] - point(i) + dist(i,1);
	for (int i=2;i<n;i++){
		int v = -point(i) + dist(i,n);
		for (int j=max(h[i]-1000,0ll);j<=h[i] + 1000;j++)
			if (seen[j])
				ans = min(ans,v + val[j] + (h[i] - j) * (h[i] - j));
		if (!seen[h[i]])
			val[h[i]] = dp[i];
		seen[h[i]] = true;
		val[h[i]] = min(val[h[i]],dp[i]);
	}
	cout<<ans<<endl;
	exit(0);
}
signed  main(){
	int n;
	cin>>n;
 
	for (int i=1;i<=n;i++)
		cin>>h[i];
	for (int i=1;i<=n;i++){
		cin>>pre[i];
		pre[i] *= (i != 1 and i != n);
		pre[i] += pre[i-1];
	}
 
	if (n <= 1000)
		sub1(n);
 	sub2(n);
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 2 ms 6492 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 287 ms 11472 KB Output is correct
2 Correct 271 ms 11712 KB Output is correct
3 Correct 257 ms 11724 KB Output is correct
4 Correct 218 ms 11612 KB Output is correct
5 Incorrect 240 ms 11596 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 2 ms 6492 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 287 ms 11472 KB Output is correct
7 Correct 271 ms 11712 KB Output is correct
8 Correct 257 ms 11724 KB Output is correct
9 Correct 218 ms 11612 KB Output is correct
10 Incorrect 240 ms 11596 KB Output isn't correct
11 Halted 0 ms 0 KB -