zoukankan      html  css  js  c++  java
  • Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3)

    题意:一共有$n$个数,$m$次询问,每次询问包括$k、pos$两个数,需要你从这$n$个数里面找出$k$个数,使得他们的总和最大,如果有多种情况,找出序号字典序最小的一组,然后输出这个序列中第$pos$个数的值。

    思路:根据贪心的思想,把这$n$个数按大到小排个序$($相同大小按下标从小到大$)$,再按照每个元素的下标依次放入主席树中,对于每次询问,查找前$k$个数中第$pos$大的数,但这个查找到的值是下标,再把这个下标对应到数值即可,体现在代码中的$b[]$数组中。

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <vector>
     
    using namespace std;
     
    const int N = 200010;
     
    struct date {
        int val, pos;
    };
     
    struct node {
        int l, r, sum;
    };
     
    int n, q, cnt, root[N], b[N];
    date a[N];
    node tree[40 * N];
     
    bool cmp(date one, date two)
    {
        if (one.val != two.val) return one.val > two.val;
        return one.pos < two.pos; 
    }
     
    void update(int l, int r, int &x, int y, int pos)
    {
        tree[++cnt] = tree[y];
        tree[cnt].sum++, x = cnt;
        if (l == r) return;
        int mid = (l + r) / 2;
        if (mid >= pos) update(l, mid, tree[x].l, tree[y].l, pos);
        else update(mid + 1, r, tree[x].r, tree[y].r, pos);
    }
     
    int query(int l, int r, int x, int y, int k)
    {
        if (l == r) return l;
        int mid = (l + r) / 2;
        int sum = tree[tree[y].l].sum - tree[tree[x].l].sum;
        if (sum >= k) return query(l, mid, tree[x].l, tree[y].l, k);
        else return query(mid + 1, r, tree[x].r, tree[y].r, k - sum);
    }
     
    int main()
    {
        scanf("%d", &n);
        for (int i = 1; i <= n; i++) {
            scanf("%d", &a[i].val);
            a[i].pos = i;
        }
        sort(a + 1, a + n + 1, cmp);
        for (int i = 1; i <= n; i++) {
            b[a[i].pos] = a[i].val;
            update(1, n, root[i], root[i - 1], a[i].pos);
        }
        scanf("%d", &q);
        while (q--) {
            int k, pos;
            scanf("%d%d", &k, &pos);
            printf("%d
    ", b[query(1, n, root[0], root[k], pos)]);
        }
        return 0;
    }
  • 相关阅读:
    linux 下查看文件个数及大小
    weblogic日志小结
    Excel数据通过plsql导入到Oracle
    Linux查看外网IP
    linux挂载/卸载优盘
    git版本回退
    linux修改文件所属用户、用户组
    retry.RetryInvocationHandler (RetryInvocationHandler.java:invoke(140))
    Hadoop切换namenode为active
    Netty使用LineBasedFrameDecoder解决TCP粘包/拆包
  • 原文地址:https://www.cnblogs.com/zzzzzzy/p/12213269.html
Copyright © 2011-2022 走看看