PAT-2020年春季考試_2020.07.22_線上模擬_甲級1087-1090_心得+題解


題外話:PAT甲級滿分可以去姥姥公司^_^,955高性價比

投遞簡歷連接https://www.pat-edu.com/jia-ru-wo-men.html

--------------------------------------------------------

級別:甲級

時間2020年7月22日,下午13:30-16:30,線上模擬,雙機位攝像頭,需要提前一小時測試比賽環境、調整機器。

本次模擬使用題集:PTA甲級1087、1088、1089、1090

監考客戶端下載:https://pintia.cn/problem-sets?tab=0

 

 

1087(第四題):

#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=250;
string s[maxn],start;//,res;
int val[maxn],G[maxn][maxn];
bool vst[maxn];
map<string,int>mp;
map<int,string>mp2;
int num[1000000],cost=inf,happy,avehappy,happynum,_end;
int n,k;


int a[maxn],tmp[maxn];
void dfs(int cur,int _cost,int _happy,int _happynum)
{
    tmp[_happynum]=cur;
    //cout<<_happynum<<" "<<cur<<endl;
    if(cur==_end)
    {
        //cout<<cur<<" "<<_cost<<" "<<_happy<<" "<<_happynum<<endl;
        //for(int i=0;i<=_happynum;i++)cout<<tmp[i]<<" ";
        //cout<<endl;
        num[_cost]++;
        if(_cost<cost)
        {
            cost=_cost;
            happy=_happy;
            happynum=_happynum;
            for(int i=0;i<=_happynum;i++)a[i]=tmp[i];
        
        }
        else if(_cost==cost)
        {
            if(_happy>happy)
            {        
                cost=_cost;
                happy=_happy;
                happynum=_happynum;
                for(int i=0;i<=_happynum;i++)a[i]=tmp[i];
    
            }
            else if(_happy==happy&&_happynum<happynum)
            {
                cost=_cost;
                happy=_happy;
                happynum=_happynum;
                for(int i=0;i<=_happynum;i++)a[i]=tmp[i];
    
            }
        }
        return;
    }
    for(int i=0;i<n;i++)
    {
        if(G[cur][i]!=-1&&!vst[i])
        {
            vst[i]=1;
            
            dfs(i,_cost+G[cur][i],_happy+val[i],_happynum+1);
            vst[i]=0;
        }
    }
}
int main()
{
    cin>>n>>k>>start;
    mp[start]=0;
    mp2[0]=start;
    for(int i=1;i<n;i++)
    {
        cin>>s[i]>>val[i];
        mp[s[i]]=i;
        mp2[i]=s[i];
        if(s[i]=="ROM")_end=i;
    }
    memset(G,-1,sizeof G);
    for(int i=0;i<k;i++)
    {
        string s1,s2;
        int cst;
        cin>>s1>>s2>>cst;
        G[mp[s1]][mp[s2]]=G[mp[s2]][mp[s1]]=cst;
    }

    vst[0]=1;
    dfs(0,0,0,0);
    
    double avehappy=happy/happynum;
    cout<<num[cost]<<" "<<cost<<" "<<happy<<" "<<avehappy<<endl;
    for(int i=0;i<happynum;i++)
    {
        cout<<mp2[a[i]]<<"->";
    }
    cout<<"ROM"<<endl;
    //cout<<res<<endl;
    
    return 0;
}
View Code
#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f;
const int maxn=250;
string s[maxn],start,res;
int val[maxn],G[maxn][maxn];
bool vst[maxn];
map<string,int>mp;
map<int,string>mp2;
int num[1000000],cost=inf,happy,avehappy,happynum,_end;
int n,k;
void dfs(int cur,int _cost,int _happy,int _happynum,string s)
{
    if(cur==_end)
    {
        //cout<<cur<<" "<<_cost<<" "<<_happy<<" "<<_happynum<<" "<<s<<endl;
        num[_cost]++;
        if(_cost<cost)
        {
            cost=_cost;
            happy=_happy;
            happynum=_happynum;
            res=s;
        }
        else if(_cost==cost)
        {
            if(_happy>happy)
            {        
                cost=_cost;
                happy=_happy;
                happynum=_happynum;
                res=s;
            }
            else if(_happy==happy&&_happynum<happynum)
            {
                cost=_cost;
                happy=_happy;
                happynum=_happynum;
                res=s;
            }
        }
        return;
    }
    for(int i=0;i<n;i++)
    {
        if(G[cur][i]!=-1&&!vst[i])
        {
            vst[i]=1;
            dfs(i,_cost+G[cur][i],_happy+val[i],_happynum+1,s+"->"+mp2[i]);
            vst[i]=0;
        }
    }
}
int main()
{
    cin>>n>>k>>start;
    mp[start]=0;
    mp2[0]=start;
    for(int i=1;i<n;i++)
    {
        cin>>s[i]>>val[i];
        mp[s[i]]=i;
        mp2[i]=s[i];
        if(s[i]=="ROM")_end=i;
    }
    memset(G,-1,sizeof G);
    for(int i=0;i<k;i++)
    {
        string s1,s2;
        int cst;
        cin>>s1>>s2>>cst;
        G[mp[s1]][mp[s2]]=G[mp[s2]][mp[s1]]=cst;
    }

    vst[0]=1;
    dfs(0,0,0,0,mp2[0]);
    
    avehappy=happy/happynum;
    cout<<num[cost]<<" "<<cost<<" "<<happy<<" "<<avehappy<<endl;
    cout<<res<<endl;
    
    return 0;
}
View Code

