Java教程

2021PAT甲级秋季考试题解

本文主要是介绍2021PAT甲级秋季考试题解,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

7-1Arrays and Linked Lists

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
const int N = 1e4+10;
struct L{
    int address;
    int len;
}link[N];
int sum[N];
int main()
{
    int n,k;
    int total = 0;
    cin >> n >> k;
    for(int i = 0 ; i < n ; i ++)
    {
        cin >> link[i].address >> link[i].len;
        total+=link[i].len;
    }
    sum[0] = link[0].len-1;
    for(int i = 1 ; i < n ; i++) sum[i] = sum[i-1] + link[i].len;
    total -= 1;
    int q;
    int cnt = 1;
    for(int i = 0 ; i < k ; i ++)
    {
        cin >> q;
        if(q>total) puts("Illegal Access");
        else
        {
            int l = 0,r=n-1;
            while(l<r)
            {
                int mid = (l+r)/2;
                if(sum[mid] < q) l = mid+1;
                else r = mid;
            }
            if(l==0)
            {
                int diff = link[l].address + 4*q;
                cout<<diff<<endl;
            }
            else
            {
                int diff = link[l].address + 4*(q-1-sum[l-1]);
                cout<<diff<<endl;
            }
            cnt = max(cnt,l+1);
        }
    }
    cout<<cnt;
    return 0;
}

7-2 Stack of Hats

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
const int N = 1e4+10;
unordered_map<int,int> ord;// 表示帽子的大小顺序
unordered_map<int,int> pos;// 表示人先后到来顺序
int a[N];
int b[N];
int n;
int main()
{
    cin >> n;
    for(int i = 1 ; i <= n ; i ++)
    {
        cin >> a[i];
        b[i] = a[i];
    }
    sort(a+1,a+n+1,greater<int>()); //从大到小排序
    for(int i = 1 ; i <= n ; i ++) ord[a[i]] = i; //记录帽子大小顺序
    for(int i = 1 ; i <= n ; i ++)
    {
        cin >> a[i];
        pos[a[i]] = i;
    }

    sort(a+1,a+n+1,greater<int>()); //从大到小排序
    for(int i = n ; i >= 2 ; i--)
    {
        cout<<pos[a[ord[b[i]]]]<<" ";
    }
    cout<<pos[a[ord[b[1]]]];
    return 0;


}

7-3 Playground Exploration

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
const int N = 110;
bool st[N];
vector<int> g[N];
int n,m;
int dfs(int u)
{
    int len = 1 ;
    for(int i = 0 ; i < g[u].size() ; i ++)
    {
        if(!st[g[u][i]])
        {
//            cout<<g[u][i]<<" ";
            st[g[u][i]] = true;
            len = len + dfs(g[u][i]);
            break;
        }
    }
    return len;
}
int main()
{
    cin >> n >> m;
    for(int i = 1 ; i <= m ; i ++)
    {
        int a,b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    for(int i = 1 ; i <= n ; i ++)
    {
        sort(g[i].begin(),g[i].end());
    }
    int cnt = 0;
    int idx = 0;
    for(int i = 1 ; i <= n ; i ++)
    {
        memset(st,0,sizeof st);
        st[i] = true;
        int len = dfs(i);
        if(len > cnt) {
            idx = i;
            cnt = len;
        }
    }
    cout<<idx<<" "<<cnt;
    return 0;
}

7-4 Sorted Cartesian Tree

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
const int N = 35;
PII a[N];
unordered_map<int,int> le,rt;//记录左右子树
int n;
vector<PII> ans;
bool cmp(PII a,PII b)
{
    return a.first < b.first;
}
int build(int l,int r)
{
    int minn = a[l].second; //priority值进行比较
    int root = l;
    if(l>r) return -1;
    for(int i = l; i <= r; i ++){
        if(a[i].second<minn){
            root = i;
            minn = a[i].second;
        }
    }
    if( root > l ) le[root] = build(l,root-1);
    if( root < r ) rt[root] = build(root+1,r);
    return root;
}
void bfs(int root)
{
    queue<int> q;
    q.push(root);
    while(q.size())
    {
        int t = q.front();
        q.pop();
        PII now = a[t];
        ans.push_back(now);
        if(le.count(t)) q.push(le[t]);
        if(rt.count(t)) q.push(rt[t]);
    }
}
int main()
{
    cin >> n;
    for(int i = 1 ; i <= n ; i ++ ){
        cin >> a[i].first >> a[i].second;
    }
    sort(a+1,a+n+1,cmp); //根据Key值排序,得到中序遍历序列
    int root = build(1,n); //建树
    bfs(root);
    for(int i = 0 ; i < ans.size()-1 ; i ++) cout<<ans[i].first<<" ";
    cout<<ans[int(ans.size()-1)].first<<"\n";
    for(int i = 0 ; i < ans.size()-1 ; i ++) cout<<ans[i].second<<" ";
    cout<<ans[int(ans.size()-1)].second;
    return 0;
}
这篇关于2021PAT甲级秋季考试题解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!