Submission #46340

# Submission time Handle Problem Language Result Execution time Memory
46340 2018-04-19T13:27:57 Z RezwanArefin01 Building Bridges (CEOI17_building) C++17
100 / 100
167 ms 18004 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii; 

const int maxn = 1e5 + 10; 
const ll isQuery = -(1ll << 62);

struct line {
	ll m, b;
	mutable function<const line*()> succ; 
	bool operator < (const line &l) const {
		if(l.b != isQuery) return m < l.m; 
		const line* s = succ(); 
		if(!s) return 0;
		return b - s -> b < (s -> m - m) * l.m;
	}	
};
struct cht : public multiset<line> {
	bool bad(iterator y) {
		auto z = next(y);
		if(y == begin()) {
			if(z == end()) return 0; 
			return y -> m == z -> m && y -> b <= z -> b;
		}
		auto x = prev(y);
		if(z == end()) return y -> m == x -> m && y -> b <= x -> b;
		return 1.0 * (z -> b - x -> b) * (x -> m - y -> m) <= 1.0 * (y -> b - x -> b) * (x -> m - z -> m);
	}
	void add(ll m, ll b) { 
		auto y = insert({-m, -b}); 
		y -> succ = [=]{ return next(y) == end() ? 0 : &*next(y); }; 
		if(bad(y)) return (void) erase(y); 
		while(next(y) != end() && bad(next(y))) erase(next(y)); 
		while(y != begin() && bad(prev(y))) erase(prev(y));
	}
	ll eval(ll x) {
		auto it = *lower_bound({x, isQuery}); 
		return -(it.m * x + it.b);
	}
} ds;
int n, h[maxn], w[maxn]; 
ll dp[maxn], sum;

int main(int argc, char const *argv[]) {
	int n; scanf("%d", &n); 
	for(int i = 1; i <= n; i++) 
		scanf("%d", &h[i]);
	for(int i = 1; i <= n; i++) 
		scanf("%d", &w[i]), sum += w[i]; 

	dp[1] = -w[1]; 
	ds.add(-2ll * h[1], dp[1] + (ll) h[1] * h[1]);
	for(int i = 2; i <= n; i++) {
		dp[i] = ds.eval(h[i]) + (ll) h[i] * h[i] - w[i];
		ds.add(-2ll * h[i], dp[i] + (ll) h[i] * h[i]);
	}

	printf("%lld\n", dp[n] + sum);
}

Compilation message

building.cpp: In function 'int main(int, const char**)':
building.cpp:47:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  int n; scanf("%d", &n); 
         ~~~~~^~~~~~~~~~
building.cpp:49:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &h[i]);
   ~~~~~^~~~~~~~~~~~~
building.cpp:51:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &w[i]), sum += w[i]; 
   ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 356 KB Output is correct
3 Correct 2 ms 412 KB Output is correct
4 Correct 3 ms 528 KB Output is correct
5 Correct 3 ms 632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 88 ms 2276 KB Output is correct
2 Correct 86 ms 2492 KB Output is correct
3 Correct 87 ms 2492 KB Output is correct
4 Correct 84 ms 2492 KB Output is correct
5 Correct 123 ms 5092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 356 KB Output is correct
3 Correct 2 ms 412 KB Output is correct
4 Correct 3 ms 528 KB Output is correct
5 Correct 3 ms 632 KB Output is correct
6 Correct 88 ms 2276 KB Output is correct
7 Correct 86 ms 2492 KB Output is correct
8 Correct 87 ms 2492 KB Output is correct
9 Correct 84 ms 2492 KB Output is correct
10 Correct 123 ms 5092 KB Output is correct
11 Correct 133 ms 5092 KB Output is correct
12 Correct 96 ms 5520 KB Output is correct
13 Correct 74 ms 6592 KB Output is correct
14 Correct 95 ms 7980 KB Output is correct
15 Correct 167 ms 18004 KB Output is correct
16 Correct 127 ms 18004 KB Output is correct
17 Correct 36 ms 18004 KB Output is correct
18 Correct 38 ms 18004 KB Output is correct