D - D HDU - 3282

D - D HDU - 3282

思路

  • 题意:给我一个奇数长度为n的序列,从左到右依次输出 1~当前技术位置的 这个子区间内的中位数。

  • 思路
    1 .法一: 维护一个最小根堆、最大根堆(注意less 在priority_queue 中的数字排列的顺序是按从头部top 按数字大小逐渐递减,而gerater 在priority_queue 则是从top按数字从小到大排序的 )

  1. 法2: 暴力水过去,输入完 n 个数之后从小到大排序,每次遍历着没个位置相应的中位数存到 ans中

代码(维护最小根对、最大根堆)

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<list>
using namespace std;
void fre() { freopen("A.txt","r",stdin), freopen("Ans.txt","w",stdout); }
#define ll long long 
const int mxn = 1e6;


priority_queue<int, vector<int>, less<int> > q1;
priority_queue<int, vector<int>, greater<int> > q2;

int main()
{
    /* fre(); */
    int t;    
    scanf("%d", &t);
    while(t --)
    {
        while(! q1.empty())
            q1.pop();
        while(! q2.empty())
            q2.pop();
        int Case, n;
        scanf("%d %d", &Case, &n);
        printf("%d %d", Case, n/2 + 1);
        int x;
        for(int i = 1; i <= n; i ++)
        {
            scanf("%d", &x);
            int to = i/2 + 1;
            if(! q1.empty() && x <= q1.top())
                q1.push(x);
            else if(! q2.empty() && x >= q2.top())
                q2.push(x);
            else if(q1.size() >= to)
                q2.push(x);
            else
                q1.push(x);


            if(i&1)
            {
                while(q1.size() > to)
                {
                    q2.push(q1.top());
                    q1.pop();
                }
                while(q1.size() < to)
                {
                    q1.push(q2.top());
                    q2.pop();
                }
                int cnt = i/2;    
                if(cnt%10 == 0)
                    printf("\n%d", q1.top());
                else
                    printf(" %d", q1.top());
            }
        }
        printf("\n");
    }



    return 0;
}

代码(暴力解)

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
void fre() { freopen("A.txt","r",stdin), freopen("Ans.txt","w",stdout); }
#define ll long long 
const int mxn = 1e6;

struct Node
{
    int x, id;
    bool operator <(const Node b) const
    {
        return x < b.x;
    }
} node[mxn];



int main()
{
    /* fre(); */
    int t;    
    scanf("%d", &t);
    while(t --)
    {
        int Case, n;
        scanf("%d %d", &Case, &n);
        for(int i = 1; i <= n; i ++)
            scanf("%d", &node[i].x), node[i].id = i;
        sort(node + 1, node + 1 + n);
        vector<int> ans;
        for(int i = 1; i <= n; i += 2)
        {
            int cnt = 0;
            for(int j = 1; ; j ++)
            {
                if(node[j].id <= i)
                    cnt ++;
                if(cnt == i/2 + 1)
                {
                    ans.push_back(node[j].x);
                    break;
                }
            }
        }

        printf("%d %d", Case, n/2 + 1);
        for(int i = 0; i < ans.size(); i ++)
        {
            if(i%10 == 0)
                printf("\n%d", ans[i]);
            else
                printf(" %d", ans[i]);
        }
        printf("\n");
    }



    return 0;
}
原创文章 220 获赞 292 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_34261446/article/details/105654412
d
<d>
d'd