답안 #252008

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
252008 2020-07-23T15:14:13 Z lyc Building Bridges (CEOI17_building) C++14
30 / 100
143 ms 5248 KB
#include <bits/stdc++.h>
using namespace std;

#define TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for (int i=(a);i>=(b);--i)
#define SQ(x) ((ll)(x)*(x))
using ll=long long;
using ld=long double;
using ii=pair<int,int>;

const int mxN = 1e5+5;

int N, H[mxN], W[mxN];
ll pW[mxN], dp[mxN];

struct Line {
	ll m, c;
	ll eval(ll x) { return m*x + c; }
};

struct Hull {
	Line dq[mxN];
	int s, e;
	
	inline void reset() { s = e = 0; }
	
	Hull() { reset(); }
	
	inline ld intersect(Line a, Line b) {
		return (ld)(a.c - b.c) / (b.m - a.m);
	}
	
	void add(ll m, ll b) {
		Line l = {m,b};
		while (e-s > 1 && intersect(l,dq[e-1]) <= intersect(dq[e-1],dq[e-2])) --e;
		dq[e++] = l;
	}
	
	ll query(ll x) {
		while (e-s > 1 && dq[s].eval(x) >= dq[s+1].eval(x)) ++s;
		return dq[s].eval(x);
	}
} ch;

void cdq(int l, int r) {
	if (l == r) return;
	int m = (l+r)/2;
	cdq(l,m);
	
	ch.reset();
	vector<ii> slopes, queries;
	FOR(i,l,m){
		slopes.emplace_back(-2*H[i],i);
	}
	FOR(i,m+1,r){
		queries.emplace_back(H[i],i);
	}
	sort(ALL(slopes),greater<ii>());
	sort(ALL(queries));
	
	// (H[i]-H[j])^2 + pW[i-1]-pW[j] + dp[j];
	for (auto& s : slopes) {
		int i = s.second;
		ch.add(-2*H[i], SQ(H[i]) - pW[i] + dp[i]);
	}
	for (auto& q : queries) {
		int i = q.second;
		dp[i] = min(dp[i], ch.query(H[i]) + SQ(H[i]) + pW[i-1]);
	}
	
	cdq(m+1,r);
}

int main() {
	//freopen("in.txt","r",stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    cin >> N;
    FOR(i,1,N){ cin >> H[i]; }
    FOR(i,1,N){ cin >> W[i]; pW[i] = pW[i-1] + W[i]; }
    
    dp[1] = 0;
    FOR(i,2,N) dp[i] = 1e18;
    cdq(1,N);
	cout << dp[N] << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 143 ms 5248 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Incorrect 143 ms 5248 KB Output isn't correct
7 Halted 0 ms 0 KB -