problemscpp
A collection of my answers to algorithm problems in c++.
| 函数
acwing::acwing149 命名空间参考

  1. 荷马史诗
更多...

struct  Compare
 
class  huff_tree
 

函数

int main (istream &cin, ostream &cout)
 
 TEST (acwing149, case1)
 
 TEST (acwing149, case2)
 
 TEST (acwing149, case3)
 

详细描述

  1. 荷马史诗

函数说明

◆ main()

int acwing::acwing149::main ( istream &  cin,
ostream &  cout 
)

在文件 acwing408.cpp536 行定义.

536 {
537 u_int64_t n, k, w;
538 cin >> n >> k;
539 priority_queue<huff_tree *, vector<huff_tree *>, Compare> pq;
540 for(int i = 0; i < n; i++) {
541 cin >> w;
542 pq.push(new huff_tree(w, 1, k));
543 }
544 while((n - 1) % (k - 1)) {
545 pq.push(new huff_tree(0, 1, k));
546 n++;
547 }
548 while(pq.size() > 1) {
549 huff_tree *ht = new huff_tree(0, 0, k);
550 for(int i = 0; i < k && !pq.empty(); i++) {
551 huff_tree *least = pq.top();
552 ht->height = max(ht->height, least->height + 1);
553 ht->children[i] = least;
554 ht->val += least->val;
555 pq.pop();
556 }
557 pq.push(ht);
558 }
559 huff_tree *root = pq.top();
560 queue<pair<huff_tree *, u_int64_t>> q;
561 q.push(make_pair(root, 0));
562 u_int64_t min_level = 0;
563 u_int64_t sum = 0;
564 while(!q.empty()) {
565 auto [ht, level] = q.front();
566 q.pop();
567 bool flag = true;
568 for(int i = 0; i < k; i++) {
569 if(ht->children[i] != nullptr) {
570 flag = false;
571 q.push(make_pair(ht->children[i], level + 1));
572 }
573 }
574 if(flag) {
575 sum += ht->val * level;
576 min_level = max(min_level, level);
577 }
578 }
579 cout << sum << endl
580 << min_level << endl;
581 return 0;
582 }

引用了 acwing::acwing149::huff_tree::children, acwing::acwing149::huff_tree::height, acwing::acwing3786::root, acwing::TreeNode::val , 以及 acwing::acwing149::huff_tree::val.

被这些函数引用 TEST().

◆ TEST() [1/3]

acwing::acwing149::TEST ( acwing149  ,
case1   
)

在文件 acwing408_test.cpp1023 行定义.

1023 {
1024 istringstream in("4 2\n"
1025 "1\n"
1026 "1\n"
1027 "2\n"
1028 "2");
1029 auto out = ostringstream();
1030 main(in, out);
1031 const auto ans = out.str();
1032 ASSERT_EQ("12\n"
1033 "2\n",
1034 ans);
1035 }
int main(int argc, char **argv)
Definition: main.cpp:5

引用了 main().

◆ TEST() [2/3]

acwing::acwing149::TEST ( acwing149  ,
case2   
)

在文件 acwing408_test.cpp1037 行定义.

1037 {
1038 istringstream in("3 2\n"
1039 "1\n"
1040 "2\n"
1041 "3");
1042 auto out = ostringstream();
1043 main(in, out);
1044 const auto ans = out.str();
1045 ASSERT_EQ("9\n"
1046 "2\n",
1047 ans);
1048 }

引用了 main().

◆ TEST() [3/3]

acwing::acwing149::TEST ( acwing149  ,
case3   
)

在文件 acwing408_test.cpp1050 行定义.

1050 {
1051 istringstream in("16 3\n"
1052 "98\n"
1053 "98\n"
1054 "98\n"
1055 "98\n"
1056 "98\n"
1057 "98\n"
1058 "98\n"
1059 "98\n"
1060 "98\n"
1061 "98\n"
1062 "98\n"
1063 "98\n"
1064 "98\n"
1065 "98\n"
1066 "98\n"
1067 "98");
1068 auto out = ostringstream();
1069 main(in, out);
1070 const auto ans = out.str();
1071 ASSERT_EQ("4214\n"
1072 "3\n",
1073 ans);
1074 }

引用了 main().