2021.9.11 PAT顶级(100分)

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) 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值