• 数据结构day42


    总结:DFS还没有掌握,前几天期中考,耽搁了好久,这个day42写了3天可以说;
    题目详情 - 1102 Invert a Binary Tree (pintia.cn)

    思路:用数组储存,-用-1表示;输入时就更换左右孩子即可,然后用hash标记出现的数字,如果没出现就是root,最后遍历即可;

    1. #include<iostream>
    2. #include<cctype>
    3. #include<queue>
    4. #include<algorithm>
    5. #pragma warning(disable:4996)
    6. using namespace std;
    7. struct node {
    8. int left;//- equals to -1
    9. int right;
    10. }arr[10];
    11. void level_traverse(int root) {
    12. queue<int>q;
    13. if (root != -1) {
    14. q.push(root);
    15. }
    16. int flag = 1;
    17. while (!q.empty()) {
    18. if (flag) {
    19. cout << root;
    20. flag = 0;
    21. }
    22. else
    23. {
    24. cout << ' ' << root;
    25. }
    26. if (arr[root].left != -1) {
    27. q.push(arr[root].left);
    28. }
    29. if (arr[root].right != -1) {
    30. q.push(arr[root].right);
    31. }
    32. q.pop();
    33. root = q.front();
    34. }
    35. }
    36. int f = 1;
    37. void in_traverse(int root) {
    38. if (root == -1) {
    39. return;
    40. }
    41. in_traverse(arr[root].left);
    42. if (f) {
    43. f = 0;
    44. cout << root;
    45. }
    46. else
    47. {
    48. cout << ' ' << root;
    49. }
    50. in_traverse(arr[root].right);
    51. }
    52. int main() {
    53. int n;
    54. cin >> n;
    55. int hash[10] = { 0 };
    56. char c1, c2;
    57. for (int i = 0; i < n; i++) {
    58. cin >> c1 >> c2;
    59. arr[i].right = isdigit(c1) ? c1 - '0' : -1;
    60. arr[i].left = isdigit(c2) ? c2 - '0' : -1;
    61. if (arr[i].left != -1) {
    62. hash[arr[i].left] = 1;
    63. }
    64. if (arr[i].right != -1) {
    65. hash[arr[i].right] = 1;
    66. }
    67. }
    68. int root = 0;
    69. for (int i = 0; i < n; i++) {
    70. if (hash[i] == 0) {
    71. root = i;
    72. break;
    73. }
    74. }
    75. level_traverse(root);
    76. cout << '\n';
    77. in_traverse(root);
    78. return 0;
    79. }

    题目详情 - 1053 Path of Equal Weight (pintia.cn)

    思路:测试点6尚未通过;原因是sort只保证了儿子节点,未保证孙子节点从大到小;需要多写两次掌握DFS;

    1. #include<iostream>
    2. #include<cctype>
    3. #include<stack>
    4. #include<vector>
    5. #include<algorithm>
    6. #pragma warning(disable:4996)
    7. using namespace std;
    8. struct node {
    9. int data;
    10. vector<int>son;
    11. }arr[100];
    12. int n, m, s;
    13. int path[100];
    14. bool cmp(int a, int b) {
    15. return arr[a].data > arr[b].data;
    16. }
    17. void dfs(int index,int num_node,int sum) {
    18. if (sum > s) {
    19. return;
    20. }
    21. if (sum == s) {
    22. if (arr[index].son.size() != 0) {
    23. return;
    24. }
    25. for (int i = 0; i < num_node; i++) {
    26. cout << arr[path[i]].data;
    27. if (i < num_node - 1) {
    28. cout << ' ';
    29. }
    30. else
    31. {
    32. cout << '\n';
    33. }
    34. }
    35. return;
    36. }
    37. for (int i = 0; i < arr[index].son.size(); i++) {
    38. int child = arr[index].son[i];
    39. path[num_node] = child;
    40. dfs(child, num_node + 1, sum + arr[child].data);
    41. }
    42. }
    43. int main() {
    44. cin >> n >> m >> s;
    45. for (int i = 0; i < n; i++) {
    46. cin >> arr[i].data;
    47. }
    48. int id, num, son_id;
    49. for (int i = 0; i < m; i++) {
    50. cin >> id >> num;
    51. for (int j = 0; j < num; j++) {
    52. cin >> son_id;
    53. arr[id].son.push_back(son_id);
    54. }
    55. sort(arr[id].son.begin(), arr[id].son.end(), cmp);//输出时从大到小
    56. }
    57. dfs(0, 1, arr[0].data);
    58. return 0;
    59. }

    题目详情 - 1079 Total Sales of Supply Chain (pintia.cn)

    思路:使用了BFS,先建树,然后用累加叶子节点层数对应的钱*个数得到答案;

    1. #include<iostream>
    2. #include<cctype>
    3. #include<queue>
    4. #include<vector>
    5. #include<algorithm>
    6. #pragma warning(disable:4996)
    7. using namespace std;
    8. struct node {
    9. int data;
    10. int layer;
    11. vector<int>son;
    12. }arr[100000];
    13. int n;
    14. double p, r, res;//default: 0
    15. double rate[100000];
    16. void layer() {
    17. queue<int>q;
    18. q.push(0);
    19. while (!q.empty()) {
    20. int root = q.front();
    21. q.pop();
    22. for (int i = 0; i < arr[root].son.size(); i++) {
    23. q.push(arr[root].son[i]);
    24. arr[arr[root].son[i]].layer = arr[root].layer + 1;
    25. }
    26. }
    27. }
    28. void bfs() {
    29. queue<int>q;
    30. q.push(0);
    31. while (!q.empty()) {
    32. int root = q.front();
    33. q.pop();
    34. if (!arr[root].son.size()) {
    35. res += rate[arr[root].layer] * arr[root].data;
    36. }
    37. else
    38. {
    39. for (int i = 0; i < arr[root].son.size(); i++) {
    40. q.push(arr[root].son[i]);
    41. }
    42. }
    43. }
    44. }
    45. int main() {
    46. cin >> n >> p >> r;
    47. rate[0] = p;
    48. for (int i = 1; i < n; i++) {//打表
    49. rate[i] =rate[i-1]*(1 + r / 100);
    50. }
    51. int num, id;
    52. arr[0].layer = 0;
    53. for (int i = 0; i < n; i++) {
    54. cin >> num;
    55. if (num == 0) {
    56. cin >> arr[i].data;
    57. }
    58. else
    59. {
    60. for (int j = 0; j < num; j++) {
    61. cin >> id;
    62. arr[i].son.push_back(id);
    63. }
    64. }
    65. }
    66. layer();
    67. bfs();
    68. printf("%.1f", res);
    69. return 0;
    70. }

    题目详情 - 1090 Highest Price in Supply Chain (pintia.cn)

    思路:和上题几乎一样,这次选择熟悉DFS;

    1. #include<iostream>
    2. #include<cmath>
    3. #include<vector>
    4. #pragma warning(disable:4996)
    5. using namespace std;
    6. int n, num, max_depth;
    7. double p, r;
    8. vector<int>v[100000];
    9. void dfs(int index, int depth) {
    10. if (v[index].size() == 0) {
    11. if (depth > max_depth) {
    12. max_depth = depth;
    13. num = 1;
    14. }
    15. else if (depth == max_depth) {
    16. num++;
    17. }
    18. }
    19. for (int i = 0; i < v[index].size(); i++) {
    20. dfs(v[index][i], depth + 1);
    21. }
    22. }
    23. int main() {
    24. cin >> n >> p >> r;
    25. r /= 100;//r/100
    26. int father, root;
    27. for (int i = 0; i < n; i++) {
    28. cin >> father;
    29. if (father == -1) {
    30. root = i;
    31. }
    32. else
    33. {
    34. v[father].push_back(i);//储存儿子的下标
    35. }
    36. }
    37. dfs(root, 0);
    38. printf("%.2f %d", p * pow(1 + r, max_depth), num);
    39. return 0;
    40. }

    题目详情 - 1094 The Largest Generation (pintia.cn)

    思路:和上题一样,用DFS,甚至根节点已经给定为1; 

    1. #include<iostream>
    2. #include<cmath>
    3. #include<vector>
    4. #pragma warning(disable:4996)
    5. using namespace std;
    6. vector<int>v[100];
    7. int max_num[100];
    8. int n, m;
    9. //dfs记录个数和层数
    10. void dfs(int index, int layer) {
    11. max_num[layer]++;
    12. for (int i = 0; i < v[index].size(); i++) {
    13. dfs(v[index][i], layer + 1);
    14. }
    15. }
    16. int main() {
    17. cin >> n >> m;
    18. int num, id, child;
    19. for (int i = 0; i < m; i++) {
    20. cin >> id >> num;
    21. for (int j = 0; j < num; j++) {
    22. cin >> child;
    23. v[id].push_back(child);
    24. }
    25. }
    26. dfs(1, 1);
    27. int max_ctn = 0, layer = 1;
    28. for (int i = 1; i < 100; i++) {
    29. if (max_num[i] > max_ctn) {
    30. max_ctn = max_num[i];
    31. layer = i;
    32. }
    33. }
    34. cout << max_ctn << ' ' << layer;
    35. return 0;
    36. }

    复习题: 

    题目详情 - 1075 PAT Judge (pintia.cn) 

    思路:对sort的熟悉,用时1小时,模拟题的速度快了不少;第一次写完测试点2和4出错,测试点2是要区分-1和0;测试点4我错误的原因是对满分个数情况,比如多次同一题满分不能叠加个数;

    这里有比较详细的对测试点4的分析:

    (25条消息) PAT 甲级 1075 PAT Judge 最后一个测试点未通过(C语言版本)_Xaiver_97的博客-CSDN博客

    这次写的代码比上一次逻辑好了不是一点点了;

    1. #include<iostream>
    2. #include<cmath>
    3. #include<vector>
    4. #include<algorithm>
    5. #pragma warning(disable:4996)
    6. using namespace std;
    7. struct node {
    8. int score[6];//0储存总分
    9. bool ctn[6];//0是-,1正常打印分数; ctn[0]代表是否提交过不为-1的答案,测试点2:区分-10
    10. int flag;//判断是否没有提交,或者全-1
    11. int full;//满分个数
    12. int id;
    13. }arr[10001];
    14. int max_score[6];//下标从1开始
    15. bool cmp(node s1, node s2) {
    16. if (s1.score[0] != s2.score[0]) {
    17. return s1.score[0] > s2.score[0];
    18. }
    19. else if (s1.full != s2.full) {
    20. return s1.full > s2.full;
    21. }
    22. else if(s1.flag!=s2.flag) {
    23. return s1.flag > s2.flag;
    24. }
    25. else
    26. {
    27. return s1.id < s2.id;
    28. }
    29. }
    30. int main() {
    31. int n, k, m;
    32. cin >> n >> k >> m;
    33. for (int i = 1; i <= k; i++) {
    34. cin >> max_score[i];
    35. }
    36. int ur_id, problem_id, score;
    37. for (int i = 0; i < m; i++) {//10^5
    38. cin >> ur_id >> problem_id >> score;
    39. arr[ur_id].id = ur_id;
    40. arr[ur_id].ctn[problem_id] = 1;
    41. if (score != -1) {
    42. arr[ur_id].ctn[0] = 1;
    43. }
    44. //提交多次满分答案,只能full++一次;
    45. if (score == max_score[problem_id]&&max_score[problem_id]!=arr[ur_id].score[problem_id]) {
    46. arr[ur_id].full++;
    47. }
    48. if (score > arr[ur_id].score[problem_id]) {//default: 0 so not to attn to -1
    49. arr[ur_id].score[0] += (score - arr[ur_id].score[problem_id]);//total score
    50. arr[ur_id].score[problem_id] = score;
    51. }
    52. if (arr[ur_id].ctn[0] != 0) {
    53. arr[ur_id].flag = 1;//提交过至少一次,且总分不为0
    54. }
    55. }
    56. sort(arr, arr + 10001, cmp);
    57. int rank = 1;
    58. for (int i = 0; i < n; i++) {
    59. if (arr[i].flag == 0)
    60. {
    61. break;
    62. }
    63. if (i > 0 && arr[i].score[0] < arr[i - 1].score[0]) {
    64. rank = i + 1;
    65. }
    66. printf("%d %05d %d", rank, arr[i].id, arr[i].score[0]);
    67. for (int j = 1; j <= k; j++) {
    68. if (arr[i].ctn[j] != 0) {
    69. printf(" %d", arr[i].score[j]);
    70. }
    71. else
    72. {
    73. printf(" -");
    74. }
    75. }
    76. printf("\n");
    77. }
    78. return 0;
    79. }

  • 相关阅读:
    go使用grpc
    017-JAVA重载及实例讲解
    驶入产业发展快车道,汉鑫科技人工智能研发中心正式启用!
    使用Maxent模型预测适生区
    eNSP - 基本命令解析
    redis相关知识点
    【云原生】Kubernetes CRD 详解(Custom Resource Definition)
    最新ACR15.0新功能如何使用?ps插件camera raw15.0mac版新功能教程
    Java的Lambda表达式学习笔记
    JSON字符串之JS中JSON.parse()
  • 原文地址:https://blog.csdn.net/weixin_58073817/article/details/127622908