我是靠谱客的博主 端庄黄蜂,最近开发中收集的这篇文章主要介绍Gym - 101466 J. Jeronimo's List 桶排序,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

J. Jeronimo's List
time limit per test
2.0 s
memory limit per test
1024 MB
input
standard input
output
standard output

Jeronimo the bear loves numbers and he is planning to write n numbers in his notebook.

After writing the first m numbers, Jeronimo felt that he was spending a lot of time thinking new numbers, so he wrote the next n - mmissing numbers as the sum modulo 3 × 107 of the numbers in the i - m and i - m + 1 positions for m < i ≤ n

While Jeronimo was writing, his sister Lupe arrived and asked him q questions. The i - th question consist of a number bi, Jeronimo has to say what would be the number in the position bi if all the numbers were sorted in ascending order. Jeronimo wants to answer each question as soon as possible but he spends a lot of time counting so he ask your help.

Input

The first line of the input has three integers n (3 ≤ n ≤ 3 × 107)m (3 ≤ m ≤ min(100, n)) and q (1 ≤ q ≤ 10000).

The second line contains m numbers a1, a2, ..., am(0 ≤ ai < 3 × 107), The first m numbers that Jeronimo wrote.

The third line contains q questions b1, b2, ..., bq (1 ≤ bi ≤ n)

Output

Print q lines. The i - th line must be the answer of the i - th question made by Lupe.

Examples
input
6 3 6
1 2 3
1 2 3 4 5 6
output
1
2
3
3
5
6
input
10 4 3
1 2 9 10
1 5 10
output
1
10
30

Source

Gym - 101466J

2017 ACM-ICPC, Universidad Nacional de Colombia Programming Contest

My Solution

题意:一共n个数字(3<=n<=3e7,  0<=ai<3e7 ),给出前面的m个(3<=m<=min(100, n)),a[i] = (a[i-m] + a[i-m+1]) % MOD,q个询问(1<=q<=1e4),询问a[1,n]里的从小到大第bi大。

桶排序

先按照要求用a[1,m]构造出a[1,n],然后这里n == 3e7,所以如果采用快速的比较排序算法如归并排序快速排序的时间复杂度是O(nlogn)会超时,因此我们想到可以使用线性排序方法桶排序(或者说计数排序),建一个数组cnt[3e7+8],扫一遍a数组,对于每一个ai,cnt[a[i]]++,最后扫一遍cnt数组,就可以把ai排好序了。

for(i = 0; i < MOD; i++){
        while(cnt[i]){
            a[ptr] = i;
            ptr++;
            cnt[i]--;
        }

    }

时间复杂度 O(n)

空间复杂度 O(n)

#include <iostream>
#include <cstdio>

using namespace std;
typedef long long LL;
const int MAXN = 3e7 + 8;
const int MOD = 3e7;
int cnt[MAXN], a[MAXN];

int main()
{
    #ifdef LOCAL
    freopen("j.txt", "r", stdin);
    //freopen("j.out", "w", stdout);
    int T = 4;
    while(T--){
    #endif // LOCAL
    //ios::sync_with_stdio(false); cin.tie(0);

    int n, m, q, i, x;
    //cin >> n >> m >> q;
    scanf("%d%d%d", &n, &m, &q);
    for(i = 1; i <= m; i++) scanf("%d", &a[i]); //cin >> a[i];
    for(i = m+1; i <= n; i++){
        a[i] = a[i-m] + a[i-m+1] - (a[i-m] + a[i-m+1]) / MOD * MOD;
    }
    for(i = 1; i <= n; i++){
        cnt[a[i]]++;
    }
    int ptr = 0;
    for(i = 0; i < MOD; i++){
        while(cnt[i]){
            a[ptr] = i;
            ptr++;
            cnt[i]--;
        }
    }
    for(i = 0; i < q; i++){
        //cin >> x;
        scanf("%d", &x);
        //cout << a[x-1] << "n";
        printf("%dn", a[x-1]);
    }



    #ifdef LOCAL
    cout << endl;
    }
    #endif // LOCAL
    return 0;
}


  Thank you!

                                                                                                                                            ------from ProLights

最后

以上就是端庄黄蜂为你收集整理的Gym - 101466 J. Jeronimo's List 桶排序的全部内容,希望文章能够帮你解决Gym - 101466 J. Jeronimo's List 桶排序所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(72)

评论列表共有 0 条评论

立即
投稿
返回
顶部