PAT甲级1101~1125

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<iostream>
#include<algorithm>
#include<set>
#include<vector>
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<iostream>
#include<queue>
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<iostream>
#include<vector>
#include<algorithm>
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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南宫萧幕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值