답안 #498709

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
498709 2021-12-26T08:41:13 Z jiahng Discharging (NOI20_discharging) C++14
47 / 100
894 ms 1048580 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define int ll
typedef pair<int,int> pi;
typedef vector <ll> vi;
typedef vector <pi> vpi;
typedef pair<pi, ll> pii;
typedef set <ll> si;
typedef long double ld;
#define f first
#define s second
#define mp make_pair
#define FOR(i,s,e) for(int i=s;i<=int(e);++i)
#define DEC(i,s,e) for(int i=s;i>=int(e);--i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i,x) for (auto i: x)
#define mem(x,i) memset(x,i,sizeof x)
#define fast ios_base::sync_with_stdio(false),cin.tie(0)
#define MOD 1000000007
#define maxn 1000010
#define INF (ll)1e18

int N;
int dp[maxn], T[maxn];
int f(pi line, int x){
	if (line == pi(-1,-1)) return INF;
	return line.f * x + line.s;
}

ld intersect(pi line1, pi line2){
	return (line2.s - line1.s) / (line1.f - line2.f);
}

struct node{
	int s,e,m;
	node *l,*r;
	pi val = pi(-1,-1);
	
	node (int ss,int ee){
		s = ss; e = ee; m = (s + e) / 2;
	}
	void create_all(){
		if (s != e){
			l = new node(s,m); r = new node(m+1,e);
			l->create_all(); r->create_all();
		}
	}
		
	void upd(pi line, node *prev){
		val = prev->val;
		
		if (s == e){
			if (f(line, s) < f(val, s)) val = line;
			return;
		}
		if (val == pi(-1,-1)){
			val = line; l = prev->l; r = prev->r;
		}else if (intersect(line, val) < m){
			if (f(line, m) < f(val, m)) swap(val, line);
			r = prev->r;
			l = new node(s,m);
			l->upd(line, prev->l);
		}else{
			if (f(line, m) < f(val, m)) swap(val, line);
			l = prev->l;
			r = new node(m+1,e);
			r->upd(line, prev->r);
		}
	}
	int qry(int x){
		if (s == e) return f(val, x);
		if (x > m) return min(f(val, x), r->qry(x));
		else return min(f(val, x), l->qry(x));
	}
};
vector <node*> roots;
int suffmax[maxn];

int32_t main(){
    fast;
    
	cin >> N;
	FOR(i,1,N) cin >> T[i];
	DEC(i,N,1) suffmax[i] = max(suffmax[i+1], T[i]);
	stack <pi> st;
	node *root = new node(1,N);
	root->create_all();
	roots.pb(root);
	
	dp[N+1] = 0;
	DEC(i,N,1){
		//~ int mx = -1;
		//~ dp[i] = INF;
		//~ FOR(k,i,N) {
			//~ mx = max(mx, T[k]);
			//~ if (k == N || T[k+1] > mx) dp[i] = min(dp[i], dp[k+1] + mx * (N - i + 1));
		//~ }
		while (!st.empty() && st.top().f <= T[i]){
			st.pop();
			if (!st.empty()){
				roots.pop_back();
			}
		}
		
		if (!st.empty()){
			node *newroot = new node(1,N);
			newroot->upd(pi(T[i],dp[st.top().s]), roots.back());
			roots.pb(newroot);
		}
		st.push(pi(T[i], i));
		
		dp[i] = suffmax[i] * (N - i + 1);
		if (!roots.empty()) dp[i] = min(dp[i], roots.back()->qry(N-i+1));
	}
	//~ FOR(i,1,N) cout << dp[i] << ' ';
	//~ cout << '\n';
	cout << dp[1];
	
	
}



# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 328 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1612 KB Output is correct
2 Correct 2 ms 1556 KB Output is correct
3 Correct 2 ms 1484 KB Output is correct
4 Correct 2 ms 1484 KB Output is correct
5 Correct 2 ms 1612 KB Output is correct
6 Correct 2 ms 1492 KB Output is correct
7 Correct 2 ms 1484 KB Output is correct
8 Correct 2 ms 1484 KB Output is correct
9 Correct 2 ms 1612 KB Output is correct
10 Correct 1 ms 1484 KB Output is correct
11 Correct 2 ms 1612 KB Output is correct
12 Correct 2 ms 1484 KB Output is correct
13 Correct 1 ms 1484 KB Output is correct
14 Correct 2 ms 1616 KB Output is correct
15 Correct 2 ms 1484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1612 KB Output is correct
2 Correct 2 ms 1556 KB Output is correct
3 Correct 2 ms 1484 KB Output is correct
4 Correct 2 ms 1484 KB Output is correct
5 Correct 2 ms 1612 KB Output is correct
6 Correct 2 ms 1492 KB Output is correct
7 Correct 2 ms 1484 KB Output is correct
8 Correct 2 ms 1484 KB Output is correct
9 Correct 2 ms 1612 KB Output is correct
10 Correct 1 ms 1484 KB Output is correct
11 Correct 2 ms 1612 KB Output is correct
12 Correct 2 ms 1484 KB Output is correct
13 Correct 1 ms 1484 KB Output is correct
14 Correct 2 ms 1616 KB Output is correct
15 Correct 2 ms 1484 KB Output is correct
16 Runtime error 894 ms 1048580 KB Execution killed with signal 9
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 338 ms 158664 KB Output is correct
2 Correct 292 ms 158712 KB Output is correct
3 Correct 317 ms 158700 KB Output is correct
4 Correct 305 ms 158612 KB Output is correct
5 Correct 375 ms 158628 KB Output is correct
6 Correct 328 ms 158668 KB Output is correct
7 Correct 333 ms 158688 KB Output is correct
8 Correct 376 ms 158612 KB Output is correct
9 Correct 340 ms 158576 KB Output is correct
10 Correct 370 ms 158676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 328 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 328 KB Output is correct
15 Correct 3 ms 1612 KB Output is correct
16 Correct 2 ms 1556 KB Output is correct
17 Correct 2 ms 1484 KB Output is correct
18 Correct 2 ms 1484 KB Output is correct
19 Correct 2 ms 1612 KB Output is correct
20 Correct 2 ms 1492 KB Output is correct
21 Correct 2 ms 1484 KB Output is correct
22 Correct 2 ms 1484 KB Output is correct
23 Correct 2 ms 1612 KB Output is correct
24 Correct 1 ms 1484 KB Output is correct
25 Correct 2 ms 1612 KB Output is correct
26 Correct 2 ms 1484 KB Output is correct
27 Correct 1 ms 1484 KB Output is correct
28 Correct 2 ms 1616 KB Output is correct
29 Correct 2 ms 1484 KB Output is correct
30 Correct 1 ms 968 KB Output is correct
31 Correct 2 ms 956 KB Output is correct
32 Correct 2 ms 972 KB Output is correct
33 Correct 2 ms 976 KB Output is correct
34 Correct 1 ms 972 KB Output is correct
35 Correct 1 ms 972 KB Output is correct
36 Correct 1 ms 984 KB Output is correct
37 Correct 1 ms 976 KB Output is correct
38 Correct 2 ms 1024 KB Output is correct
39 Correct 2 ms 972 KB Output is correct
40 Correct 2 ms 972 KB Output is correct
41 Correct 2 ms 960 KB Output is correct
42 Correct 2 ms 972 KB Output is correct
43 Correct 3 ms 1616 KB Output is correct
44 Correct 1 ms 976 KB Output is correct
45 Correct 2 ms 972 KB Output is correct
46 Correct 2 ms 972 KB Output is correct
47 Correct 1 ms 972 KB Output is correct
48 Correct 1 ms 984 KB Output is correct
49 Correct 2 ms 972 KB Output is correct
50 Correct 1 ms 972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 328 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 328 KB Output is correct
15 Correct 3 ms 1612 KB Output is correct
16 Correct 2 ms 1556 KB Output is correct
17 Correct 2 ms 1484 KB Output is correct
18 Correct 2 ms 1484 KB Output is correct
19 Correct 2 ms 1612 KB Output is correct
20 Correct 2 ms 1492 KB Output is correct
21 Correct 2 ms 1484 KB Output is correct
22 Correct 2 ms 1484 KB Output is correct
23 Correct 2 ms 1612 KB Output is correct
24 Correct 1 ms 1484 KB Output is correct
25 Correct 2 ms 1612 KB Output is correct
26 Correct 2 ms 1484 KB Output is correct
27 Correct 1 ms 1484 KB Output is correct
28 Correct 2 ms 1616 KB Output is correct
29 Correct 2 ms 1484 KB Output is correct
30 Runtime error 894 ms 1048580 KB Execution killed with signal 9
31 Halted 0 ms 0 KB -