zoukankan      html  css  js  c++  java
  • Just h-index

    Just h-index

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)
    Total Submission(s): 1778    Accepted Submission(s): 793


    Problem Description
    The h-index of an author is the largest h where he has at least h papers with citations not less than h.

    Bobo has published n papers with citations a1,a2,,an respectively.
    One day, he raises q questions. The i-th question is described by two integers li and ri, asking the h-index of Bobo if has *only* published papers with citations ali,ali+1,,ari.
     
    Input
    The input consists of several test cases and is terminated by end-of-file.

    The first line of each test case contains two integers n and q.
    The second line contains n integers a1,a2,,an.
    The i-th of last q lines contains two integers li and ri.
     
    Output
    For each question, print an integer which denotes the answer.

    ## Constraint

    1n,q105
    1ain
    1lirin
    * The sum of n does not exceed 250,000.
    * The sum of q does not exceed 250,000.
     
    Sample Input
    5 3 1 5 3 2 1 1 3 2 4 1 5 5 1 1 2 3 4 5 1 5
     
    Sample Output
    2 2 2 3
     
    Source
    #include <bits/stdc++.h>
    
    typedef long long ll;
    using namespace std;
    const int maxn = 1e5 + 10;
    
    int n, q, c[maxn];
    vector<int> e;
    int root[maxn * 40], sum[maxn * 40], tot;
    int ls[maxn * 40], rs[maxn * 40];
    
    inline void init() {
        e.clear();
        memset(root, 0, sizeof(root));
        memset(ls, 0, sizeof(ls));
        memset(rs, 0, sizeof(rs));
        tot = 0;
    }
    
    inline void build(int &rt, int l, int r) {
        rt = ++tot;
        sum[rt] = 0;
        if (l == r)return;
        int mid = l + r >> 1;
        build(ls[rt], l, mid);
        build(rs[rt], mid + 1, r);
    }
    
    inline void update(int l, int r, int pre, int &rt, int pos) {
        rt = ++tot;
        ls[rt] = ls[pre];
        rs[rt] = rs[pre];
        sum[rt] = sum[pre] + 1;
        if (l == r)return;
        int mid = l + r >> 1;
        if (pos <= mid) {
            update(l, mid, ls[pre], ls[rt], pos);
        } else {
            update(mid + 1, r, rs[pre], rs[rt], pos);
        }
    }
    
    inline int query(int l, int r, int pre, int &rt, int pos) {
        if (l == r)return l;
        int mid = l + r >> 1;
        int cur = sum[ls[rt]] - sum[ls[pre]];
        if (cur >= pos) {
            return query(l, mid, ls[pre], ls[rt], pos);
        } else {
            return query(mid + 1, r, rs[pre], rs[rt], pos - cur);
        }
    }
    
    int main() {
    #ifndef ONLINE_JUDGE
        freopen("1.txt", "r", stdin);
    #endif
        while (scanf("%d%d", &n, &q) != EOF) {
            init();
            for (register int i = 1; i <= n; ++i) {
                scanf("%d", &c[i]);
                e.emplace_back(c[i]);
            }
            sort(e.begin(), e.end());
            e.erase(unique(e.begin(), e.end()), e.end());
            build(root[0], 1, n);
    
            for (register int i = 1; i <= n; ++i) {
                c[i] = lower_bound(e.begin(), e.end(), c[i]) - e.begin() + 1;
            }
            for (register int i = 1; i <= n; ++i) {
                update(1, n, root[i - 1], root[i], c[i]);
            }
            //cerr<<"***"<<endl;
            int l, r;
            while (q--) {
                scanf("%d%d", &l, &r);
                int mx = r - l + 1;
                int L = 1, R = mx, res = 0;
                while (L <= R) {
                    int mid = L + R >> 1;
                    int rk = mx - mid + 1;
                    int id = query(1, n, root[l - 1], root[r], rk);
                    if (e[id - 1] >= mid) {
                        res = mid;
                        L = mid + 1;
                    } else {
                        R = mid - 1;
                    }
                }
                printf("%d
    ", res);
            }
        }
        return 0;
    }
  • 相关阅读:
    [Maid] Write Tasks in Markdown with Maid
    [React] Preview and edit a component live with React Live
    [React] Build a slide deck with mdx-deck using Markdown + React
    [React] Spread Component Props in JSX with React
    重载new delete操作符是怎么调用的
    oracle如何设置show parameter显示隐含参数
    Google用户登录界面 Android实现
    Jquery 动态生成表单 并将表单数据 批量通过Ajax插入到数据库
    消息机4_B
    jQuery中对 input 控件的操作
  • 原文地址:https://www.cnblogs.com/czy-power/p/11450429.html
Copyright © 2011-2022 走看看