답안 #684652

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
684652 2023-01-22T09:10:15 Z pragmatist Money (IZhO17_money) C++17
45 / 100
453 ms 262144 KB
/*
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC target ("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
*/

#include<bits/stdc++.h>
 
#define sz(v) (int)v.size()
#define ll long long
#define pb push_back
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define nl "\n"

using namespace std;

const int N = (int)1e6 + 7; // make sure this is right
const int M = (int)1e7 + 7;
const int inf = (int)2e9 + 7;
const ll INF = (ll)3e18 + 7; 
const ll MOD = (ll)1e9 + 7; // make sure this is right

bool bit(int x, int i) {
	return x >> i & 1;
}

int sum(int x, int y) {
	x += y;
	if(x >= MOD) x -= MOD;
	return x;
}

int n, a[N], dp[N], p[N], mx[N], mn[N];
int timer, tree[N], t[M], L[M], R[M], lim = 1e6;

void build(int v, int tl, int tr) {
	if(tl == tr) {
		return;
	}
	int mid = (tl + tr) >> 1;
	L[v] = ++timer;
	build(L[v], tl, mid);
	R[v] = ++timer;
	build(R[v], mid + 1, tr);
}

void upd(int prev, int v, int tl, int tr, int id) {
	if(tl == tr) {
		t[v] = t[prev] + 1;
		return;
	}
	int mid = (tl + tr) >> 1;
	if(id <= mid) {
		L[v] = ++timer;
		R[v] = R[prev];
		upd(L[prev], L[v], tl, mid, id); 
	} else {
		R[v] = ++timer;
		L[v] = L[prev];
		upd(R[prev], R[v], mid + 1, tr, id);
	}
	t[v] = t[L[v]] + t[R[v]];
} 

int get(int v, int tl, int tr, int id) {
	if(tl == tr) {
		return tl;
	}	
	int mid = (tl + tr) >> 1;
	if(t[L[v]] >= id) return get(L[v], tl, mid, id);
	else return get(R[v], mid + 1, tr, id - t[L[v]]);
}

int get(int i, int j) {	
	return get(tree[i], 1, lim, j);
}

void compress() {
	vector<int> v;
	for(int i = 1; i <= n; ++i) {
		v.pb(a[i]);
	}	
	sort(all(v));
	v.erase(unique(all(v)), v.end());
	for(int i = 1; i <= n; ++i) {
		a[i] = lower_bound(all(v), a[i]) - v.begin() + 1;
	}
}

void solve() {
	cin >> n;
	for(int i = 1; i <= n; ++i) {
		cin >> a[i];
	}
	compress();

	mn[0] = inf;
	mx[0] = -inf;
	for(int i = 1; i <= n; ++i) {
		mn[i] = min(mn[i - 1], a[i]);
		mx[i] = max(mx[i - 1], a[i]);
		if(i > 1) p[i] = p[i - 1] + (a[i - 1] <= a[i]);
	}
	lim = mx[n];
	
	tree[0] = ++timer;
	build(1, 1, lim);
	for(int i = 1; i <= n; ++i) {
		tree[i] = ++timer;
		upd(tree[i - 1], tree[i], 1, lim, a[i]);
	}
	int j = 1;
	for(int i = 1; i <= n; ++i) {
	    dp[i] = inf;
	    while(j <= i) {
	    	if(p[i] - p[j] != i - j) {
	    	    j++;
	    		continue;
	    	}
        	bool can = (a[j] >= mx[j - 1] || a[i] <= mn[j - 1]);
        	
        	int l = 1, r = j - 2, k = -1;
        	while(l <= r) {
        		int mid = (l + r) >> 1;
        		if(a[i] <= get(j - 1, mid + 1)) {
        			k = mid;
        			r = mid - 1;
        		} else {
        			l = mid + 1;
        		}
        	}
        	if(k != -1) {
        		can |= (a[j] >= get(j - 1, k));
        	}
        	if(can) {
        		dp[i] = min(dp[i], dp[j - 1] + 1);
        		break;
        	} else {
        		j++;
        	}
		}				
	}
	cout << dp[n];
}

signed main() {	
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	int test = 1;
	//cin >> test;
	for(int i = 1; i <= test; ++i) {
	 	//cout << "Case #" << i << ": ";
		solve();
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 0 ms 412 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 0 ms 412 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 190 ms 30876 KB Output is correct
40 Correct 453 ms 66372 KB Output is correct
41 Correct 137 ms 25792 KB Output is correct
42 Correct 201 ms 30488 KB Output is correct
43 Correct 142 ms 21888 KB Output is correct
44 Correct 453 ms 77672 KB Output is correct
45 Correct 417 ms 73604 KB Output is correct
46 Correct 445 ms 77732 KB Output is correct
47 Correct 232 ms 54340 KB Output is correct
48 Correct 336 ms 62008 KB Output is correct
49 Runtime error 446 ms 262144 KB Execution killed with signal 11
50 Halted 0 ms 0 KB -