黑匣子(未完)

思路就是和《中位数》差不多

 

但是人家代码实现的方法就是比我快乐十几二十倍,就离谱好嘛

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
priority_queue<int, vector<int>, greater<int> > mi;//存储更大的部分,最小的为队首
priority_queue<int> ma;//存储更小的部分(这一部分在前面哈)
int a[N];
inline int read()
{
    char C=getchar();
    int N=0, F=1;
    while(('0' > C || C > '9') && (C != '-'))
        C=getchar();
    if(C == '-')
        F=-1, C=getchar();
    while('0' <= C && C <= '9')
        N=(N << 1)+(N << 3)+(C - 48), C=getchar();
    return F*N;
} //骗时间的读入优化 QAQ
int main()
{
    int n, m;
    n = read();
    m = read();
    //scanf("%lld %lld", &n, &m);
    for(int i = 1; i <= n; i++)
        //scanf("%lld", &a[i]);
        a[i] = read();
    int u, ai = 1;
    for(int i = 1; i <= m; i++)
    {
        u = read();
        //scanf("%lld", &u);
        for(; ai <= u; ai++)
            {
                ma.push(a[ai]);
                if(ma.size() == i)
                {
                    mi.push(ma.top());
                    ma.pop();
                }
            }
        printf("%d\n", mi.top());
        ma.push(mi.top());
        mi.pop();
    }
    return 0;
}

/*
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
priority_queue<long long, vector<long long>, greater<long long> > mi;//存储更大的部分,最小的为队首
priority_queue<long long> ma;//存储更小的部分(这一部分在前面哈)
long long a[N];
inline int read()
{
    char C=getchar();
    int N=0, F=1;
    while(('0' > C || C > '9') && (C != '-'))
        C=getchar();
    if(C == '-')
        F=-1, C=getchar();
    while('0' <= C && C <= '9')
        N=(N << 1)+(N << 3)+(C - 48), C=getchar();
    return F*N;
} //骗时间的读入优化 QAQ
int main()
{
    long long n, m;
    scanf("%lld %lld", &n, &m);
    for(long long i = 1; i <= n; i++)
        scanf("%lld", &a[i]);
    long long u, ai = 1;
    for(long long i = 1; i <= m; i++)
    {
        scanf("%lld", &u);
        for(; ai <= u; ai++)
            mi.push(a[ai]);
        ma.push(mi.top());//每次只会增加一个树
        mi.pop();
        while(!mi.empty())
        {
            int x = ma.top();
            int y = mi.top();

            if(x < y)
                break;
            ma.pop();
            ma.push(y);
            mi.pop();
            mi.push(x);
        }
        printf("%lld\n", ma.top());
    }
    return 0;
}
*/
View Code

 

posted @ 2021-04-13 20:32  bear_xin  阅读(61)  评论(0)    收藏  举报