PAT (Advanced Level) 1132~1135:1132 模拟 1133模拟(易超时!) 1134图 1135红黑树

1132 Cut Integer(20 分)

题意:将一个含K(K为偶数)个数字的整数Z割分为A和B两部分,若Z能被A*B整除,则输出Yes,否则输出No。

分析:当A*B为0的时候,不能被Z整除,输出No。否则会出现浮点错误。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<map>
#include<iostream>
#include<vector>
#include<set>
#include<cmath>
using namespace std;
char s[20];
int main(){
    int N;
    scanf("%d", &N);
    while(N--){
        scanf("%s", s);
        int len = strlen(s);
        int A = 0;
        for(int i = 0; i < len / 2; ++i){
            A = A * 10 + s[i] - ‘0‘;
        }
        int B = 0;
        for(int i = len / 2; i < len; ++i){
            B = B * 10 + s[i] - ‘0‘;
        }
        int C = A * B;
        if(C == 0){
            printf("No\n");
            continue;
        }
        int x = atoi(s);
        if(x % C == 0) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

1133 Splitting A Linked List(25 分)

题意:给定一个链表,将链表重新排序,在不打乱原链表相对顺序的前提下,小于0的在最前面,其次是0~K,最后是大于K的数。

分析:

1、3次遍历可实现链表重排。

2、map映射value和pre或suc的关系会超时,所以,以pre为结点定义结构体,组织链表的重排,从而进行优化。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<map>
#include<iostream>
#include<vector>
#include<set>
#include<cmath>
using namespace std;
const int MAXN = 100000 + 10;
struct Node{
    int pre, value, suc;
}num[MAXN];
vector<int> old, ans;
int main(){
    int N, K, head, pre, value, suc;
    scanf("%d%d%d", &head, &N, &K);
    for(int i = 0; i < N; ++i){
        scanf("%d%d%d", &pre, &value, &suc);
        num[pre].pre = pre;
        num[pre].value = value;
        num[pre].suc = suc;
    }
    while(head != -1){
        old.push_back(head);
        head = num[head].suc;
    }
    int len = old.size();
    for(int i = 0; i < len; ++i){
        if(num[old[i]].value < 0) ans.push_back(old[i]);
    }
    for(int i = 0; i < len; ++i){
        if(num[old[i]].value >= 0 && num[old[i]].value <= K) ans.push_back(old[i]);
    }
    for(int i = 0; i < len; ++i){
        if(num[old[i]].value > K) ans.push_back(old[i]);
    }
    for(int i = 0; i < len - 1; ++i){
        printf("%05d %d %05d\n", ans[i], num[ans[i]].value, ans[i + 1]);
    }
    printf("%05d %d -1\n", ans[len - 1], num[ans[len - 1]].value);
    return 0;
}
1134 Vertex Cover(25 分)

题意:vertex cover是指图中一些点的集合,使得图中每一条边的两个点中都至少有一个点在该点集中。给定点的集合,判断是否为vertex cover。

分析:按题意模拟即可。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<map>
#include<iostream>
#include<vector>
#include<set>
#include<cmath>
using namespace std;
const int MAXN = 10000 + 10;
int N, M;
bool vis[MAXN];
struct Edge{
    int u, v;
    void read(){
        scanf("%d%d", &u, &v);
    }
}num[MAXN];
int main(){
    scanf("%d%d", &N, &M);
    for(int i = 0; i < M; ++i){
        num[i].read();
    }
    int K;
    scanf("%d", &K);
    while(K--){
        int n, x;
        scanf("%d", &n);
        memset(vis, false, sizeof vis);
        while(n--){
            scanf("%d", &x);
            vis[x] = true;
        }
        bool ok = true;
        for(int i = 0; i < M; ++i){
            if(vis[num[i].u] || vis[num[i].v]) continue;
            ok = false;
            break;
        }
        if(ok) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

1135 Is It A Red-Black Tree(30 分

题意:给定一棵二叉搜索树的前序遍历序列,判断其是否为一棵红黑树。

分析:

1、红黑树是一棵平衡的二叉搜索树,满足以下条件:

(1)所有结点不是红色就是黑色;

(2)根结点是黑色;

(3)每一个为NULL的叶子结点是黑色;

(4)如果某结点是红色,其左右子结点都是黑色;

(5)对于每个结点,其到所有后代叶子结点经过的黑色结点数相同;

2、根据给定的前序遍历序列,结合二叉搜索树的定义可以建树。

3、递归检查条件4。

4、同理,递归统计左右子树的黑色结点数,来检查条件5。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<map>
#include<iostream>
#include<vector>
#include<set>
#include<cmath>
using namespace std;
const int MAXN = 30 + 10;
struct Node{
    Node *left, *right;
    int value;
};
struct Node *root;
bool ok;
void build(Node* &r, int x){
    if(r == NULL){
        r = (Node*)malloc(sizeof(Node));
        r -> value = x;
        r -> left = r -> right = NULL;
        return;
    }
    if(abs(x) < abs(r -> value)){
        build(r -> left, x);
    }
    else{
        build(r -> right, x);
    }
}
void judge_RedNode(Node* r){
    if(!ok) return;
    if(r -> left != NULL){
        if(r -> value < 0 && r -> left -> value < 0){
            ok = false;
            return;
        }
        else judge_RedNode(r -> left);
    }
    if(r -> right != NULL){
        if(r -> value < 0 && r -> right -> value < 0){
            ok = false;
            return;
        }
        else judge_RedNode(r -> right);
    }
}
int judge_BlackNode(Node* r){
    int leftcnt, rightcnt;
    if(!ok) return -1;
    if(r == NULL) return 1;
    leftcnt = judge_BlackNode(r -> left);
    rightcnt = judge_BlackNode(r -> right);
    if(leftcnt != rightcnt){
        ok = false;
        return -1;
    }
    else{
        if(r -> value > 0) ++leftcnt;
    }
    return leftcnt;
}
int main(){
    int K;
    scanf("%d", &K);
    while(K--){
        root = NULL;
        int N, x;
        scanf("%d", &N);
        while(N--){
            scanf("%d", &x);
            build(root, x);
        }
        ok = true;
        judge_RedNode(root);
        judge_BlackNode(root);
        if(root -> value < 0 || !ok){
            printf("No\n");
        }
        else{
            printf("Yes\n");
        }
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/9560759.html

时间: 2024-11-01 15:57:57

PAT (Advanced Level) 1132~1135:1132 模拟 1133模拟(易超时!) 1134图 1135红黑树的相关文章

Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)

Pat1043代码 题目描述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes

Pat(Advanced Level)Practice--1044(Shopping in Mars)

Pat1044代码 题目描述: Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position for only once and some of the diam

PAT (Advanced Level) 1093. Count PAT&#39;s (25)

预处理每个位置之前有多少个P,每个位置之后有多少个T. 对于每个A,贡献的答案是这个A之前的P个数*这个A之后T个数. #include<cstdio> #include<cstring> long long MOD=1e9+7; const int maxn=1e5+10; long long dp1[maxn],dp2[maxn]; char s[maxn]; int main() { scanf("%s",s); memset(dp1,0,sizeof d

PAT (Advanced Level) 1055. The World&#39;s Richest (25)

排序.随便加点优化就能过. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<stack> #include<vector> using names

Pat(Advanced Level)Practice--1018(Public Bike Management)

Pat1018代码 题目描述: There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city. The Public Bike Management C

Pat(Advanced Level)Practice--1076(Forwards on Weibo)

Pat1076代码 题目描述: Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a user makes a post on Weibo, all hi

Pat(Advanced Level)Practice--1016(Phone Bills)

Pat1016代码 题目描述: A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connecting a lon

1093. Count PAT&#39;s (25)【计数】——PAT (Advanced Level) Practise

题目信息 1093. Count PAT's (25) 时间限制120 ms 内存限制65536 kB 代码长度限制16000 B The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th c

Pat(Advanced Level)Practice--1060(Are They Equal)

Pat1060代码 题目描述: If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0.123*105 with simple chopping. Now given the number of significant digits on a machine and two flo