牛客小白月赛11:Rinne Loves Data Structure
生活随笔
收集整理的這篇文章主要介紹了
牛客小白月赛11:Rinne Loves Data Structure
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Rinne Loves Data Structure
思路
我們插入的位置大概分了四種:
第一種
顯然我們找到比當前插入的值的pre,也就是比當前節點大的最小值。
第二種
我們只要找到當前節點的suc,也就是比當前節點小的,最大值。
第三種
我們只要找到當前節點的suc,也就是比當前節點小的,最大值。
第四種
顯然我們找到比當前插入的值的pre,也就是比當前節點大的最小值。
綜上,一,三我們可以直接去dep[now]=max(dep[pre],dep[suc])+1dep[now] = max(dep[pre], dep[suc]) + 1dep[now]=max(dep[pre],dep[suc])+1
第二種是沒有preprepre的,我們也可以直接特判,得到它的sucsucsuc,更新dep[now]=dep[suc]+1dep[now] = dep[suc] + 1dep[now]=dep[suc]+1
第四種是沒有sucsucsuc的,我們可以直接特判,然后得到它的preprepre,所以dep[now]=dep[pre]+1dep[now] = dep[pre] + 1dep[now]=dep[pre]+1
插入第一個元素的時候我們既沒有前驅,也沒有后驅,需要單獨考慮。
代碼
/*Author : lifehappy */ #pragma GCC optimize(2) #pragma GCC optimize(3) #include <bits/stdc++.h> #define mp make_pair #define pb push_back #define endl '\n'using namespace std;typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii;const double pi = acos(-1.0); const double eps = 1e-7; const int inf = 0x3f3f3f3f;inline ll read() {ll f = 1, x = 0;char c = getchar();while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();}while(c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return f * x; }void print(ll x) {if(x < 10) {putchar(x + 48);return ;}print(x / 10);putchar(x % 10 + 48); }const int N = 3e5 + 10;int dep[N], n;set<int> st;int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);int n = read();ll ans = 0;while(n--) {int x = read();if(st.size() == 0) {puts("0");st.insert(x);continue;}// auto p = lower_bound(st.begin(), st.end(), x);//用這個給tle了,不懂原理。auto p = st.lower_bound(x);if(p == st.begin()) {dep[x] = dep[*p] + 1;ans += dep[x];}else if(p == st.end()) {p--;dep[x] = dep[*p] + 1;ans += dep[x];}else {dep[x] = dep[*p] + 1;p--;dep[x] = max(dep[x], dep[*p] + 1);ans += dep[x];}st.insert(x);printf("%lld\n", ans);}return 0; }總結
以上是生活随笔為你收集整理的牛客小白月赛11:Rinne Loves Data Structure的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数列分块入门 (1 ~ 7)
- 下一篇: 现在想再弄一个无线路由器家里已经有一个无