T1
开局刚看的时候没怎么看懂题,写了个假算法样例没过。开完T2回过头来重新读的题。
大致题意:
给出一组(key,value)对,按key值升序排列,以此为建堆顺序,根据value值建立小根堆,对建好的堆层序遍历。
做法:
读完题不太想去建堆(主要是太久没写代码,调试起来比较麻烦),找了个别的做法,对一个序列[l,r]遍历一遍找到value值最小的位置pos,记录pos,再把序列拆分成[l,pos-1]和[pos+1,r]两段重复如上做法,按层存储pos,遍历一遍输出。
难度大致 div2.B
#include <bits/stdc++.h>
using namespace std;
static const int MAXN=30+10;
struct Node{
int key,value;
bool operator<(const Node &o)const{
return key<o.key;
}
}t[MAXN];
int n;
vector<int> ans1[MAXN],ans2[MAXN];
void dfs(int u,int l,int r)
{
if(r<l) return;
int pos=l;
for(int i=l+1;i<=r;i++)
if(t[i].value<t[pos].value) pos=i;
ans1[u].push_back(t[pos].key);
ans2[u].push_back(t[pos].value);
dfs(u+1,l,pos-1);
dfs(u+1,pos+1,r);
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d%d",&t[i].key,&t[i].value);
sort(t+1,t+n+1);
dfs(1,1,n);
int cnt=0;
for(int i=1;i<=n;i++)
for(auto it:ans1[i])
{
printf("%d",it);
cnt++;
if(cnt!=n) putchar(' ');
}
puts("");
cnt=0;
for(int i=1;i<=n;i++)
for(auto it:ans2[i])
{
printf("%d",it);
cnt++;
if(cnt!=n)