#include<bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10 , M = 10, INF = 0x3f3f3f3f;
const double eps = 1e-8;
//#define x first
//#define y second
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
int arr[N], cnt[N], res[N];
bool st[N];
//重写堆的比较函数
struct Node1
{
int x, y;
};
struct Node2
{
int x, y;
};
bool operator < (const Node1& a, const Node1& b)
{
if(a.x != b.x) return a.x < b.x;
return a.y > b.y;
}
bool operator < (const Node2& a, const Node2& b)
{
if(a.x != b.x) return a.x > b.x;
return a.y > b.y;
}
void Solved()
{
int n;
cin >> n;
for(int i = 1; i <= n; i ++)
{
cin >> arr[i];
cnt[arr[i]] = 0;
st[arr[i]] = false;
}
for(int i = 1; i <= n; i ++)
cnt[arr[i]] ++;
priority_queue<Node2> mi;
priority_queue<Node1> mx;
int idx = 1;
for(int r = 1, l = 1; r <= n; r ++)
{
mx.push({arr[r], r}), mi.push({arr[r], r});
cnt[arr[r]] --;
if(cnt[arr[r]] > 0 || st[arr[r]]) continue;
while(!st[arr[r]])
{
if(idx % 2)
{
auto t = mx.top();
mx.pop();
if(st[t.x] || t.y < l) continue;
res[idx ++] = t.x, st[t.x] = true;
l = t.y + 1;
}
else
{
auto t = mi.top();
mi.pop();
if(st[t.x] || t.y < l) continue;
res[idx ++] = t.x, st[t.x] = true;
l = t.y + 1;
}
}
}
cout << idx - 1 << "\n";
for(int i = 1; i < idx; i ++)
cout << res[i] << " ";
cout << "\n";
}
int main()
{
/* ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0); */
int t;
cin >> t;
//t = 1;
while (t --)
{
Solved();
}
return 0;
}
D - Longest Max Min Subsequence (堆写法)
最新推荐文章于 2024-11-09 13:08:09 发布