Submission #731071

# Submission time Handle Problem Language Result Execution time Memory
731071 2023-04-26T22:27:51 Z Doncho_Bonboncho Building Bridges (CEOI17_building) C++14
60 / 100
73 ms 4464 KB
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const int MAX_N = 1e6;
const int INF = 1e9;
const int mod = 1e9 + 7;

struct Line{
	bool type;
	ld x;
	ll m, c;
};

typedef const Line& cLine;
typedef const std::set<Line>::iterator& cIter;

std::set<Line> cht;

bool operator <( cLine a, cLine b ){
	if( a.type || b.type ) return a.x < b.x;
	return a.m > b.m;
}

bool hasPrev( cIter it ){
	return it != cht.begin();
}

bool hasNext( cIter it ){
	return it != cht.end() and next(it) != cht.end();
}

ld intersection( cIter a, cIter b ){
	return ld( a -> c - b->c ) / ( b->m - a->m );
}

void calcX( cIter it ){
	if( !hasPrev(it) ) return;
	Line l = *it;
	l.x = intersection( prev(it), it );
	cht.insert(cht.erase(it), l);	
}

bool bad( cIter it ){
	if(  hasNext(it) and next(it) -> c <= it -> c ) return true;
	return hasPrev(it) and hasNext(it) and intersection( prev(it), next(it) ) <= intersection( prev(it), it );
}

void addLine( ll m, ll c ){
	std::set<Line>::iterator it = cht.lower_bound({0, 0, m, c});
	if( it != cht.end() and m == it -> m ){
		if( it -> c <= c ) return;
		cht.erase(it);
	}

	it = cht.insert({0, 0, m, c}).first;
	if( bad( it ) ) cht.erase(it);
	else{
		while( hasPrev(it) and bad(prev(it)) ) cht.erase(prev(it));
		while( hasNext(it) and bad(next(it)) ) cht.erase(next(it));
		calcX(it);
		if( hasNext(it) ) calcX(next(it));
	}
}

ll query( ld x ){
	Line l = *prev(cht.upper_bound({1, x, 0, 0}));
	return l.m * x + l.c;
}


ll dp[MAX_N];
ll h[MAX_N];
ll w[MAX_N];
ll sumW = 0;

int main () {

	std::ios_base::sync_with_stdio(false); std::cin.tie(NULL);

	int n;
	std::cin>>n;
	for( int i=1 ; i<=n ; i++ ) std::cin>>h[i];
	for( int i=1 ; i<=n ; i++ ){ std::cin>>w[i]; sumW += w[i]; }

	dp[1] = -w[1];
	for( int i=2 ; i<=n ; i++ ){
		addLine( -2*h[i-1], dp[i-1] + h[i-1]*h[i-1] );
		dp[i] = query( h[i] ) + h[i] * h[i] - w[i];
	}

	std::cout<<dp[n] + sumW<<"\n";

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 2732 KB Output is correct
2 Correct 72 ms 2780 KB Output is correct
3 Correct 72 ms 2692 KB Output is correct
4 Correct 69 ms 2600 KB Output is correct
5 Correct 63 ms 4464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 73 ms 2732 KB Output is correct
7 Correct 72 ms 2780 KB Output is correct
8 Correct 72 ms 2692 KB Output is correct
9 Correct 69 ms 2600 KB Output is correct
10 Correct 63 ms 4464 KB Output is correct
11 Incorrect 64 ms 2636 KB Output isn't correct
12 Halted 0 ms 0 KB -