답안 #644800

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
644800 2022-09-25T09:13:00 Z ghostwriter Money (IZhO17_money) C++14
100 / 100
1379 ms 88516 KB
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) (x).bg(), (x).ed()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i))
#define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
----------------------------------------------------------------
    END OF TEMPLATE
----------------------------------------------------------------
    Tran The Bao - ghostwriter
    Training for VOI23 gold medal
----------------------------------------------------------------
    DIT ME CHUYEN BAO LOC
----------------------------------------------------------------
*/
const int oo = 1e9 + 5;
struct Node {
	int opt, left, right;
	Node() : opt(oo), left(0), right(0) {}
	Node(int opt, int left, int right) : opt(opt), left(left), right(right) {}
};
const int N = 1e6 + 5;
const int Nx2 = 2e6 + 5;
int n, a[N], r[N], d[N], root = 0, cnt = 0;
set<int> s;
Node tr[Nx2];
void upd(int &i, int l, int r, int q, int v) {
	if (!i) i = ++cnt;
	if (r < q || l > q) return;
	if (l == r) {
		tr[i].opt = min(tr[i].opt, v);
		return;
	}
	int mid = l + (r - l) / 2;
	upd(tr[i].left, l, mid, q, v);
	upd(tr[i].right, mid + 1, r, q, v);
	tr[i].opt = min(tr[tr[i].left].opt, tr[tr[i].right].opt);
}
int get(int &i, int l, int r, int ql, int qr) {
	if (!i) i = ++cnt;
	if (r < ql || l > qr) return oo;
	if (ql <= l && r <= qr) return tr[i].opt;
	int mid = l + (r - l) / 2;
	return min(get(tr[i].left, l, mid, ql, qr), get(tr[i].right, mid + 1, r, ql, qr));
}
signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    // freopen(file".inp", "r", stdin);
    // freopen(file".out", "w", stdout);
    cin >> n;
    FOR(i, 1, n) cin >> a[i];
    FOR(i, 1, n) {
    	r[i] = max(r[i - 1], i);
    	WHILE(r[i] < n && a[r[i]] <= a[r[i] + 1]) {
    		if (a[r[i]] == a[r[i] + 1]) {
    			++r[i];
    			continue;
    		}
    		bool ok1 = 0, ok2 = 0;
    		if (!s.empty() && *--s.ed() >= a[r[i]]) {
    			int v = *s.lb(a[r[i]]);
    			if (v == a[r[i]]) ok1 = 1;
    		}
    		if (!s.empty() && *--s.ed() > a[r[i]]) {
    			int v = *s.ub(a[r[i]]);
    			if (v < a[r[i] + 1]) ok2 = 1;
    		}
    		if (ok2) break;
    		if (!ok1) {
    			++r[i];
    			continue;
    		}
			if (a[r[i]] == a[i]) {
				++r[i];
				continue;
			}
			break;
    	}
    	s.insert(a[i]);
    }
    d[n] = 1;
    upd(root, 1, n, n, d[n]);
    FOS(i, n - 1, 1) {
    	if (r[i] == n) d[i] = 1;
    	else d[i] = get(root, 1, n, i + 1, r[i] + 1) + 1;
    	upd(root, 1, n, i, d[i]);
    }
    cout << d[1];
    return 0;
}
/*
6
3 6 4 5 1 2
----------------------------------------------------------------
From Benq:
    stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
        * DON'T GET STUCK ON ONE APPROACH
----------------------------------------------------------------
*/

Compilation message

money.cpp: In function 'int main()':
money.cpp:30:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
money.cpp:84:5: note: in expansion of macro 'FOR'
   84 |     FOR(i, 1, n) cin >> a[i];
      |     ^~~
