Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) D. Dense Subsequence 贪心 + RMQ

分析:

固定长度为 m <script type="math/tex" id="MathJax-Element-26">m</script>的区间不断右移动,只要这个区间内没有一个被选的元素就选择其中一个最小的,利用ST表的RMQ来做到。
这样求出必须要选择的最大的字母的次数,然后比它小的都要选。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <stack>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
#define pr(x) cout << #x << ": " << x << "  "
#define pl(x) cout << #x << ": " << x << endl;

using namespace std;
const int maxn = int(1e5) + 8, key = 20;

pair<char, int> A[maxn];

int df(int a, int b) {
    return 31 - __builtin_clz(b - a + 1);
}

struct st {
    int n;
    pair<char, int> table[key][maxn];
    st() {}
    st(int x, pair<char, int>* a) {
        n = x;
        for (int i = 1; i <= n; ++i) table[0][i] = a[i];
        for (int i = 1; i < key; ++i)
            for (int j = 1; j + (1 << i) - 1 <= n; ++j)
                table[i][j] = min(table[i - 1][j], table[i - 1][j + (1 << (i - 1))]);
    }
    pair<int, int> gm(int a, int b) {
        int k = df(a, b);
        return min(table[k][a], table[k][b - (1 << k) + 1]);
    }
};
int n, mx;
int vis[maxn];
char str[maxn], outs[maxn];
int ct = 0;

void fun() {
    cin >> mx;
    scanf("%s", str + 1);
    n = strlen(str + 1);
    for (int i = 1; i <= n; ++i) A[i] = make_pair(str[i], -i);
    st sts(n, A);
    int l = 1, r = mx;
    char mc = 'a';
    memset(vis, 0, sizeof(vis));
    while (true) {
        char c;
        int cur;
        tie(c, cur) = sts.gm(l, r);
        cur = -cur;
        vis[cur] = true;
        mc = max(mc, c);
        outs[ct++] = c;
        if (cur + mx - 1 >= n) break;
        l = cur + 1;
        r = min(l + mx - 1, n);
    }
    for (int i = 1; i <= n; ++i)
        if (!vis[i] && str[i] < mc) outs[ct++] = str[i];
    sort(outs, outs +  ct);
    printf("%s\n", outs);
}


int main()
{
#ifdef LOCAL
    freopen("inn.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif
    fun();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值