POJ3253 Haffman

4168 단어 poj
POJ3253
분석:
간단한 하프만 나무의 응용.
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 }

좋은 웹페이지 즐겨찾기