标签:数字 cas cst pop back list str printf long
题意:给我一个奇数长度为n的序列,从左到右依次输出 1~当前技术位置的 这个子区间内的中位数。
思路
1 .法一: 维护一个最小根堆、最大根堆(注意less
#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;
}
标签:数字 cas cst pop back list str printf long
原文地址:https://www.cnblogs.com/lql-nyist/p/12743477.html