PAT甲级1101~1125

PAT甲级1101~1125

    • 1101 Quick Sort (25 分)
    • 1102 Invert a Binary Tree (25 分)
    • 1103 Integer Factorization (30 分)

1101 Quick Sort (25 分)

There is a classical process named partition in the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements less than the pivot are moved to its left and those larger than the pivot to its right. Given N distinct positive integers after a run of partition, could you tell how many elements could be the selected pivot for this partition?
For example, given N=5 and the numbers 1, 3, 2, 4, and 5. We have:

1 could be the pivot since there is no element to its left and all the elements to its right are larger than it;
3 must not be the pivot since although all the elements to its left are smaller, the number 2 to its right is less than it as well; 
2 must not be the pivot since although all the elements to its right are larger, the number 3 to its left is larger than it as well; and for the similar reason, 4 and 5 could also be the pivot. 

Hence in total there are 3 pivot candidates.

#include
#include
#include
#include
using namespace std;

int N,a[100000];

int main(){
    int i,j,k;
    vector<int>candidates;
    set<int>before,after;
    
    cin>>N;
    for(i=0;i<N;i++){
        scanf("%d",a+i);
        after.insert(a[i]);
    }
    for(i=0;i<N;i++){
        after.erase(a[i]);
        if(before.empty()||*before.rbegin()<a[i]){
            if(after.empty()||*after.begin()>a[i])
                candidates.push_back(a[i]);
        }
        before.insert(a[i]);
    }
    
    sort(candidates.begin(),candidates.end());
    cout<<candidates.size()<<endl;
    for(i=0;i<candidates.size();i++){
        if(i)cout<<" ";
        cout<<candidates[i];
    }
    cout<<endl;
}

1102 Invert a Binary Tree (25 分)

The following is from Max Howell @twitter:
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.
Now it’s your turn to prove that YOU CAN invert a binary tree!

#include
#include
using namespace std;

struct node{
    int data,lchild,rchild;
};

int N,root;
node n[100];
bool inordertraverse_started=false;

void levelorderedtraverse(){
    queue<int>Q;
    Q.push(root);
    while(Q.size()){
        auto a=Q.front();
        Q.pop();
        if(a!=root)cout<<" ";
        cout<<n[a].data;
        if(n[a].rchild>=0)Q.push(n[a].rchild);
        if(n[a].lchild>=0)Q.push(n[a].lchild);
    }
    cout<<endl;
}

void inordertraverse(int cur){
    if(n[cur].rchild>=0)inordertraverse(n[cur].rchild);
    if(inordertraverse_started)cout<<" ";
    else inordertraverse_started=true;
    cout<<n[cur].data;
    if(n[cur].lchild>=0)inordertraverse(n[cur].lchild);
}

int main(){
    int i,j,k,l;
    string s;
    bool appeared[10]={0};
    cin>>N;
    for(i=0;i<N;i++){
        n[i].data=i;
        cin>>s;
        if(s=="-")n[i].lchild=-1;
        else{
            appeared[s[0]-'0']=true;
            n[i].lchild=s[0]-'0';
        }
        cin>>s;
        if(s=="-")n[i].rchild=-1;
        else{
            appeared[s[0]-'0']=true;
            n[i].rchild=s[0]-'0';
        }
    }
    
    for(root=0;appeared[root];root++);
    
    levelorderedtraverse();
    inordertraverse(root);
}

1103 Integer Factorization (30 分)

The K−P factorization of a positive integer N is to write N as the sum of the P-th power of K positive integers. You are supposed to write a program to find the K−P factorization of N for any positive integers N, K and P.

#include
#include
#include
using namespace std;

struct node{
    int a[401];
};

int N,P,K;//N要分解的数,P指数,K个数
int power[21];
vector<node>solves;

void dfs(int cur,int restSum,int rest,int idx,node &n){
    restSum-=power[cur];
    rest--;
    n.a[idx++]=cur;
    if(!restSum){
        if(!rest)
            solves.push_back(n);
    }
    else if(restSum>0){
        if(rest){
            for(int i=cur;i<=20&&power[i]<=restSum;i++)
                dfs(i,restSum,rest,idx,n);
        }
    }
}

int main(){
    int i,j,k,l;
    cin>>N>>K>>P;
    for(i=1;i<=20;i++){
        power[i]=1;
        for(j=P;j--;)power[i]*=i;
    }
    for(i=1;i<=20;i++){
        node n;
        dfs(i,N,K,1,n);
    }
    
    if(solves.empty()){
        cout<<"Impossible";
        return 0;
    }
    
    for(node&b:solves){
        b.a[0]=0;
        for(i=1;i<=K;i++)b.a[0]+=b.a[i];
        reverse(b.a+1,b.a+K+1);
    }
    
    int ans=0;
    for(i=1;i<solves.size();i++){
        if(solves[i].a[0]>solves[ans].a[0])ans=i;
        else if(solves[i].a[0]<solves[ans].a[0])continue;
        else {
            for(j=1;j<=K;j++){
                if(solves[i].a[j]!=solves[ans].a[j])break;
            }
            if(solves[i].a[j]>solves[ans].a[j])ans=i;
        }
    }
    
    cout<<N;
    for(i=1;i<=K;i++){
        if(i==1)cout<<" = ";
        else cout<<" + ";
        cout<<solves[ans].a[i]<<'^'<<P;
    }
}

你可能感兴趣的:(PAT,算法基础,c语言,c++,开发语言)