题解 | 奶茶店特调

奶茶店特调

https://www.nowcoder.com/practice/2b4005eecef849f6998953db5c9bfe8b

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;

int n, m, a[N], pre[N];

int main() {
    ios :: sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> n;
    for(int i=0; i<n; i++) cin >> a[i], pre[i] = i - 1;
    cin >> m;
    while( m -- ) {
        int u; cin >> u;
        int p = pre[u];
        while(p != -1 && a[p] == a[u]) {
            a[u] ++;
            pre[u] = pre[p];
            p = pre[p];
        }
    }
    int res = 0, p = n - 1;
    while(p != -1) {
        res ++;
        p = pre[p];
    }
    cout << res;
    return 0;
}

用类似链表的方式处理,感觉时间复杂度是O(N)

全部评论

相关推荐

牛客44320985...:你的当务之急是把这个糖的要死的沟槽ide主题改了
点赞 评论 收藏
分享
03-26 12:00
已编辑
门头沟学院 Java
offer魅魔_oc...:100-200每天,你还要倒贴100
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务