POJ3253 Haffman
4168 단어 poj
분석:
간단한 하프만 나무의 응용.
AC 코드:
1 //Memory: 316K Time: 16MS
2 #include <iostream>
3 #include <cstring>
4 #include <cstdio>
5 #include <queue>
6
7 using namespace std;
8
9 int n;
10 int l;
11 priority_queue <int, vector<int>, greater<int>> q;
12
13 int main()
14 {
15 while ( !q.empty() ) q.pop();
16 scanf("%d", &n);
17 for (int i = 0; i < n; i++) {
18 scanf("%d", &l);
19 q.push(l);
20 }
21 long long ans = 0;
22 while ( q.size() >= 2 ) {
23 int a = q.top();
24 q.pop();
25 int b = q.top();
26 q.pop();
27 ans += (a + b);
28 q.push(a + b);
29 }
30 printf("%lld
", ans);
31 return 0;
32 }
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
POJ3071: Football(확률 DP)Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, …, 2n. After n rounds, only one team...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.