1088(第一題):

#include<bits/stdc++.h>
using namespace std;
void cal(long long a,long long b)
{
    long long ans,a2,b2;
    if(b==0)
    {
        cout<<"Inf";
        return;
    }
    if(b<0)a=-a,b=-b;
    if(a==0)
    {
        cout<<0;
    }
    else if(a>0)
    {
        ans=a/b,a2=a%b,b2=b;
        int tmp=__gcd(a2,b2);
        a2=a2/tmp;
        b2=b2/tmp;
        if(a2==0)cout<<ans;
        else if(ans==0)cout<<a2<<"/"<<b2;
        else cout<<ans<<" "<<a2<<"/"<<b2;
    }
    else 
    {
        a=-a;
        ans=a/b,a2=a%b,b2=b;
        int tmp=__gcd(a2,b2);
        a2=a2/tmp;
        b2=b2/tmp;
        cout<<"(-";
        if(a2==0)cout<<ans;
        else if(ans==0)cout<<a2<<"/"<<b2;
        else cout<<ans<<" "<<a2<<"/"<<b2;
        cout<<")";
    }
}
void sol(long long a,long long b,long long c,long long d,char ch)
{
    if(ch=='+')
    {
        cal(a*d+b*c,b*d);
    }
    else if(ch=='-')
    {
        cal(a*d-b*c,b*d);
    }
    else if(ch=='*')
    {
        cal(a*c,b*d);
    }
    else 
    {
        cal(a*d,b*c);
    }
}
int main()
{
    long long a,b,c,d;
    scanf("%lld/%lld %lld/%lld",&a,&b,&c,&d);
    //cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;
    cal(a,b);cout<<" + ";cal(c,d);cout<<" = ";sol(a,b,c,d,'+');cout<<endl;
    cal(a,b);cout<<" - ";cal(c,d);cout<<" = ";sol(a,b,c,d,'-');cout<<endl;
    cal(a,b);cout<<" * ";cal(c,d);cout<<" = ";sol(a,b,c,d,'*');cout<<endl;
    cal(a,b);cout<<" / ";cal(c,d);cout<<" = ";sol(a,b,c,d,'/');cout<<endl;
    
    return 0;
} 
View Code

1089(第二題):

#include<bits/stdc++.h>
using namespace std;
const int maxn=200;
int a[maxn],b[maxn];
int tmp[maxn],n;
bool cmp()
{
    /*
    for(int i=0;i<n;i++)
    {
        cout<<tmp[i]<<" ";
    }
    cout<<endl;
    */
    for(int i=0;i<n;i++)
    {
        if(tmp[i]!=b[i])return false;
    }
    return true;
}

void Insertion()
{
    for(int i=0;i<n;i++)tmp[i]=a[i];
    for(int i=2;i<=n;i++)//如果i從1開始,Merge函數要運行在Insertion函數前 
    {
        sort(tmp,tmp+i); 
        if(cmp())
        {
            int last=min(i+1,n);
            sort(tmp,tmp+last);
            cout<<"Insertion Sort"<<endl;
            for(int j=0;j<n-1;j++)cout<<tmp[j]<<" ";
            cout<<tmp[n-1]<<endl;
            exit(0);
        }
    }
}
void Merge()
{
    for(int i=0;i<n;i++)tmp[i]=a[i];
    for(int i=1;;i*=2)
    {
        for(int j=0;j<n;j+=i)
        {
            int l=j,r=min(j+i,n);
            sort(tmp+l,tmp+r);
            
            //cout<<l<<" "<<r-1<<endl;
            //merge(j,j+i);
        }
        
        if(cmp())
        {
            cout<<"Merge Sort"<<endl;
            i*=2;
            for(int j=0;j<n;j+=i)
            {
                int l=j,r=min(j+i,n);
                sort(tmp+l,tmp+r);
            }
        
            for(int j=0;j<n-1;j++)cout<<tmp[j]<<" ";
            cout<<tmp[n-1]<<endl;
            exit(0);
        }
        if(i>n)break;
    }
}

int main()
{
    cin>>n;
    for(int i=0;i<n;i++)cin>>a[i];
    for(int i=0;i<n;i++)cin>>b[i];
    Insertion();
    Merge();
    
    
    return 0;
}
View Code

