답안 #288330

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
288330 2020-09-01T12:18:51 Z someone_aa Discharging (NOI20_discharging) C++17
100 / 100
968 ms 68144 KB
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define ld long double
#define pii pair<int,int>
#define pll pair<ll, ll>
#define sz(x) int(x.size())
using namespace std;
const int maxn = 1000100;
const ll inf = 1e18;
ll n, q, arr[maxn];

class node {
public:
	ll val;
	ll ind;
}tree[4*maxn];

node leaf_value(ll val, ll ind) {
	return {val, ind};
}

node merge_values(node a, node b) {
	node c;

	if(a.val > b.val)
		c = a;
	else if(a.val < b.val) 
		c = b;
	else {
		c.val = a.val;
		c.ind = min(a.ind, b.ind);
	}

	return c;
}

void build(int li=0, int ri=n-1, int index=1) {
	if(li == ri) {
		tree[index] = leaf_value(arr[li], li);
	}
	else {
		int mid = (li + ri) / 2;

		build(li, mid, 2*index);
		build(mid+1, ri, 2*index+1);

		tree[index] = merge_values(tree[2*index], tree[2*index+1]);
	}
}

node empty_node;
node query(int ql, int qr, int li=0, int ri=n-1, int index=1) {
	if(li > qr || ri < ql) return empty_node;
	else if(li >= ql && ri <= qr) return tree[index];
	else {
		int mid = (li + ri) / 2;

		return merge_values(query(ql, qr, li, mid, 2*index), query(ql, qr, mid+1, ri, 2*index+1));
	}
}

struct cht {
	struct line {
		ll k, b; ld x;
		ll val; bool isquery;

		line(ll _k = 0, ll _b = 0) :
			k(_k), b(_b), x(-inf), val(0), isquery(false) {}

		ll eval(ll x) const { return k * x + b; }
		bool parallel(const line& l) const {return k == l.k; }
		ld intersection(const line& l) const {
			if(parallel(l)) return inf;
			return (1.0*l.b - b) / (1.0 * k - l.k);
		}

		bool operator <(const line& l) const {
			if(l.isquery) return x < l.val;
			else return k < l.k;
		}
	};

	set<line> hull;
	typedef set<line>::iterator iter;

	bool cPrev(iter it) { return it != hull.begin(); }
	bool cNext(iter it) { return it != hull.end() && next(it) != hull.end(); }

	bool bad(const line &l1, const line &l2, const line &l3) {
		return l1.intersection(l3) <= l1.intersection(l2);
	}
	bool bad(iter it) {
		return cPrev(it) && cNext(it) && bad(*prev(it), *it, *next(it));
	}	

	iter update(iter it) {
		if(!cPrev(it)) return it;

		ld x = it -> intersection(*prev(it));
		line tmp(*it); tmp.x = x;

		it = hull.erase(it);
		return hull.insert(it, tmp);
	}

	void addLine(ll k, ll b) {
		line l(k, b);
		iter it = hull.lower_bound(l);
		if(it != hull.end() && l.parallel(*it)) {
			if(it->b < b) it = hull.erase(it);
			else return;
		}

		it = hull.insert(it, l);
		if(bad(it)) return (void) hull.erase(it);

		while(cPrev(it) && bad(prev(it))) hull.erase(prev(it));
		while(cNext(it) && bad(next(it))) hull.erase(next(it));

		it = update(it);
		if(cPrev(it)) update(prev(it));
		if(cNext(it)) update(next(it));
	}

	ll query(ll x) {
		if(hull.empty()) return -inf;

		line tmp;
		tmp.val = x; tmp.isquery = true;
		iter it = --hull.lower_bound(tmp);
		return it->eval(x);
	}

}ds;

ll pref[maxn];
ll dp[maxn];

int main() {
	cin>>n;

	empty_node = {INT_MIN, -1};

	for(int i=0;i<n;i++) {
		cin>>arr[i];
	}

	build();
	
	ll curr = n - 1;
	ll result = 0LL;

	vector<pll> values; 
	while(curr >= 0) {
		node curr_query = query(0, curr);
		values.pb(mp(curr_query.val, curr - curr_query.ind + 1));
		curr = curr_query.ind - 1;
	}

	values.pb(mp(0, 0));
	reverse(values.begin(), values.end());
	for(int i=1;i<sz(values);i++) {
		pref[i] = pref[i-1] + values[i].second;

		dp[i] = dp[i-1] + (n - pref[i-1]) * values[i].first;

	//	cout<<values[i].first<<" "<<values[i].second<<"\n";
		
		if(ds.hull.size() > 0) dp[i] = min(dp[i], values[i].first*(n) - ds.query(-values[i].first));
		ds.addLine(-pref[i-1], -dp[i-1]);
	}

	cout<<dp[sz(values) - 1]<<"\n";

	return 0;
}

Compilation message

Discharging.cpp: In function 'int main()':
Discharging.cpp:153:5: warning: unused variable 'result' [-Wunused-variable]
  153 |  ll result = 0LL;
      |     ^~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 2 ms 544 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 2 ms 512 KB Output is correct
13 Correct 2 ms 512 KB Output is correct
14 Correct 2 ms 640 KB Output is correct
15 Correct 2 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 2 ms 544 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 2 ms 512 KB Output is correct
13 Correct 2 ms 512 KB Output is correct
14 Correct 2 ms 640 KB Output is correct
15 Correct 2 ms 512 KB Output is correct
16 Correct 734 ms 58488 KB Output is correct
17 Correct 810 ms 61264 KB Output is correct
18 Correct 610 ms 54432 KB Output is correct
19 Correct 812 ms 60880 KB Output is correct
20 Correct 815 ms 62292 KB Output is correct
21 Correct 801 ms 61136 KB Output is correct
22 Correct 752 ms 59344 KB Output is correct
23 Correct 932 ms 66588 KB Output is correct
24 Correct 859 ms 63804 KB Output is correct
25 Correct 883 ms 63932 KB Output is correct
26 Correct 968 ms 68144 KB Output is correct
27 Correct 709 ms 57168 KB Output is correct
28 Correct 848 ms 61648 KB Output is correct
29 Correct 875 ms 64444 KB Output is correct
30 Correct 937 ms 67132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 863 ms 41020 KB Output is correct
2 Correct 841 ms 40952 KB Output is correct
3 Correct 852 ms 41080 KB Output is correct
4 Correct 859 ms 40968 KB Output is correct
5 Correct 855 ms 41112 KB Output is correct
6 Correct 862 ms 40960 KB Output is correct
7 Correct 852 ms 41048 KB Output is correct
8 Correct 851 ms 41080 KB Output is correct
9 Correct 849 ms 41080 KB Output is correct
10 Correct 867 ms 41080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 2 ms 512 KB Output is correct
16 Correct 2 ms 512 KB Output is correct
17 Correct 2 ms 512 KB Output is correct
18 Correct 2 ms 512 KB Output is correct
19 Correct 2 ms 512 KB Output is correct
20 Correct 2 ms 512 KB Output is correct
21 Correct 2 ms 544 KB Output is correct
22 Correct 2 ms 512 KB Output is correct
23 Correct 2 ms 512 KB Output is correct
24 Correct 2 ms 512 KB Output is correct
25 Correct 2 ms 512 KB Output is correct
26 Correct 2 ms 512 KB Output is correct
27 Correct 2 ms 512 KB Output is correct
28 Correct 2 ms 640 KB Output is correct
29 Correct 2 ms 512 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 1 ms 384 KB Output is correct
33 Correct 1 ms 384 KB Output is correct
34 Correct 1 ms 384 KB Output is correct
35 Correct 1 ms 416 KB Output is correct
36 Correct 1 ms 384 KB Output is correct
37 Correct 2 ms 384 KB Output is correct
38 Correct 1 ms 384 KB Output is correct
39 Correct 1 ms 384 KB Output is correct
40 Correct 1 ms 384 KB Output is correct
41 Correct 1 ms 384 KB Output is correct
42 Correct 2 ms 384 KB Output is correct
43 Correct 2 ms 512 KB Output is correct
44 Correct 1 ms 384 KB Output is correct
45 Correct 2 ms 384 KB Output is correct
46 Correct 1 ms 384 KB Output is correct
47 Correct 2 ms 384 KB Output is correct
48 Correct 1 ms 384 KB Output is correct
49 Correct 1 ms 384 KB Output is correct
50 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 2 ms 512 KB Output is correct
16 Correct 2 ms 512 KB Output is correct
17 Correct 2 ms 512 KB Output is correct
18 Correct 2 ms 512 KB Output is correct
19 Correct 2 ms 512 KB Output is correct
20 Correct 2 ms 512 KB Output is correct
21 Correct 2 ms 544 KB Output is correct
22 Correct 2 ms 512 KB Output is correct
23 Correct 2 ms 512 KB Output is correct
24 Correct 2 ms 512 KB Output is correct
25 Correct 2 ms 512 KB Output is correct
26 Correct 2 ms 512 KB Output is correct
27 Correct 2 ms 512 KB Output is correct
28 Correct 2 ms 640 KB Output is correct
29 Correct 2 ms 512 KB Output is correct
30 Correct 734 ms 58488 KB Output is correct
31 Correct 810 ms 61264 KB Output is correct
32 Correct 610 ms 54432 KB Output is correct
33 Correct 812 ms 60880 KB Output is correct
34 Correct 815 ms 62292 KB Output is correct
35 Correct 801 ms 61136 KB Output is correct
36 Correct 752 ms 59344 KB Output is correct
37 Correct 932 ms 66588 KB Output is correct
38 Correct 859 ms 63804 KB Output is correct
39 Correct 883 ms 63932 KB Output is correct
40 Correct 968 ms 68144 KB Output is correct
41 Correct 709 ms 57168 KB Output is correct
42 Correct 848 ms 61648 KB Output is correct
43 Correct 875 ms 64444 KB Output is correct
44 Correct 937 ms 67132 KB Output is correct
45 Correct 863 ms 41020 KB Output is correct
46 Correct 841 ms 40952 KB Output is correct
47 Correct 852 ms 41080 KB Output is correct
48 Correct 859 ms 40968 KB Output is correct
49 Correct 855 ms 41112 KB Output is correct
50 Correct 862 ms 40960 KB Output is correct
51 Correct 852 ms 41048 KB Output is correct
52 Correct 851 ms 41080 KB Output is correct
53 Correct 849 ms 41080 KB Output is correct
54 Correct 867 ms 41080 KB Output is correct
55 Correct 1 ms 384 KB Output is correct
56 Correct 1 ms 384 KB Output is correct
57 Correct 1 ms 384 KB Output is correct
58 Correct 1 ms 384 KB Output is correct
59 Correct 1 ms 384 KB Output is correct
60 Correct 1 ms 416 KB Output is correct
61 Correct 1 ms 384 KB Output is correct
62 Correct 2 ms 384 KB Output is correct
63 Correct 1 ms 384 KB Output is correct
64 Correct 1 ms 384 KB Output is correct
65 Correct 1 ms 384 KB Output is correct
66 Correct 1 ms 384 KB Output is correct
67 Correct 2 ms 384 KB Output is correct
68 Correct 2 ms 512 KB Output is correct
69 Correct 1 ms 384 KB Output is correct
70 Correct 2 ms 384 KB Output is correct
71 Correct 1 ms 384 KB Output is correct
72 Correct 2 ms 384 KB Output is correct
73 Correct 1 ms 384 KB Output is correct
74 Correct 1 ms 384 KB Output is correct
75 Correct 2 ms 384 KB Output is correct
76 Correct 595 ms 47196 KB Output is correct
77 Correct 596 ms 46968 KB Output is correct
78 Correct 554 ms 46668 KB Output is correct
79 Correct 586 ms 46712 KB Output is correct
80 Correct 611 ms 47392 KB Output is correct
81 Correct 556 ms 46712 KB Output is correct
82 Correct 548 ms 46456 KB Output is correct
83 Correct 596 ms 47096 KB Output is correct
84 Correct 576 ms 46936 KB Output is correct
85 Correct 605 ms 47192 KB Output is correct
86 Correct 552 ms 46584 KB Output is correct
87 Correct 546 ms 46456 KB Output is correct
88 Correct 572 ms 46712 KB Output is correct
89 Correct 558 ms 46584 KB Output is correct
90 Correct 551 ms 46584 KB Output is correct
91 Correct 534 ms 46296 KB Output is correct
92 Correct 889 ms 63952 KB Output is correct
93 Correct 819 ms 61392 KB Output is correct
94 Correct 521 ms 45940 KB Output is correct
95 Correct 616 ms 47352 KB Output is correct
96 Correct 538 ms 46328 KB Output is correct
97 Correct 561 ms 46604 KB Output is correct
98 Correct 578 ms 46712 KB Output is correct
99 Correct 561 ms 46432 KB Output is correct
100 Correct 555 ms 46584 KB Output is correct