money.cpp:30:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
money.cpp:85:5: note: in expansion of macro 'FOR'
   85 |     FOR(i, 1, n) {
      |     ^~~
money.cpp:31:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   31 | #define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
      |                               ^
money.cpp:116:5: note: in expansion of macro 'FOS'
  116 |     FOS(i, n - 1, 1) {
      |     ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 11 ms 23764 KB Output is correct
3 Correct 11 ms 23804 KB Output is correct
4 Correct 13 ms 23796 KB Output is correct
5 Correct 12 ms 23820 KB Output is correct
6 Correct 11 ms 23820 KB Output is correct
7 Correct 11 ms 23816 KB Output is correct
8 Correct 12 ms 23820 KB Output is correct
9 Correct 12 ms 23784 KB Output is correct
10 Correct 11 ms 23764 KB Output is correct
11 Correct 13 ms 23764 KB Output is correct
12 Correct 15 ms 23720 KB Output is correct
13 Correct 12 ms 23740 KB Output is correct
14 Correct 11 ms 23728 KB Output is correct
15 Correct 11 ms 23748 KB Output is correct
16 Correct 14 ms 23820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 11 ms 23764 KB Output is correct
3 Correct 11 ms 23804 KB Output is correct
4 Correct 13 ms 23796 KB Output is correct
5 Correct 12 ms 23820 KB Output is correct
6 Correct 11 ms 23820 KB Output is correct
7 Correct 11 ms 23816 KB Output is correct
8 Correct 12 ms 23820 KB Output is correct
9 Correct 12 ms 23784 KB Output is correct
10 Correct 11 ms 23764 KB Output is correct
11 Correct 13 ms 23764 KB Output is correct
12 Correct 15 ms 23720 KB Output is correct
13 Correct 12 ms 23740 KB Output is correct
14 Correct 11 ms 23728 KB Output is correct
15 Correct 11 ms 23748 KB Output is correct
16 Correct 14 ms 23820 KB Output is correct
17 Correct 14 ms 23764 KB Output is correct
18 Correct 11 ms 23764 KB Output is correct
19 Correct 11 ms 23740 KB Output is correct
20 Correct 11 ms 23764 KB Output is correct
21 Correct 12 ms 23764 KB Output is correct
22 Correct 13 ms 23764 KB Output is correct
23 Correct 12 ms 23720 KB Output is correct
24 Correct 11 ms 23820 KB Output is correct
25 Correct 11 ms 23744 KB Output is correct
26 Correct 11 ms 23820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 11 ms 23764 KB Output is correct
3 Correct 11 ms 23804 KB Output is correct
4 Correct 13 ms 23796 KB Output is correct
5 Correct 12 ms 23820 KB Output is correct
6 Correct 11 ms 23820 KB Output is correct
7 Correct 11 ms 23816 KB Output is correct
8 Correct 12 ms 23820 KB Output is correct
9 Correct 12 ms 23784 KB Output is correct
10 Correct 11 ms 23764 KB Output is correct
11 Correct 13 ms 23764 KB Output is correct
12 Correct 15 ms 23720 KB Output is correct
13 Correct 12 ms 23740 KB Output is correct
14 Correct 11 ms 23728 KB Output is correct
15 Correct 11 ms 23748 KB Output is correct
16 Correct 14 ms 23820 KB Output is correct
17 Correct 14 ms 23764 KB Output is correct
18 Correct 11 ms 23764 KB Output is correct
19 Correct 11 ms 23740 KB Output is correct
20 Correct 11 ms 23764 KB Output is correct
21 Correct 12 ms 23764 KB Output is correct
22 Correct 13 ms 23764 KB Output is correct
23 Correct 12 ms 23720 KB Output is correct
24 Correct 11 ms 23820 KB Output is correct
25 Correct 11 ms 23744 KB Output is correct
26 Correct 11 ms 23820 KB Output is correct
27 Correct 11 ms 23764 KB Output is correct
28 Correct 14 ms 23808 KB Output is correct
29 Correct 11 ms 23764 KB Output is correct
30 Correct 12 ms 23764 KB Output is correct
31 Correct 11 ms 23744 KB Output is correct
32 Correct 12 ms 23764 KB Output is correct
33 Correct 11 ms 23764 KB Output is correct
34 Correct 11 ms 23764 KB Output is correct
35 Correct 14 ms 23816 KB Output is correct
36 Correct 12 ms 23816 KB Output is correct
37 Correct 12 ms 23756 KB Output is correct
38 Correct 11 ms 23768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 11 ms 23764 KB Output is correct
3 Correct 11 ms 23804 KB Output is correct
4 Correct 13 ms 23796 KB Output is correct
5 Correct 12 ms 23820 KB Output is correct
6 Correct 11 ms 23820 KB Output is correct
7 Correct 11 ms 23816 KB Output is correct
8 Correct 12 ms 23820 KB Output is correct
9 Correct 12 ms 23784 KB Output is correct
10 Correct 11 ms 23764 KB Output is correct
11 Correct 13 ms 23764 KB Output is correct
12 Correct 15 ms 23720 KB Output is correct
13 Correct 12 ms 23740 KB Output is correct
14 Correct 11 ms 23728 KB Output is correct
15 Correct 11 ms 23748 KB Output is correct
16 Correct 14 ms 23820 KB Output is correct
17 Correct 14 ms 23764 KB Output is correct
18 Correct 11 ms 23764 KB Output is correct
19 Correct 11 ms 23740 KB Output is correct
20 Correct 11 ms 23764 KB Output is correct
21 Correct 12 ms 23764 KB Output is correct
22 Correct 13 ms 23764 KB Output is correct
23 Correct 12 ms 23720 KB Output is correct
24 Correct 11 ms 23820 KB Output is correct
25 Correct 11 ms 23744 KB Output is correct
26 Correct 11 ms 23820 KB Output is correct
27 Correct 11 ms 23764 KB Output is correct
28 Correct 14 ms 23808 KB Output is correct
29 Correct 11 ms 23764 KB Output is correct
30 Correct 12 ms 23764 KB Output is correct
31 Correct 11 ms 23744 KB Output is correct
32 Correct 12 ms 23764 KB Output is correct
33 Correct 11 ms 23764 KB Output is correct
34 Correct 11 ms 23764 KB Output is correct
35 Correct 14 ms 23816 KB Output is correct
36 Correct 12 ms 23816 KB Output is correct
37 Correct 12 ms 23756 KB Output is correct
38 Correct 11 ms 23768 KB Output is correct
39 Correct 294 ms 32652 KB Output is correct
40 Correct 429 ms 37504 KB Output is correct
41 Correct 225 ms 30944 KB Output is correct
42 Correct 188 ms 30168 KB Output is correct
43 Correct 133 ms 28264 KB Output is correct
44 Correct 564 ms 41280 KB Output is correct
45 Correct 573 ms 41352 KB Output is correct
46 Correct 640 ms 41424 KB Output is correct
47 Correct 585 ms 41456 KB Output is correct
48 Correct 560 ms 41432 KB Output is correct
49 Correct 819 ms 59844 KB Output is correct
50 Correct 829 ms 59892 KB Output is correct
51 Correct 830 ms 60000 KB Output is correct
52 Correct 812 ms 60044 KB Output is correct
53 Correct 822 ms 59888 KB Output is correct
54 Correct 810 ms 59712 KB Output is correct
55 Correct 1349 ms 88376 KB Output is correct
56 Correct 1354 ms 88392 KB Output is correct
57 Correct 1347 ms 88448 KB Output is correct
58 Correct 1368 ms 88412 KB Output is correct
59 Correct 1353 ms 88516 KB Output is correct
60 Correct 1338 ms 88432 KB Output is correct
61 Correct 1338 ms 88468 KB Output is correct
62 Correct 1379 ms 88432 KB Output is correct