1090(第三題):

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
int a[maxn],ans=0,b[maxn];
vector<int> v[maxn];
void dfs(int cur,int len)
{
    int size=v[cur].size();
    if(size==0)
    {
        ans=max(ans,len);
        b[len]++;
        return;
    }
    for(int i=0;i<size;i++)
    {
        dfs(v[cur][i],len+1);
    }
}
int main()
{
    //cout<<1.80*1.01*1.01*1.01<<endl;
    int n,start=0;
    long double p,r;
    cin>>n>>p>>r;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]==-1)start=i;
        else v[a[i]].push_back(i);
    }
    dfs(start,0);
    //cout<<ans<<endl;
    for(int i=0;i<ans;i++)
    {
        p=p+p*r*0.01;
        //cout<<p<<endl;
    }
    printf("%.2Lf %d\n",p,b[ans]);
    //cout<<p<<" "<<b[ans]<<endl;
    
    
    return 0;
}
View Code

 

 

先開第一題,模擬模得頭裂了,竟然過了

再看第二題,看得焦頭爛額,跳過看別的題

看了兩遍第三題,還是沒看懂題目,研究了一下樣例,貌似懂了一些什么......試了一下然后一發入魂,這個時候還有2hour42min

第三道開的是第四題,本來以為是最短路板子題,后來發現不大行,於是改了一下用了dfs,but最后兩個點wa了(23/30),懷疑過avehappy的取整,懷疑過string的長度會不會不夠,改完代碼之后於事無補,最后發現記錄cost最大值的數組開小了,開到1e6之后就過了,這個時候還有1hour22min

最后折回來看第二題,跟着樣例學了一發插入排序和歸並排序,用sort取巧了,妙得我只想寫題解233。不過有個點wa掉了(24/25),盲猜是序列和原序列相同時應該判為Merge sort,寫完四題還剩50min

 

寫到第三題的時候看了一眼榜,已經有人AK了,真是巨佬啊......

總體感覺挺愉快~沒有被特殊樣例卡掉的場次就是好場次

不過考到一半轉過頭發現后面的機位上顯目的qq消息,感覺人都傻了,正式賽一定退qq!

 

tips:

萬能頭可以用,所有圖論都能開鄰接矩陣(不用背鄰接表),最短路基本上都可以用dijstra(不用背kruskal),用不到set,基本上map和vector就完事了

刷題請倒序!!!近幾年真題題型重復率大,沒有時間還要正着刷的都是耍流氓!

自用dijstra板子:

#include <iostream>
#include <string.h>
using namespace std;
const int inf=0x3f3f3f3f;
int G[110][110];
int dist[110];
bool vst[110];
int n,m;
void dijkstra(int s)
{
    memset(vst,false,sizeof(vst));
    memset(dist,0x3f,sizeof(dist));
    dist[s]=0;
    for(int i=0;i<n;i++)
    {
        int v,min_d=inf;
        for(int j=1;j<=n;j++)
        {
            if(!vst[j]&&dist[j]<min_d)
            {
                min_d=dist[j];
                v=j;
            }
        }
        vst[v]=true;
        for(int j=1;j<=n;j++)
        {
            dist[j]=min(dist[j],dist[v]+G[v][j]);
        }
    }
}
int main() {
    cin>>n>>m;
    memset(G,0x3f,sizeof(G));
    for(int i=0;i<m;i++)
    {
        int u,v,len;
        cin>>u>>v>>len;
        G[u][v]=G[v][u]=len;
    }
    dijkstra(1);
    for(int i=1;i<=n;i++)
    {
        cout<<dist[i]<<" ";
    }
    return 0;
}
View Code

自用並查集板子:

//初始化
void init() {
    for (int i = 1; i <= n; ++i) {
        father[i] = i;
    }
}
//查找
int get(int x) {
    if (father[x] == x) { // x 結點就是根結點
        return x;
    }
    return get(father[x]); // 返回父結點的根結點
}
//合並
void merge(int x, int y) {
    x = get(x);
    y = get(y);
    if (x != y) { // 不在同一個集合
        father[y] = x;
    }
}
View Code

自用前序中序后序轉換板子(柳婼版):

自用拓撲排序:

// 使用鄰接表存儲,若對此數據結構不熟悉,請學習鄰接表相關的課程內容
struct edge {
    int v, next;
} e[MAX_M];
int p[MAX_N], eid;


int topo() {
    queue<int> q;
    for (int i = 1; i <= n; i++) {
          if (indegree[i] == 0) {  // 將所有入度為零的頂點入隊
            q.push(i);
        }
    }


    while (!q.empty()) {
        int now = q.front();
        cout << "visiting " << now << endl;
        q.pop();
        for (int i = p[now]; i != -1; i = e[i].next) {
            int v = e[i].v;
            indegree[v]--;
            if (indegree[v] == 0) {  // 將入度新變成零的頂點入隊
                q.push(v);
            }
        }
    }
}
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM