蒟蒻求助啊啊

P1168 中位数

zhuyunyu @ 2018-08-11 12:02:40

#include<bits/stdc++.h>
using namespace std;
int n,k,a[100005];
int main()
{
    scanf("%d",&n);
    k=(n+1)/2;
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for(int i=1;i<=2*k-1;i=i+2)
    {
        sort(a+1,a+i+1);
        printf("%d\n",a[(i+1)/2]);
    }
    return 0;
}

by zhuyunyu @ 2018-08-11 12:03:12

思路简单、学疏才浅的我根本不会用什么线段树啊!!


by Sai0511 @ 2018-08-11 12:07:59

@zhuyunyu ...您做一次排一次序,肯定会 超时啊。。。


by _LiM @ 2018-08-11 12:45:21

标准做法应该是链表或者对顶堆


by liweiqing @ 2018-08-28 10:22:49

include<cstdio>

include<cstring>

include<vector>

include<queue>

using namespace std; struct cmp1{ bool operator () (int &a,int &b){ return a<b; } }; struct cmp2{ bool operator () (int &a,int &b){ return a>b; } }; int n,x,y; int main(){ priority_queue<int,vector<int>,cmp1>que1; priority_queue<int,vector<int>,cmp2>que2; scanf("%d",&n); scanf("%d",&x); que1.push(x); printf("%d\n",x); for(int i=3;i<=n;i+=2){ scanf("%d%d",&x,&y); if(x>y){ int t=x;x=y;y=t; } que1.push(x); que2.push(y); if(que1.top()>que2.top()){ int a=que1.top(),b=que2.top(); que1.pop(); que1.push(b); que2.pop(); que2.push(a); } printf("%d\n",que1.top()); } return 0; }


|